@@ -191,6 +191,43 @@ describe('renderToChunks', () => {
191191 ] ) ;
192192 } ) ;
193193
194+ it ( 'should inject deferred content before </body></html> for full document rendering' , async ( ) => {
195+ const { Suspender, suspended } = createSuspender ( ) ;
196+
197+ const result = [ ] ;
198+ const promise = renderToChunks (
199+ < html >
200+ < head >
201+ < title > Test</ title >
202+ </ head >
203+ < body >
204+ < Suspense fallback = "loading..." >
205+ < Suspender />
206+ </ Suspense >
207+ </ body >
208+ </ html > ,
209+ { onWrite : ( s ) => result . push ( s ) }
210+ ) ;
211+ suspended . resolve ( ) ;
212+ await promise ;
213+
214+ const fullHtml = result . join ( '' ) ;
215+
216+ // Deferred wrapper must appear before </body></html>, not after
217+ const deferredPos = fullHtml . indexOf ( '<div hidden>' ) ;
218+ const bodyClosePos = fullHtml . indexOf ( '</body>' ) ;
219+ const htmlClosePos = fullHtml . indexOf ( '</html>' ) ;
220+
221+ expect ( deferredPos ) . toBeGreaterThan ( - 1 ) ;
222+ expect ( deferredPos ) . toBeLessThan ( bodyClosePos ) ;
223+ expect ( bodyClosePos ) . toBeLessThan ( htmlClosePos ) ;
224+
225+ // The document must end with </html>
226+ expect ( fullHtml . endsWith ( '</html>' ) ) . toBe ( true ) ;
227+ // No content after </html>
228+ expect ( result [ result . length - 1 ] ) . toBe ( '</body></html>' ) ;
229+ } ) ;
230+
194231 it ( 'should support a component that suspends multiple times' , async ( ) => {
195232 const { Suspender, suspended } = createSuspender ( ) ;
196233 const { Suspender : Suspender2 , suspended : suspended2 } = createSuspender ( ) ;
@@ -217,10 +254,10 @@ describe('renderToChunks', () => {
217254 await promise ;
218255
219256 expect ( result ) . to . deep . equal ( [
220- '<div><!--preact-island:49 -->loading part 1...<!--/preact-island:49 --></div>' ,
257+ '<div><!--preact-island:57 -->loading part 1...<!--/preact-island:57 --></div>' ,
221258 '<div hidden>' ,
222259 createInitScript ( 1 ) ,
223- createSubtree ( '49 ' , '<p>it works</p><p>it works</p>' ) ,
260+ createSubtree ( '57 ' , '<p>it works</p><p>it works</p>' ) ,
224261 '</div>'
225262 ] ) ;
226263 } ) ;
0 commit comments