diff --git a/packages/nuxt/src/app/plugins/router.ts b/packages/nuxt/src/app/plugins/router.ts index 680c0a721e..b12c829c15 100644 --- a/packages/nuxt/src/app/plugins/router.ts +++ b/packages/nuxt/src/app/plugins/router.ts @@ -246,7 +246,10 @@ export default defineNuxtPlugin<{ route: Route, router: Router }>({ if (result === false || result instanceof Error) { const error = result || createError({ statusCode: 404, - statusMessage: `Page Not Found: ${initialURL}` + statusMessage: `Page Not Found: ${initialURL}`, + data: { + path: initialURL + } }) delete nuxtApp._processingMiddleware return nuxtApp.runWithContext(() => showError(error)) diff --git a/packages/nuxt/src/pages/runtime/plugins/router.ts b/packages/nuxt/src/pages/runtime/plugins/router.ts index e39e33d890..4c5934edc2 100644 --- a/packages/nuxt/src/pages/runtime/plugins/router.ts +++ b/packages/nuxt/src/pages/runtime/plugins/router.ts @@ -209,7 +209,10 @@ const plugin: Plugin<{ router: Router }> = defineNuxtPlugin({ await nuxtApp.runWithContext(() => showError(createError({ statusCode: 404, fatal: false, - statusMessage: `Page not found: ${to.fullPath}` + 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 || '/')) diff --git a/packages/nuxt/src/pages/runtime/validate.ts b/packages/nuxt/src/pages/runtime/validate.ts index 4b53777cee..b3aac24ed9 100644 --- a/packages/nuxt/src/pages/runtime/validate.ts +++ b/packages/nuxt/src/pages/runtime/validate.ts @@ -15,9 +15,13 @@ export default defineNuxtRouteMiddleware(async (to) => { if (import.meta.server) { return result } + const error = createError({ statusCode: 404, - statusMessage: `Page Not Found: ${to.fullPath}` + statusMessage: `Page Not Found: ${to.fullPath}`, + data: { + path: to.fullPath + } }) const unsub = router.beforeResolve((final) => { unsub() diff --git a/test/basic.test.ts b/test/basic.test.ts index 53436491b1..c3fa14dfac 100644 --- a/test/basic.test.ts +++ b/test/basic.test.ts @@ -155,6 +155,7 @@ describe('pages', () => { await page.getByText('should throw a 404 error').click() expect(await page.getByRole('heading').textContent()).toMatchInlineSnapshot('"Page Not Found: /forbidden"') + expect(await page.getByTestId('path').textContent()).toMatchInlineSnapshot('" Path: /forbidden"') await gotoPath(page, '/navigate-to-forbidden') await page.getByText('should be caught by catchall').click() diff --git a/test/fixtures/basic/error.vue b/test/fixtures/basic/error.vue index 7344c5e3b6..a5386a2229 100644 --- a/test/fixtures/basic/error.vue +++ b/test/fixtures/basic/error.vue @@ -3,6 +3,12 @@