From 63e6130d728b95da3155271e00aeed0140252e2a Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 10 May 2022 13:44:57 +0200 Subject: [PATCH] fix(nuxt): check for 404s after user middleware --- packages/nuxt/src/pages/runtime/router.ts | 27 ++++++++++------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/packages/nuxt/src/pages/runtime/router.ts b/packages/nuxt/src/pages/runtime/router.ts index 5fa4819e5e2..8b9b454f3df 100644 --- a/packages/nuxt/src/pages/runtime/router.ts +++ b/packages/nuxt/src/pages/runtime/router.ts @@ -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])