Skip to content

Commit

Permalink
fix(nuxt): throw 404 when accessing /__nuxt_error directly (#20497)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Apr 25, 2023
1 parent 4c200e4 commit 200cb27
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion packages/nuxt/src/core/runtime/nitro/renderer.ts
Expand Up @@ -180,7 +180,10 @@ export default defineRenderHandler(async (event) => {
}

if (ssrError && event.node.req.socket.readyState !== 'readOnly' /* direct request */) {
throw createError('Cannot directly render error page!')
throw createError({
statusCode: 404,
statusMessage: 'Page Not Found: /__nuxt_error'
})
}

// Check for island component rendering
Expand Down
19 changes: 19 additions & 0 deletions test/basic.test.ts
Expand Up @@ -574,6 +574,25 @@ describe('errors', () => {
expect(await res.text()).toContain('This is a custom error')
})

it('should not allow accessing error route directly', async () => {
const res = await fetch('/__nuxt_error', {
headers: {
accept: 'application/json'
}
})
expect(res.status).toBe(404)
const error = await res.json()
delete error.stack
expect(error).toMatchInlineSnapshot(`
{
"message": "Page Not Found: /__nuxt_error",
"statusCode": 404,
"statusMessage": "Page Not Found: /__nuxt_error",
"url": "/__nuxt_error",
}
`)
})

// TODO: need to create test for webpack
it.runIf(!isDev() && !isWebpack)('should handle chunk loading errors', async () => {
const { page, consoleLogs } = await renderPage('/')
Expand Down

0 comments on commit 200cb27

Please sign in to comment.