Skip to content

Commit

Permalink
fix(nuxt): abort navigation when updating window.location (#21521)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaspayot committed Jun 14, 2023
1 parent 1ab19b9 commit 187230b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion packages/nuxt/src/app/composables/router.ts
Expand Up @@ -148,8 +148,9 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na

const router = useRouter()

const nuxtApp = useNuxtApp()

if (process.server) {
const nuxtApp = useNuxtApp()
if (nuxtApp.ssrContext) {
const fullPath = typeof to === 'string' || isExternal ? toPath : router.resolve(to).fullPath || '/'
const location = isExternal ? toPath : joinURL(useRuntimeConfig().app.baseURL, fullPath)
Expand Down Expand Up @@ -183,6 +184,16 @@ export const navigateTo = (to: RouteLocationRaw | undefined | null, options?: Na
} else {
location.href = toPath
}
// Within in a Nuxt route middleware handler
if (inMiddleware) {
// Abort navigation when app is hydrated
if (!nuxtApp.isHydrating) {
return false
}
// When app is hydrating (i.e. on page load), we don't want to abort navigation as
// it would lead to a 404 error / page that's blinking before location changes.
return new Promise(() => {})
}
return Promise.resolve()
}

Expand Down
2 changes: 1 addition & 1 deletion test/bundle.test.ts
Expand Up @@ -35,7 +35,7 @@ describe.skipIf(process.env.SKIP_BUNDLE_SIZE === 'true' || process.env.ECOSYSTEM

it('default server bundle size', async () => {
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"61.9k"')
expect(roundToKilobytes(stats.server.totalBytes)).toMatchInlineSnapshot('"62.0k"')

const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect(roundToKilobytes(modules.totalBytes)).toMatchInlineSnapshot('"2295k"')
Expand Down

0 comments on commit 187230b

Please sign in to comment.