mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-13 09:33:54 +00:00
fix(nuxt3): return error page on blocked initial navigation (#3201)
This commit is contained in:
parent
9734856fb0
commit
3589a2d55f
@ -6,6 +6,7 @@ import {
|
|||||||
RouterLink,
|
RouterLink,
|
||||||
NavigationGuard
|
NavigationGuard
|
||||||
} from 'vue-router'
|
} from 'vue-router'
|
||||||
|
import { createError } from 'h3'
|
||||||
import NuxtPage from './page'
|
import NuxtPage from './page'
|
||||||
import NuxtLayout from './layout'
|
import NuxtLayout from './layout'
|
||||||
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app'
|
import { callWithNuxt, defineNuxtPlugin, useRuntimeConfig } from '#app'
|
||||||
@ -93,6 +94,15 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const result = await callWithNuxt(nuxtApp, middleware, [to, from])
|
const result = await callWithNuxt(nuxtApp, middleware, [to, from])
|
||||||
|
if (process.server) {
|
||||||
|
if (result === false || result instanceof Error) {
|
||||||
|
const error = result || createError({
|
||||||
|
statusMessage: `Route navigation aborted: ${nuxtApp.ssrContext.url}`
|
||||||
|
})
|
||||||
|
nuxtApp.ssrContext.error = error
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
if (result || result === false) { return result }
|
if (result || result === false) { return result }
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -112,13 +122,17 @@ export default defineNuxtPlugin((nuxtApp) => {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
await router.isReady()
|
try {
|
||||||
|
await router.isReady()
|
||||||
|
|
||||||
const is404 = router.currentRoute.value.matched.length === 0
|
const is404 = router.currentRoute.value.matched.length === 0
|
||||||
if (process.server && is404) {
|
if (process.server && is404) {
|
||||||
const error = new Error(`Page not found: ${nuxtApp.ssrContext.url}`)
|
throw createError({
|
||||||
// @ts-ignore
|
statusCode: 404,
|
||||||
error.statusCode = 404
|
statusMessage: `Page not found: ${nuxtApp.ssrContext.url}`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
nuxtApp.ssrContext.error = error
|
nuxtApp.ssrContext.error = error
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user