fix(nuxt): throw 404 when accessing `/__nuxt_error` directly (#20497)

This commit is contained in:
Daniel Roe 2023-04-25 15:47:02 +01:00 committed by GitHub
parent 4c200e4a2a
commit 200cb2787e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -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

View File

@ -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('/')