@@ -137,7 +137,7 @@ export function createFlushEffectStream(
137137}
138138
139139export function createHeadInjectionTransformStream (
140- inject : string
140+ inject : ( ) => string
141141) : TransformStream < Uint8Array , Uint8Array > {
142142 let injected = false
143143 return new TransformStream ( {
@@ -147,7 +147,7 @@ export function createHeadInjectionTransformStream(
147147 if ( ! injected && ( index = content . indexOf ( '</head' ) ) !== - 1 ) {
148148 injected = true
149149 const injectedContent =
150- content . slice ( 0 , index ) + inject + content . slice ( index )
150+ content . slice ( 0 , index ) + inject ( ) + content . slice ( index )
151151 controller . enqueue ( encodeText ( injectedContent ) )
152152 } else {
153153 controller . enqueue ( chunk )
@@ -175,12 +175,14 @@ export async function continueFromInitialStream(
175175 dataStream,
176176 generateStaticHTML,
177177 flushEffectHandler,
178+ flushEffectsToHead,
178179 initialStylesheets,
179180 } : {
180181 suffix ?: string
181182 dataStream ?: ReadableStream < Uint8Array >
182183 generateStaticHTML : boolean
183184 flushEffectHandler ?: ( ) => string
185+ flushEffectsToHead : boolean
184186 initialStylesheets ?: string [ ]
185187 }
186188) : Promise < ReadableStream < Uint8Array > > {
@@ -193,15 +195,22 @@ export async function continueFromInitialStream(
193195
194196 const transforms : Array < TransformStream < Uint8Array , Uint8Array > > = [
195197 createBufferedTransformStream ( ) ,
196- flushEffectHandler ? createFlushEffectStream ( flushEffectHandler ) : null ,
198+ flushEffectHandler && ! flushEffectsToHead
199+ ? createFlushEffectStream ( flushEffectHandler )
200+ : null ,
197201 suffixUnclosed != null ? createDeferredSuffixStream ( suffixUnclosed ) : null ,
198202 dataStream ? createInlineDataStream ( dataStream ) : null ,
199203 suffixUnclosed != null ? createSuffixStream ( closeTag ) : null ,
200- createHeadInjectionTransformStream (
201- ( initialStylesheets || [ ] )
204+ createHeadInjectionTransformStream ( ( ) => {
205+ const inlineStyleLinks = ( initialStylesheets || [ ] )
202206 . map ( ( href ) => `<link rel="stylesheet" href="/_next/${ href } ">` )
203207 . join ( '' )
204- ) ,
208+ // TODO-APP: Inject flush effects to end of head in app layout rendering, to avoid
209+ // hydration errors. Remove this once it's ready to be handled by react itself.
210+ const flushEffectsContent =
211+ flushEffectHandler && flushEffectsToHead ? flushEffectHandler ( ) : ''
212+ return inlineStyleLinks + flushEffectsContent
213+ } ) ,
205214 ] . filter ( nonNullable )
206215
207216 return transforms . reduce (
0 commit comments