Skip to content

Commit 3c64203

Browse files
author
Jason Gao
committed
create postRenderHead hooks
1 parent 3204adc commit 3c64203

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

packages/fastify-renderer/src/node/DocumentTemplate.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface TemplateData<Props> {
66
tail: NodeJS.ReadableStream
77
content: string | NodeJS.ReadableStream
88
props: Props
9+
postRenderHead: NodeJS.ReadableStream
910
}
1011

1112
/** A template renders out a full HTML document given the content for the document and the scripts for the document, and can optionally grab values out of the props to use for other bits like the page title, metatags, or non-client-side-hydrated body content. */
@@ -19,6 +20,7 @@ export const DefaultDocumentTemplate: Template = (data: TemplateData<any>) => te
1920
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
2021
<title>${data.props.title || 'Fastify Renderer App'}</title>
2122
${data.head}
23+
${data.postRenderHead}
2224
</head>
2325
<body>
2426
<div id="fstrapp">${data.content}</div>

packages/fastify-renderer/src/node/renderers/react/ReactRenderer.tsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,19 @@ export class ReactRenderer implements Renderer {
204204

205205
private renderStreamingTemplate<Props>(app: JSX.Element, bus: RenderBus, ReactDOMServer: any, render: Render<Props>) {
206206
this.runHeadHooks(bus)
207+
// Do nothing for now when using streaming template
208+
bus.push('postRenderHead', null)
207209
const contentStream = ReactDOMServer.renderToNodeStream(app)
208-
contentStream.on('end', () => this.runTailHooks(bus))
210+
contentStream.on('end', () => {
211+
this.runTailHooks(bus)
212+
})
209213

210214
return render.document({
211215
content: contentStream,
212216
head: bus.stack('head'),
213217
tail: bus.stack('tail'),
214218
props: render.props,
219+
postRenderHead: bus.stack('postRenderHead'),
215220
})
216221
}
217222

@@ -221,18 +226,30 @@ export class ReactRenderer implements Renderer {
221226
ReactDOMServer: any,
222227
render: Render<Props>
223228
) {
224-
const content = ReactDOMServer.renderToString(app)
225229
this.runHeadHooks(bus)
230+
const content = ReactDOMServer.renderToString(app)
231+
this.runPostRenderHeadHooks(bus)
226232
this.runTailHooks(bus)
227233

228234
return render.document({
229235
content,
230236
head: bus.stack('head'),
231237
tail: bus.stack('tail'),
232238
props: render.props,
239+
postRenderHead: bus.stack('postRenderHead'),
233240
})
234241
}
235242

243+
private runPostRenderHeadHooks(bus: RenderBus) {
244+
// Run any heads hooks that might want to push something after the content
245+
for (const hook of this.plugin.hooks) {
246+
if (hook.postRenderHeads) {
247+
bus.push('postRenderHead', hook.postRenderHeads())
248+
}
249+
}
250+
bus.push('postRenderHead', null)
251+
}
252+
236253
private runHeadHooks(bus: RenderBus) {
237254
// Run any heads hooks that might want to push something before the content
238255
for (const hook of this.plugin.hooks) {

packages/fastify-renderer/src/node/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface FastifyRendererHook {
2525
tails?: () => string
2626
heads?: () => string
2727
transform?: (app: ReactElement) => ReactElement
28+
postRenderHeads?: () => string
2829
}
2930

3031
export interface ViteClientManifest {

0 commit comments

Comments
 (0)