From 24a499d285777997b2f5fd1f2e462b5fd94e70fb Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Thu, 11 Aug 2022 18:34:39 +0200 Subject: [PATCH] fix(nuxt)!: allow `app:rendered` to modify ssr context and add `render:html` (#6521) --- packages/nuxt/src/app/nuxt.ts | 6 +- .../nuxt/src/core/runtime/nitro/renderer.ts | 70 +++++++++---------- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 2f5bc1ec72..e933ae2527 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -6,8 +6,6 @@ import type { RuntimeConfig } from '@nuxt/schema' import { getContext } from 'unctx' import type { SSRContext } from 'vue-bundle-renderer/runtime' import type { CompatibilityEvent } from 'h3' -// eslint-disable-next-line import/no-restricted-paths -import type { NuxtRenderContext } from '../core/runtime/nitro/renderer' const nuxtAppCtx = getContext('nuxt-app') @@ -21,11 +19,13 @@ type NuxtMeta = { } type HookResult = Promise | void + +type AppRenderedContext = { ssrContext: NuxtApp['ssrContext'] } export interface RuntimeNuxtHooks { 'app:created': (app: App) => HookResult 'app:beforeMount': (app: App) => HookResult 'app:mounted': (app: App) => HookResult - 'app:rendered': (ctx: NuxtRenderContext) => HookResult + 'app:rendered': (ctx: AppRenderedContext) => HookResult 'app:redirected': () => HookResult 'app:suspense:resolve': (Component?: VNode) => HookResult 'app:error': (err: any) => HookResult diff --git a/packages/nuxt/src/core/runtime/nitro/renderer.ts b/packages/nuxt/src/core/runtime/nitro/renderer.ts index ab9d58042d..ff4225d6d0 100644 --- a/packages/nuxt/src/core/runtime/nitro/renderer.ts +++ b/packages/nuxt/src/core/runtime/nitro/renderer.ts @@ -15,16 +15,13 @@ export type NuxtSSRContext = NuxtApp['ssrContext'] const defineRenderHandler = _defineRenderHandler as (h: RenderHandler) => CompatibilityEvent -export interface NuxtRenderContext { - ssrContext: NuxtSSRContext - html: { - htmlAttrs: string[] - head: string[] - bodyAttrs: string[] - bodyPreprend: string[] - body: string[] - bodyAppend: string[] - } +export interface NuxtRenderHTMLContext { + htmlAttrs: string[] + head: string[] + bodyAttrs: string[] + bodyPreprend: string[] + body: string[] + bodyAppend: string[] } export interface NuxtRenderResponse { @@ -124,6 +121,7 @@ export default defineRenderHandler(async (event) => { const _rendered = await renderer.renderToString(ssrContext).catch((err) => { if (!ssrError) { throw err } }) + await ssrContext.nuxt?.hooks.callHook('app:rendered', { ssrContext }) // Handle errors if (!_rendered) { @@ -137,42 +135,38 @@ export default defineRenderHandler(async (event) => { const renderedMeta = await ssrContext.renderMeta?.() ?? {} // Create render context - const rendered: NuxtRenderContext = { - ssrContext, - html: { - htmlAttrs: normalizeChunks([renderedMeta.htmlAttrs]), - head: normalizeChunks([ - renderedMeta.headTags, - _rendered.renderResourceHints(), - _rendered.renderStyles(), - ssrContext.styles - ]), - bodyAttrs: normalizeChunks([renderedMeta.bodyAttrs]), - bodyPreprend: normalizeChunks([ - renderedMeta.bodyScriptsPrepend, - ssrContext.teleports?.body - ]), - body: [ + const htmlContext: NuxtRenderHTMLContext = { + htmlAttrs: normalizeChunks([renderedMeta.htmlAttrs]), + head: normalizeChunks([ + renderedMeta.headTags, + _rendered.renderResourceHints(), + _rendered.renderStyles(), + ssrContext.styles + ]), + bodyAttrs: normalizeChunks([renderedMeta.bodyAttrs]), + bodyPreprend: normalizeChunks([ + renderedMeta.bodyScriptsPrepend, + ssrContext.teleports?.body + ]), + body: [ // TODO: Rename to _rendered.body in next vue-bundle-renderer - _rendered.html - ], - bodyAppend: normalizeChunks([ + _rendered.html + ], + bodyAppend: normalizeChunks([ ``, _rendered.renderScripts(), // Note: bodyScripts may contain tags other than