fix(nuxt): disallow directly rendering error page (#8673)

This commit is contained in:
pooya parsa 2022-11-03 22:48:29 +01:00 committed by GitHub
parent ea17148a6b
commit 253c8f7ee0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
import { createRenderer, renderResourceHeaders } from 'vue-bundle-renderer/runtime' import { createRenderer, renderResourceHeaders } from 'vue-bundle-renderer/runtime'
import type { RenderResponse } from 'nitropack' import type { RenderResponse } from 'nitropack'
import type { Manifest } from 'vite' import type { Manifest } from 'vite'
import { appendHeader, getQuery, writeEarlyHints } from 'h3' import { appendHeader, createError, getQuery, writeEarlyHints } from 'h3'
import devalue from '@nuxt/devalue' import devalue from '@nuxt/devalue'
import { joinURL } from 'ufo' import { joinURL } from 'ufo'
import { renderToString as _renderToString } from 'vue/server-renderer' import { renderToString as _renderToString } from 'vue/server-renderer'
@ -121,6 +121,10 @@ export default defineRenderHandler(async (event) => {
const ssrError = event.req.url?.startsWith('/__nuxt_error') const ssrError = event.req.url?.startsWith('/__nuxt_error')
? getQuery(event) as Exclude<NuxtApp['payload']['error'], Error> ? getQuery(event) as Exclude<NuxtApp['payload']['error'], Error>
: null : null
if (ssrError && event.req.socket.readyState !== 'readOnly' /* direct request */) {
throw createError('Cannot directly render error page!')
}
let url = ssrError?.url as string || event.req.url! let url = ssrError?.url as string || event.req.url!
// Whether we are rendering payload route // Whether we are rendering payload route