fix(nuxt): respect query/hash for external routes in navigateTo (#21500)

This commit is contained in:
Daniel Roe 2023-06-11 22:27:02 +01:00 committed by GitHub
parent 03186d6d44
commit 05a8c2d955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 5 additions and 5 deletions

View File

@ -2,7 +2,7 @@ import { getCurrentInstance, hasInjectionContext, inject, onUnmounted } from 'vu
import type { Ref } from 'vue' import type { Ref } from 'vue'
import type { NavigationFailure, NavigationGuard, RouteLocationNormalized, RouteLocationPathRaw, RouteLocationRaw, Router, useRoute as _useRoute, useRouter as _useRouter } from '#vue-router' import type { NavigationFailure, NavigationGuard, RouteLocationNormalized, RouteLocationPathRaw, RouteLocationRaw, Router, useRoute as _useRoute, useRouter as _useRouter } from '#vue-router'
import { sanitizeStatusCode } from 'h3' import { sanitizeStatusCode } from 'h3'
import { hasProtocol, joinURL, parseURL } from 'ufo' import { hasProtocol, joinURL, parseURL, withQuery } from 'ufo'
import { useNuxtApp, useRuntimeConfig } from '../nuxt' import { useNuxtApp, useRuntimeConfig } from '../nuxt'
import type { NuxtError } from './error' import type { NuxtError } from './error'
@ -113,7 +113,7 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
to = '/' to = '/'
} }
const toPath = typeof to === 'string' ? to : ((to as RouteLocationPathRaw).path || '/') const toPath = typeof to === 'string' ? to : (withQuery((to as RouteLocationPathRaw).path || '/', to.query || {}) + (to.hash || ''))
// Early open handler // Early open handler
if (options?.open) { if (options?.open) {

View File

@ -788,7 +788,7 @@ describe('navigate external', () => {
it('should redirect to example.com', async () => { it('should redirect to example.com', async () => {
const { headers } = await fetch('/navigate-to-external/', { redirect: 'manual' }) const { headers } = await fetch('/navigate-to-external/', { redirect: 'manual' })
expect(headers.get('location')).toEqual('https://example.com/') expect(headers.get('location')).toEqual('https://example.com/?redirect=false#test')
}) })
it('should redirect to api endpoint', async () => { it('should redirect to api endpoint', async () => {

View File

@ -25,7 +25,7 @@ describe.skipIf(process.env.SKIP_BUNDLE_SIZE === 'true' || process.env.ECOSYSTEM
it('default client bundle size', async () => { it('default client bundle size', async () => {
stats.client = await analyzeSizes('**/*.js', publicDir) stats.client = await analyzeSizes('**/*.js', publicDir)
expect(roundToKilobytes(stats.client.totalBytes)).toMatchInlineSnapshot('"97.1k"') expect(roundToKilobytes(stats.client.totalBytes)).toMatchInlineSnapshot('"97.2k"')
expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(` expect(stats.client.files.map(f => f.replace(/\..*\.js/, '.js'))).toMatchInlineSnapshot(`
[ [
"_nuxt/entry.js", "_nuxt/entry.js",

View File

@ -8,5 +8,5 @@ if (useRoute().path === '/navigate-to-external') {
throw new Error('this should not run') throw new Error('this should not run')
}) })
} }
await navigateTo('https://example.com/', { external: true, replace: true }) await navigateTo({ path: 'https://example.com/', query: { redirect: false }, hash: '#test' }, { external: true, replace: true })
</script> </script>