Skip to content

Commit

Permalink
feat: new hook onBeforeRenderHtml() (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Nov 20, 2024
1 parent d45d142 commit 04ca5b8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/vike-react/src/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ const config = {
streamIsRequired: {
env: { server: true },
},
onBeforeRenderHtml: {
env: { server: true },
cumulative: true,
},
onAfterRenderHtml: {
env: { server: true },
cumulative: true,
Expand Down
3 changes: 3 additions & 0 deletions packages/vike-react/src/integration/onRenderHtml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ export type PageHtmlStream = Awaited<ReturnType<typeof renderToStream>>
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<typeof dangerouslySkipEscape> | PageHtmlStream = ''
if (pageContext.page) {
const { stream, streamIsRequired } = pageContext.config
Expand Down
8 changes: 8 additions & 0 deletions packages/vike-react/src/types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -220,6 +227,7 @@ declare global {
bodyHtmlEnd?: BodyHtmlBoundary[]
bodyAttributes?: TagAttributes[]
htmlAttributes?: TagAttributes[]
onBeforeRenderHtml?: Function[]
onAfterRenderHtml?: Function[]
onBeforeRenderClient?: Function[]
onAfterRenderClient?: Function[]
Expand Down

0 comments on commit 04ca5b8

Please sign in to comment.