@@ -7,7 +7,8 @@ import { normalizePath } from 'vite/dist/node'
7
7
import { FastifyRendererPlugin } from '../../Plugin'
8
8
import { RenderBus } from '../../RenderBus'
9
9
import { wrap } from '../../tracing'
10
- import { mapFilepathToEntrypointName } from '../../utils'
10
+ import { FastifyRendererHook } from '../../types'
11
+ import { mapFilepathToEntrypointName , unthunk } from '../../utils'
11
12
import { Render , RenderableRoute , Renderer } from '../Renderer'
12
13
13
14
const CLIENT_ENTRYPOINT_PREFIX = '/@fstr!entrypoint:'
@@ -79,6 +80,7 @@ export class ReactRenderer implements Renderer {
79
80
/** Renders a given request and sends the resulting HTML document out with the `reply`. */
80
81
private wrappedRender = wrap ( 'fastify-renderer.render' , async < Props , > ( render : Render < Props > ) : Promise < void > => {
81
82
const bus = this . startRenderBus ( render )
83
+ const hooks = this . plugin . hooks . map ( unthunk )
82
84
83
85
try {
84
86
const url = this . entrypointRequirePathForServer ( render )
@@ -97,16 +99,16 @@ export class ReactRenderer implements Renderer {
97
99
</ RenderBusContext . Provider >
98
100
)
99
101
100
- for ( const hook of this . plugin . hooks ) {
102
+ for ( const hook of hooks ) {
101
103
if ( hook . transform ) {
102
104
app = hook . transform ( app )
103
105
}
104
106
}
105
107
106
108
if ( this . options . mode == 'streaming' ) {
107
- await render . reply . send ( this . renderStreamingTemplate ( app , bus , ReactDOMServer , render ) )
109
+ await render . reply . send ( this . renderStreamingTemplate ( app , bus , ReactDOMServer , render , hooks ) )
108
110
} else {
109
- await render . reply . send ( this . renderSynchronousTemplate ( app , bus , ReactDOMServer , render ) )
111
+ await render . reply . send ( this . renderSynchronousTemplate ( app , bus , ReactDOMServer , render , hooks ) )
110
112
}
111
113
} catch ( error : unknown ) {
112
114
this . devServer ?. ssrFixStacktrace ( error as Error )
@@ -203,14 +205,20 @@ export class ReactRenderer implements Renderer {
203
205
return bus
204
206
}
205
207
206
- private renderStreamingTemplate < Props > ( app : JSX . Element , bus : RenderBus , ReactDOMServer : any , render : Render < Props > ) {
207
- this . runHeadHooks ( bus )
208
+ private renderStreamingTemplate < Props > (
209
+ app : JSX . Element ,
210
+ bus : RenderBus ,
211
+ ReactDOMServer : any ,
212
+ render : Render < Props > ,
213
+ hooks : FastifyRendererHook [ ]
214
+ ) {
215
+ this . runHeadHooks ( bus , hooks )
208
216
// There are not postRenderHead hooks for streaming templates
209
217
// so let's end the head stack
210
218
bus . push ( 'head' , null )
211
219
const contentStream = ReactDOMServer . renderToNodeStream ( app )
212
220
contentStream . on ( 'end' , ( ) => {
213
- this . runTailHooks ( bus )
221
+ this . runTailHooks ( bus , hooks )
214
222
} )
215
223
216
224
return render . document ( {
@@ -225,12 +233,13 @@ export class ReactRenderer implements Renderer {
225
233
app : JSX . Element ,
226
234
bus : RenderBus ,
227
235
ReactDOMServer : any ,
228
- render : Render < Props >
236
+ render : Render < Props > ,
237
+ hooks : FastifyRendererHook [ ]
229
238
) {
230
- this . runHeadHooks ( bus )
239
+ this . runHeadHooks ( bus , hooks )
231
240
const content = ReactDOMServer . renderToString ( app )
232
- this . runPostRenderHeadHooks ( bus )
233
- this . runTailHooks ( bus )
241
+ this . runPostRenderHeadHooks ( bus , hooks )
242
+ this . runTailHooks ( bus , hooks )
234
243
235
244
return render . document ( {
236
245
content,
@@ -240,28 +249,28 @@ export class ReactRenderer implements Renderer {
240
249
} )
241
250
}
242
251
243
- private runPostRenderHeadHooks ( bus : RenderBus ) {
252
+ private runPostRenderHeadHooks ( bus : RenderBus , hooks : FastifyRendererHook [ ] ) {
244
253
// Run any heads hooks that might want to push something after the content
245
- for ( const hook of this . plugin . hooks ) {
254
+ for ( const hook of hooks ) {
246
255
if ( hook . postRenderHeads ) {
247
256
bus . push ( 'head' , hook . postRenderHeads ( ) )
248
257
}
249
258
}
250
259
bus . push ( 'head' , null )
251
260
}
252
261
253
- private runHeadHooks ( bus : RenderBus ) {
262
+ private runHeadHooks ( bus : RenderBus , hooks : FastifyRendererHook [ ] ) {
254
263
// Run any heads hooks that might want to push something before the content
255
- for ( const hook of this . plugin . hooks ) {
264
+ for ( const hook of hooks ) {
256
265
if ( hook . heads ) {
257
266
bus . push ( 'head' , hook . heads ( ) )
258
267
}
259
268
}
260
269
}
261
270
262
- private runTailHooks ( bus : RenderBus ) {
271
+ private runTailHooks ( bus : RenderBus , hooks : FastifyRendererHook [ ] ) {
263
272
// when we're done rendering the content, run any hooks that might want to push more stuff after the content
264
- for ( const hook of this . plugin . hooks ) {
273
+ for ( const hook of hooks ) {
265
274
if ( hook . tails ) {
266
275
bus . push ( 'tail' , hook . tails ( ) )
267
276
}
0 commit comments