mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-26 15:42:09 +00:00
fix(nuxt): allow responding with custom headers from error.vue
(#8469)
This commit is contained in:
parent
1d795f0274
commit
12808f175b
@ -1,7 +1,7 @@
|
|||||||
import { withQuery } from 'ufo'
|
import { withQuery } from 'ufo'
|
||||||
import type { NitroErrorHandler } from 'nitropack'
|
import type { NitroErrorHandler } from 'nitropack'
|
||||||
import type { H3Error } from 'h3'
|
import { H3Error, setResponseHeader, getRequestHeaders } from 'h3'
|
||||||
import { getRequestHeaders } from 'h3'
|
import { useNitroApp } from '#internal/nitro'
|
||||||
import { normalizeError, isJsonRequest } from '#internal/nitro/utils'
|
import { normalizeError, isJsonRequest } from '#internal/nitro/utils'
|
||||||
|
|
||||||
export default <NitroErrorHandler> async function errorhandler (error: H3Error, event) {
|
export default <NitroErrorHandler> async function errorhandler (error: H3Error, event) {
|
||||||
@ -46,14 +46,15 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
|
|||||||
|
|
||||||
// HTML response (via SSR)
|
// HTML response (via SSR)
|
||||||
const isErrorPage = event.req.url?.startsWith('/__nuxt_error')
|
const isErrorPage = event.req.url?.startsWith('/__nuxt_error')
|
||||||
let html = !isErrorPage
|
const res = !isErrorPage
|
||||||
? await $fetch(withQuery('/__nuxt_error', errorObject), {
|
? await useNitroApp().localFetch(withQuery('/__nuxt_error', errorObject), {
|
||||||
headers: getRequestHeaders(event) as HeadersInit
|
headers: getRequestHeaders(event) as Record<string, string>,
|
||||||
|
redirect: 'manual'
|
||||||
}).catch(() => null)
|
}).catch(() => null)
|
||||||
: null
|
: null
|
||||||
|
|
||||||
// Fallback to static rendered error page
|
// Fallback to static rendered error page
|
||||||
if (!html) {
|
if (!res) {
|
||||||
const { template } = process.dev
|
const { template } = process.dev
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
? await import('@nuxt/ui-templates/templates/error-dev.mjs')
|
? await import('@nuxt/ui-templates/templates/error-dev.mjs')
|
||||||
@ -63,9 +64,22 @@ export default <NitroErrorHandler> async function errorhandler (error: H3Error,
|
|||||||
// TODO: Support `message` in template
|
// TODO: Support `message` in template
|
||||||
(errorObject as any).description = errorObject.message
|
(errorObject as any).description = errorObject.message
|
||||||
}
|
}
|
||||||
html = template(errorObject)
|
event.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
|
||||||
|
event.res.end(template(errorObject))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
event.res.setHeader('Content-Type', 'text/html;charset=UTF-8')
|
for (const [header, value] of res.headers.entries()) {
|
||||||
event.res.end(html)
|
setResponseHeader(event, header, value)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.status && res.status !== 200) {
|
||||||
|
event.res.statusCode = res.status
|
||||||
|
}
|
||||||
|
|
||||||
|
if (res.statusText) {
|
||||||
|
event.res.statusMessage = res.statusText
|
||||||
|
}
|
||||||
|
|
||||||
|
event.res.end(await res.text())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user