mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 05:35:13 +00:00
fix(nuxt): avoid recursive ssr errors (#24399)
This commit is contained in:
parent
abbcaf6b20
commit
1012dc0dbd
@ -45,14 +45,20 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
|
||||
return send(event, JSON.stringify(errorObject))
|
||||
}
|
||||
|
||||
// Access request headers
|
||||
const reqHeaders = getRequestHeaders(event)
|
||||
|
||||
// Detect to avoid recursion in SSR rendering of errors
|
||||
const isRenderingError = event.path.startsWith('/__nuxt_error') || !!reqHeaders['x-nuxt-error']
|
||||
|
||||
// HTML response (via SSR)
|
||||
const isErrorPage = event.path.startsWith('/__nuxt_error')
|
||||
const res = !isErrorPage
|
||||
? await useNitroApp().localFetch(withQuery(joinURL(useRuntimeConfig().app.baseURL, '/__nuxt_error'), errorObject), {
|
||||
headers: getRequestHeaders(event) as Record<string, string>,
|
||||
const res = isRenderingError ? null : await useNitroApp().localFetch(
|
||||
withQuery(joinURL(useRuntimeConfig().app.baseURL, '/__nuxt_error'), errorObject),
|
||||
{
|
||||
headers: { ...reqHeaders, 'x-nuxt-error': 'true' },
|
||||
redirect: 'manual'
|
||||
}).catch(() => null)
|
||||
: null
|
||||
}
|
||||
).catch(() => null)
|
||||
|
||||
// Fallback to static rendered error page
|
||||
if (!res) {
|
||||
|
@ -814,6 +814,17 @@ describe('errors', () => {
|
||||
"url": "/__nuxt_error",
|
||||
}
|
||||
`)
|
||||
|
||||
it('should not recursively throw an error when there is an error rendering the error page', async () => {
|
||||
const res = await $fetch('/', {
|
||||
headers: {
|
||||
'x-test-recurse-error': 'true',
|
||||
accept: 'text/html'
|
||||
}
|
||||
})
|
||||
expect(typeof res).toBe('string')
|
||||
expect(res).toContain('Hello Nuxt 3!')
|
||||
})
|
||||
})
|
||||
|
||||
// TODO: need to create test for webpack
|
||||
|
5
test/fixtures/basic/plugins/error.server.ts
vendored
Normal file
5
test/fixtures/basic/plugins/error.server.ts
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
export default defineNuxtPlugin(async () => {
|
||||
if (useRequestHeaders(['x-test-recurse-error'])['x-test-recurse-error']) {
|
||||
await useRequestFetch()('/api/error').catch(() => {})
|
||||
}
|
||||
})
|
3
test/fixtures/basic/server/api/error.ts
vendored
Normal file
3
test/fixtures/basic/server/api/error.ts
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
export default defineEventHandler(async () => {
|
||||
throw createError({ statusCode: 400 })
|
||||
})
|
Loading…
Reference in New Issue
Block a user