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[]