mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-18 09:25:54 +00:00
fix(nuxt): respect replace
in middleware with navigateTo
(#30283)
This commit is contained in:
parent
1d795a5db4
commit
b4c0f17776
@ -152,6 +152,9 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
|
||||
|
||||
// Early redirect on client-side
|
||||
if (import.meta.client && !isExternal && inMiddleware) {
|
||||
if (options?.replace) {
|
||||
return typeof to === 'string' ? { path: to, replace: true } : { ...to, replace: true }
|
||||
}
|
||||
return to
|
||||
}
|
||||
|
||||
|
@ -21,8 +21,8 @@ describe.skipIf(process.env.SKIP_BUNDLE_SIZE === 'true' || process.env.ECOSYSTEM
|
||||
const [clientStats, clientStatsInlined] = await Promise.all((['.output', '.output-inline'])
|
||||
.map(outputDir => analyzeSizes(['**/*.js'], join(rootDir, outputDir, 'public'))))
|
||||
|
||||
expect.soft(roundToKilobytes(clientStats!.totalBytes)).toMatchInlineSnapshot(`"115k"`)
|
||||
expect.soft(roundToKilobytes(clientStatsInlined!.totalBytes)).toMatchInlineSnapshot(`"115k"`)
|
||||
expect.soft(roundToKilobytes(clientStats!.totalBytes)).toMatchInlineSnapshot(`"116k"`)
|
||||
expect.soft(roundToKilobytes(clientStatsInlined!.totalBytes)).toMatchInlineSnapshot(`"116k"`)
|
||||
|
||||
const files = new Set([...clientStats!.files, ...clientStatsInlined!.files].map(f => f.replace(/\..*\.js/, '.js')))
|
||||
|
||||
|
@ -623,6 +623,18 @@ describe('routing utilities: `navigateTo`', () => {
|
||||
expect(() => navigateTo(url, { external: true })).toThrowError(`Cannot navigate to a URL with '${protocol}:' protocol.`)
|
||||
}
|
||||
})
|
||||
it('navigateTo should replace current navigation state if called within middleware', () => {
|
||||
const nuxtApp = useNuxtApp()
|
||||
nuxtApp._processingMiddleware = true
|
||||
expect(navigateTo('/')).toMatchInlineSnapshot(`"/"`)
|
||||
expect(navigateTo('/', { replace: true })).toMatchInlineSnapshot(`
|
||||
{
|
||||
"path": "/",
|
||||
"replace": true,
|
||||
}
|
||||
`)
|
||||
nuxtApp._processingMiddleware = false
|
||||
})
|
||||
})
|
||||
|
||||
describe('routing utilities: `resolveRouteObject`', () => {
|
||||
|
Loading…
Reference in New Issue
Block a user