@@ -6,7 +6,7 @@ import { Minipass } from 'minipass'
66// which enables the combination of React.lazy() and Suspense
77import { renderToPipeableStream } from 'react-dom/server'
88import * as devalue from 'devalue'
9- import Head from 'unihead '
9+ import { transformHtmlTemplate } from '@unhead/react/server '
1010import { createHtmlTemplates } from './templating.js'
1111
1212// Helper function to get an AsyncIterable (via PassThrough)
@@ -46,6 +46,7 @@ export function onAllReady(app) {
4646export async function createRenderFunction ( { routes, create } ) {
4747 // Used when hydrating React Router on the client
4848 const routeMap = Object . fromEntries ( routes . map ( _ => [ _ . path , _ ] ) )
49+
4950 // Registered as reply.render()
5051 return function ( ) {
5152 if ( this . request . route . streaming ) {
@@ -80,6 +81,7 @@ export async function createHtmlFunction (source, _, config) {
8081 return async function ( ) {
8182 const { routes, context, body } = await this . render ( )
8283
84+ context . useHead . push ( context . head )
8385 this . type ( 'text/html' )
8486
8587 // Use template with client module import removed
@@ -116,24 +118,35 @@ export async function createHtmlFunction (source, _, config) {
116118 }
117119}
118120
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+ )
126130}
127131
128132export function streamShell ( templates , context , body ) {
129- context . head = new Head ( context . head ) . render ( )
130133 return Readable . from ( createShellStream ( templates , context , body ) )
131134}
132135
133136async function * createShellStream ( templates , context , body ) {
134- yield templates . beforeElement ( context )
137+ yield await transformHtmlTemplate (
138+ context . useHead ,
139+ templates . beforeElement ( context )
140+ )
141+
135142 for await ( const chunk of body ) {
136- yield chunk
143+ yield await transformHtmlTemplate (
144+ context . useHead ,
145+ chunk . toString ( )
146+ )
137147 }
138- yield templates . afterElement ( context )
148+ yield await transformHtmlTemplate (
149+ context . useHead ,
150+ templates . afterElement ( context )
151+ )
139152}
0 commit comments