Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(nuxt3): return error page on blocked initial navigation #3201

Merged
merged 7 commits into from Feb 14, 2022
22 changes: 16 additions & 6 deletions packages/nuxt3/src/pages/runtime/router.ts
Expand Up @@ -93,6 +93,11 @@ export default defineNuxtPlugin((nuxtApp) => {
}

const result = await callWithNuxt(nuxtApp, middleware, [to, from])
if (process.server) {
if (result === false || result instanceof Error) {
throw result || new Error(`Route navigation aborted: ${nuxtApp.ssrContext.url}`)
danielroe marked this conversation as resolved.
Show resolved Hide resolved
}
}
if (result || result === false) { return result }
}
})
Expand All @@ -112,13 +117,18 @@ export default defineNuxtPlugin((nuxtApp) => {
})
}

await router.isReady()
try {
await router.isReady()

const is404 = router.currentRoute.value.matched.length === 0
if (process.server && is404) {
const error = new Error(`Page not found: ${nuxtApp.ssrContext.url}`)
// @ts-ignore
error.statusCode = 404
const is404 = router.currentRoute.value.matched.length === 0
if (process.server && is404) {
const error = new Error(`Page not found: ${nuxtApp.ssrContext.url}`)
// @ts-ignore
error.statusCode = 404
danielroe marked this conversation as resolved.
Show resolved Hide resolved
nuxtApp.ssrContext.error = error
}
} catch (error) {
error.statusCode = error.statusCode || 500
danielroe marked this conversation as resolved.
Show resolved Hide resolved
nuxtApp.ssrContext.error = error
}
})
Expand Down