From 04ca5b8fc0ae267728687cc8d36069ee56776fee Mon Sep 17 00:00:00 2001 From: Romuald Brillout Date: Wed, 20 Nov 2024 15:49:34 +0100 Subject: [PATCH] feat: new hook onBeforeRenderHtml() (#87) --- packages/vike-react/src/+config.ts | 4 ++++ packages/vike-react/src/integration/onRenderHtml.tsx | 3 +++ packages/vike-react/src/types/Config.ts | 8 ++++++++ 3 files changed, 15 insertions(+) diff --git a/packages/vike-react/src/+config.ts b/packages/vike-react/src/+config.ts index 7143d410..9a6d5fd3 100644 --- a/packages/vike-react/src/+config.ts +++ b/packages/vike-react/src/+config.ts @@ -86,6 +86,10 @@ const config = { streamIsRequired: { env: { server: true }, }, + onBeforeRenderHtml: { + env: { server: true }, + cumulative: true, + }, onAfterRenderHtml: { env: { server: true }, cumulative: true, diff --git a/packages/vike-react/src/integration/onRenderHtml.tsx b/packages/vike-react/src/integration/onRenderHtml.tsx index b22f3607..045e1204 100644 --- a/packages/vike-react/src/integration/onRenderHtml.tsx +++ b/packages/vike-react/src/integration/onRenderHtml.tsx @@ -49,6 +49,9 @@ export type PageHtmlStream = Awaited> async function getPageHtml(pageContext: PageContextServer) { if (pageContext.Page) pageContext.page = getPageElement(pageContext).page + // https://github.com/vikejs/vike-react/issues/87#issuecomment-2488742744 + await callCumulativeHooks(pageContext.config.onBeforeRenderHtml, pageContext) + let pageHtml: string | ReturnType | PageHtmlStream = '' if (pageContext.page) { const { stream, streamIsRequired } = pageContext.config diff --git a/packages/vike-react/src/types/Config.ts b/packages/vike-react/src/types/Config.ts index 7260081a..f5bb9e08 100644 --- a/packages/vike-react/src/types/Config.ts +++ b/packages/vike-react/src/types/Config.ts @@ -184,6 +184,13 @@ declare global { */ reactStrictMode?: boolean + /** + * Hook called right before rendering the page's root React component to HTML. + * + * https://vike.dev/onBeforeRenderHtml + */ + onBeforeRenderHtml?: (pageContext: PageContextServer) => void + /** * Hook called right after rendering the page's root React component to HTML. * @@ -220,6 +227,7 @@ declare global { bodyHtmlEnd?: BodyHtmlBoundary[] bodyAttributes?: TagAttributes[] htmlAttributes?: TagAttributes[] + onBeforeRenderHtml?: Function[] onAfterRenderHtml?: Function[] onBeforeRenderClient?: Function[] onAfterRenderClient?: Function[]