fix(nuxt): check for 404s after user middleware (#4913)

This commit is contained in:
Daniel Roe 2022-05-12 10:13:38 +01:00 committed by GitHub
parent 0475d475d9
commit 46c656c4b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -108,20 +108,6 @@ export default defineNuxtPlugin(async (nuxtApp) => {
}
const error = useError()
router.afterEach(async (to) => {
if (process.client && !nuxtApp.isHydrating && error.value) {
// Clear any existing errors
await callWithNuxt(nuxtApp, clearError)
}
if (to.matched.length === 0) {
callWithNuxt(nuxtApp, throwError, [createError({
statusCode: 404,
statusMessage: `Page not found: ${to.fullPath}`
})])
} else if (process.server && to.matched[0].name === '404' && nuxtApp.ssrContext) {
nuxtApp.ssrContext.res.statusCode = 404
}
})
try {
if (process.server) {
@ -175,7 +161,18 @@ export default defineNuxtPlugin(async (nuxtApp) => {
router.afterEach(async (to) => {
delete nuxtApp._processingMiddleware
if (process.server) {
if (process.client && !nuxtApp.isHydrating && error.value) {
// Clear any existing errors
await callWithNuxt(nuxtApp, clearError)
}
if (to.matched.length === 0) {
callWithNuxt(nuxtApp, throwError, [createError({
statusCode: 404,
statusMessage: `Page not found: ${to.fullPath}`
})])
} else if (process.server && to.matched[0].name === '404' && nuxtApp.ssrContext) {
nuxtApp.ssrContext.res.statusCode = 404
} else if (process.server) {
const currentURL = to.fullPath || '/'
if (!isEqual(currentURL, initialURL)) {
await callWithNuxt(nuxtApp, navigateTo, [currentURL])