fix(nuxt): respect replace in middleware with navigateTo (#30283)

This commit is contained in:
Connor Roberts 2024-12-18 17:26:32 +07:00 committed by GitHub
parent 1d795a5db4
commit b4c0f17776
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 2 deletions

View File

@ -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
}

View File

@ -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')))

View File

@ -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`', () => {