@@ -228,6 +228,57 @@ describe('renderToChunks', () => {
228228 expect ( result [ result . length - 1 ] ) . toBe ( '</body></html>' ) ;
229229 } ) ;
230230
231+ it ( 'should prepend <!DOCTYPE html> when rendering a full document with suspended content' , async ( ) => {
232+ const { Suspender, suspended } = createSuspender ( ) ;
233+
234+ const result = [ ] ;
235+ const promise = renderToChunks (
236+ < html >
237+ < head >
238+ < title > Test</ title >
239+ </ head >
240+ < body >
241+ < Suspense fallback = "loading..." >
242+ < Suspender />
243+ </ Suspense >
244+ </ body >
245+ </ html > ,
246+ { onWrite : ( s ) => result . push ( s ) }
247+ ) ;
248+ suspended . resolve ( ) ;
249+ await promise ;
250+
251+ // The first chunk must be prefixed with <!DOCTYPE html>
252+ expect ( result [ 0 ] . startsWith ( '<!DOCTYPE html>' ) ) . toBe ( true ) ;
253+
254+ // The full output must start with the doctype
255+ const fullHtml = result . join ( '' ) ;
256+ expect ( fullHtml . startsWith ( '<!DOCTYPE html>' ) ) . toBe ( true ) ;
257+
258+ // The doctype should appear exactly once
259+ const doctypeCount = ( fullHtml . match ( / < ! D O C T Y P E h t m l > / gi) || [ ] ) . length ;
260+ expect ( doctypeCount ) . toBe ( 1 ) ;
261+ } ) ;
262+
263+ it ( 'should not prepend <!DOCTYPE html> when rendering a non-document fragment with suspended content' , async ( ) => {
264+ const { Suspender, suspended } = createSuspender ( ) ;
265+
266+ const result = [ ] ;
267+ const promise = renderToChunks (
268+ < div >
269+ < Suspense fallback = "loading..." >
270+ < Suspender />
271+ </ Suspense >
272+ </ div > ,
273+ { onWrite : ( s ) => result . push ( s ) }
274+ ) ;
275+ suspended . resolve ( ) ;
276+ await promise ;
277+
278+ const fullHtml = result . join ( '' ) ;
279+ expect ( fullHtml . includes ( '<!DOCTYPE html>' ) ) . toBe ( false ) ;
280+ } ) ;
281+
231282 it ( 'should support a component that suspends multiple times' , async ( ) => {
232283 const { Suspender, suspended } = createSuspender ( ) ;
233284 const { Suspender : Suspender2 , suspended : suspended2 } = createSuspender ( ) ;
@@ -254,10 +305,10 @@ describe('renderToChunks', () => {
254305 await promise ;
255306
256307 expect ( result ) . to . deep . equal ( [
257- '<div><!--preact-island:57 -->loading part 1...<!--/preact-island:57 --></div>' ,
308+ '<div><!--preact-island:70 -->loading part 1...<!--/preact-island:70 --></div>' ,
258309 '<div hidden>' ,
259310 createInitScript ( 1 ) ,
260- createSubtree ( '57 ' , '<p>it works</p><p>it works</p>' ) ,
311+ createSubtree ( '70 ' , '<p>it works</p><p>it works</p>' ) ,
261312 '</div>'
262313 ] ) ;
263314 } ) ;
0 commit comments