perf(nitro): respond with early hints in node-based environments (#7893)

This commit is contained in:
Daniel Roe 2022-10-10 12:02:35 +01:00 committed by GitHub
parent 9521fc7234
commit 5a2616cfee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,4 @@
import { createRenderer } 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 } from 'h3' import { appendHeader, getQuery } from 'h3'
@ -100,7 +100,10 @@ const getSPARenderer = lazyCachedFunction(async () => {
return Promise.resolve(result) return Promise.resolve(result)
} }
return { renderToString } return {
rendererContext: renderer.rendererContext,
renderToString
}
}) })
const PAYLOAD_CACHE = (process.env.NUXT_PAYLOAD_EXTRACTION && process.env.prerender) ? new Map() : null // TODO: Use LRU cache const PAYLOAD_CACHE = (process.env.NUXT_PAYLOAD_EXTRACTION && process.env.prerender) ? new Map() : null // TODO: Use LRU cache
@ -150,6 +153,14 @@ export default defineRenderHandler(async (event) => {
// Render app // Render app
const renderer = (process.env.NUXT_NO_SSR || ssrContext.noSSR) ? await getSPARenderer() : await getSSRRenderer() const renderer = (process.env.NUXT_NO_SSR || ssrContext.noSSR) ? await getSPARenderer() : await getSSRRenderer()
// Render 103 Early Hints
if (!isRenderingPayload && !process.env.prerender && event.res.socket) {
const { link } = renderResourceHeaders({}, renderer.rendererContext)
// TODO: use https://github.com/nodejs/node/pull/44180 when we drop support for node 16
event.res.socket!.write(`HTTP/1.1 103 Early Hints\r\nLink: ${link}\r\n\r\n`, 'utf-8')
}
const _rendered = await renderer.renderToString(ssrContext).catch((err) => { const _rendered = await renderer.renderToString(ssrContext).catch((err) => {
if (!ssrError) { if (!ssrError) {
// Use explicitly thrown error in preference to subsequent rendering errors // Use explicitly thrown error in preference to subsequent rendering errors