@@ -6,7 +6,7 @@ import { Minipass } from 'minipass'
6
6
// which enables the combination of React.lazy() and Suspense
7
7
import { renderToPipeableStream } from 'react-dom/server'
8
8
import * as devalue from 'devalue'
9
- import Head from 'unihead '
9
+ import { transformHtmlTemplate } from '@unhead/react/server '
10
10
import { createHtmlTemplates } from './templating.js'
11
11
12
12
// Helper function to get an AsyncIterable (via PassThrough)
@@ -46,6 +46,7 @@ export function onAllReady(app) {
46
46
export async function createRenderFunction ( { routes, create } ) {
47
47
// Used when hydrating React Router on the client
48
48
const routeMap = Object . fromEntries ( routes . map ( _ => [ _ . path , _ ] ) )
49
+
49
50
// Registered as reply.render()
50
51
return function ( ) {
51
52
if ( this . request . route . streaming ) {
@@ -80,6 +81,7 @@ export async function createHtmlFunction (source, _, config) {
80
81
return async function ( ) {
81
82
const { routes, context, body } = await this . render ( )
82
83
84
+ context . useHead . push ( context . head )
83
85
this . type ( 'text/html' )
84
86
85
87
// Use template with client module import removed
@@ -116,24 +118,35 @@ export async function createHtmlFunction (source, _, config) {
116
118
}
117
119
}
118
120
119
- export function sendClientOnlyShell ( templates , context , body ) {
120
- context . head = new Head ( context . head ) . render ( )
121
- return `${
122
- templates . beforeElement ( context )
123
- } ${
124
- templates . afterElement ( context )
125
- } `
121
+ export async function sendClientOnlyShell ( templates , context ) {
122
+ return await transformHtmlTemplate (
123
+ context . useHead ,
124
+ `${
125
+ templates . beforeElement ( context )
126
+ } ${
127
+ templates . afterElement ( context )
128
+ } `
129
+ )
126
130
}
127
131
128
132
export function streamShell ( templates , context , body ) {
129
- context . head = new Head ( context . head ) . render ( )
130
133
return Readable . from ( createShellStream ( templates , context , body ) )
131
134
}
132
135
133
136
async function * createShellStream ( templates , context , body ) {
134
- yield templates . beforeElement ( context )
137
+ yield await transformHtmlTemplate (
138
+ context . useHead ,
139
+ templates . beforeElement ( context )
140
+ )
141
+
135
142
for await ( const chunk of body ) {
136
- yield chunk
143
+ yield await transformHtmlTemplate (
144
+ context . useHead ,
145
+ chunk . toString ( )
146
+ )
137
147
}
138
- yield templates . afterElement ( context )
148
+ yield await transformHtmlTemplate (
149
+ context . useHead ,
150
+ templates . afterElement ( context )
151
+ )
139
152
}
0 commit comments