perf(nuxt)!: don't call render:html for server islands (#27889)

This commit is contained in:
Julien Huang 2024-07-01 11:12:18 +02:00 committed by GitHub
parent e8faa84d83
commit 5ac6aa574a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -461,23 +461,6 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse
// remove certain tags for nuxt islands // remove certain tags for nuxt islands
const { headTags, bodyTags, bodyTagsOpen, htmlAttrs, bodyAttrs } = await renderSSRHead(head, renderSSRHeadOptions) const { headTags, bodyTags, bodyTagsOpen, htmlAttrs, bodyAttrs } = await renderSSRHead(head, renderSSRHeadOptions)
// Create render context
const htmlContext: NuxtRenderHTMLContext = {
island: isRenderingIsland,
htmlAttrs: htmlAttrs ? [htmlAttrs] : [],
head: normalizeChunks([headTags]),
bodyAttrs: bodyAttrs ? [bodyAttrs] : [],
bodyPrepend: normalizeChunks([bodyTagsOpen, ssrContext.teleports?.body]),
body: [
componentIslands ? replaceIslandTeleports(ssrContext, _rendered.html) : _rendered.html,
APP_TELEPORT_OPEN_TAG + (HAS_APP_TELEPORTS ? joinTags([ssrContext.teleports?.[`#${appTeleportAttrs.id}`]]) : '') + APP_TELEPORT_CLOSE_TAG,
],
bodyAppend: [bodyTags],
}
// Allow hooking into the rendered result
await nitroApp.hooks.callHook('render:html', htmlContext, { event })
// Response for component islands // Response for component islands
if (isRenderingIsland && islandContext) { if (isRenderingIsland && islandContext) {
const islandHead: NuxtIslandResponse['head'] = { const islandHead: NuxtIslandResponse['head'] = {
@ -494,7 +477,7 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse
const islandResponse: NuxtIslandResponse = { const islandResponse: NuxtIslandResponse = {
id: islandContext.id, id: islandContext.id,
head: islandHead, head: islandHead,
html: getServerComponentHTML(htmlContext.body), html: getServerComponentHTML(_rendered.html),
components: getClientIslandResponse(ssrContext), components: getClientIslandResponse(ssrContext),
slots: getSlotIslandResponse(ssrContext), slots: getSlotIslandResponse(ssrContext),
} }
@ -517,6 +500,23 @@ export default defineRenderHandler(async (event): Promise<Partial<RenderResponse
return response return response
} }
// Create render context
const htmlContext: NuxtRenderHTMLContext = {
island: isRenderingIsland,
htmlAttrs: htmlAttrs ? [htmlAttrs] : [],
head: normalizeChunks([headTags]),
bodyAttrs: bodyAttrs ? [bodyAttrs] : [],
bodyPrepend: normalizeChunks([bodyTagsOpen, ssrContext.teleports?.body]),
body: [
componentIslands ? replaceIslandTeleports(ssrContext, _rendered.html) : _rendered.html,
APP_TELEPORT_OPEN_TAG + (HAS_APP_TELEPORTS ? joinTags([ssrContext.teleports?.[`#${appTeleportAttrs.id}`]]) : '') + APP_TELEPORT_CLOSE_TAG,
],
bodyAppend: [bodyTags],
}
// Allow hooking into the rendered result
await nitroApp.hooks.callHook('render:html', htmlContext, { event })
// Construct HTML response // Construct HTML response
const response = { const response = {
body: renderHTMLDocument(htmlContext), body: renderHTMLDocument(htmlContext),
@ -637,8 +637,8 @@ function splitPayload (ssrContext: NuxtSSRContext) {
/** /**
* remove the root node from the html body * remove the root node from the html body
*/ */
function getServerComponentHTML (body: string[]): string { function getServerComponentHTML (body: string): string {
const match = body[0].match(ROOT_NODE_REGEX) const match = body.match(ROOT_NODE_REGEX)
return match ? match[1] : body[0] return match ? match[1] : body[0]
} }