mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-14 01:53:55 +00:00
fix: avoid throwing a 404 error before the middleware is executed
This commit is contained in:
parent
5faf720b42
commit
a340ebd13d
@ -148,16 +148,8 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
|
|||||||
if (import.meta.server && failure?.type === 4 /* ErrorTypes.NAVIGATION_ABORTED */) {
|
if (import.meta.server && failure?.type === 4 /* ErrorTypes.NAVIGATION_ABORTED */) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (to.matched.length === 0) {
|
|
||||||
await nuxtApp.runWithContext(() => showError(createError({
|
if (import.meta.server && to.redirectedFrom && to.fullPath !== initialURL) {
|
||||||
statusCode: 404,
|
|
||||||
fatal: false,
|
|
||||||
statusMessage: `Page not found: ${to.fullPath}`,
|
|
||||||
data: {
|
|
||||||
path: to.fullPath,
|
|
||||||
},
|
|
||||||
})))
|
|
||||||
} else if (import.meta.server && to.redirectedFrom && to.fullPath !== initialURL) {
|
|
||||||
await nuxtApp.runWithContext(() => navigateTo(to.fullPath || '/'))
|
await nuxtApp.runWithContext(() => navigateTo(to.fullPath || '/'))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -252,6 +244,19 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({
|
|||||||
await nuxtApp.callHook('page:loading:end')
|
await nuxtApp.callHook('page:loading:end')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.afterEach(async (to, _from) => {
|
||||||
|
if (to.matched.length === 0) {
|
||||||
|
await nuxtApp.runWithContext(() => showError(createError({
|
||||||
|
statusCode: 404,
|
||||||
|
fatal: false,
|
||||||
|
statusMessage: `Page not found: ${to.fullPath}`,
|
||||||
|
data: {
|
||||||
|
path: to.fullPath
|
||||||
|
}
|
||||||
|
})))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
nuxtApp.hooks.hookOnce('app:created', async () => {
|
nuxtApp.hooks.hookOnce('app:created', async () => {
|
||||||
try {
|
try {
|
||||||
// #4920, #4982
|
// #4920, #4982
|
||||||
|
Loading…
Reference in New Issue
Block a user