@@ -188,6 +188,21 @@ function uniqueCompositionId(baseId: string, index: number): string {
188188 return `${ baseId } __hf${ index } ` ;
189189}
190190
191+ const headNodeHost = new WeakMap < Element , Element > ( ) ;
192+
193+ /** Mounts finish in any order; `<head>` keeps their links and styles in host document order. */
194+ function insertIntoHeadInHostOrder ( node : Element , host : Element ) : void {
195+ const next = Array . from ( document . head . children ) . find ( ( element ) => {
196+ const owner = headNodeHost . get ( element ) ;
197+ return (
198+ owner ?. isConnected === true &&
199+ ( host . compareDocumentPosition ( owner ) & Node . DOCUMENT_POSITION_FOLLOWING ) !== 0
200+ ) ;
201+ } ) ;
202+ headNodeHost . set ( node , host ) ;
203+ document . head . insertBefore ( node , next ?? null ) ;
204+ }
205+
191206const waitForExternalScriptLoad = (
192207 scriptEl : HTMLScriptElement ,
193208) : Promise < { status : "load" | "error" | "timeout" ; elapsedMs : number } > =>
@@ -495,7 +510,7 @@ async function mountCompositionContent(params: {
495510 if ( ! isLinkElement ( clonedLink ) ) continue ;
496511 clonedLink . href = href ;
497512 if ( hasSameLink ( document . head , clonedLink ) ) continue ;
498- document . head . appendChild ( clonedLink ) ;
513+ insertIntoHeadInHostOrder ( clonedLink , params . host ) ;
499514 params . injectedLinks . push ( clonedLink ) ;
500515 }
501516
@@ -517,7 +532,7 @@ async function mountCompositionContent(params: {
517532 { scopeRootSelectors : true } ,
518533 ) ;
519534 }
520- document . head . appendChild ( clonedStyle ) ;
535+ insertIntoHeadInHostOrder ( clonedStyle , params . host ) ;
521536 params . injectedStyles . push ( clonedStyle ) ;
522537 const authored = clonedStyle . textContent || "" ;
523538 styles . push ( { element : clonedStyle , authored, applied : authored } ) ;
@@ -725,11 +740,10 @@ async function mountExternalCompositions(
725740 return ! parent || attemptedPaths . has ( parent ) ;
726741 } ) ;
727742
728- type PendingMount = ( ) => Promise < MountedComposition | null > ;
729743 const mounted : Array < MountedComposition | null > = [ ] ;
730744 for ( let offset = 0 ; offset < hosts . length ; offset += 4 ) {
731745 const batch = await Promise . all (
732- hosts . slice ( offset , offset + 4 ) . map ( async ( host ) : Promise < PendingMount | null > => {
746+ hosts . slice ( offset , offset + 4 ) . map ( async ( host ) : Promise < MountedComposition | null > => {
733747 const src = host . getAttribute ( "data-composition-src" ) ;
734748 if ( ! src ) return null ;
735749 const hostIdentity = hostIdentityByElement . get ( host ) ;
@@ -765,25 +779,18 @@ async function mountExternalCompositions(
765779 failed ( new Error ( refusal ) ) ;
766780 return null ;
767781 }
768- const mount =
769- ( mountParams : Parameters < typeof mountCompositionContent > [ 0 ] ) : PendingMount =>
770- async ( ) => {
782+ const mount = async ( mountParams : Parameters < typeof mountCompositionContent > [ 0 ] ) => {
783+ const composition = await mountCompositionContent ( mountParams ) ;
784+ const runScripts = composition . runScripts ;
785+ composition . runScripts = async ( ) => {
771786 try {
772- const composition = await mountCompositionContent ( mountParams ) ;
773- const runScripts = composition . runScripts ;
774- composition . runScripts = async ( ) => {
775- try {
776- await runScripts ( ) ;
777- } catch ( error ) {
778- failed ( error ) ;
779- }
780- } ;
781- return composition ;
787+ await runScripts ( ) ;
782788 } catch ( error ) {
783789 failed ( error ) ;
784- return null ;
785790 }
786791 } ;
792+ return composition ;
793+ } ;
787794 try {
788795 const localTemplate =
789796 authoredCompositionId != null
@@ -792,7 +799,7 @@ async function mountExternalCompositions(
792799 )
793800 : null ;
794801 if ( localTemplate ) {
795- return mount ( {
802+ return await mount ( {
796803 host,
797804 authoredCompositionId,
798805 runtimeCompositionId,
@@ -824,7 +831,7 @@ async function mountExternalCompositions(
824831 )
825832 : null ) ?? doc . querySelector < HTMLTemplateElement > ( "template" ) ;
826833 const sourceNode = template ? template . content : doc . body ;
827- return mount ( {
834+ return await mount ( {
828835 host,
829836 authoredCompositionId,
830837 runtimeCompositionId,
@@ -856,8 +863,7 @@ async function mountExternalCompositions(
856863 }
857864 } ) ,
858865 ) ;
859- // Mount in document order: mounting appends <head> links and styles in cascade order.
860- for ( const pending of batch ) mounted . push ( pending ? await pending ( ) : null ) ;
866+ mounted . push ( ...batch ) ;
861867 }
862868 return mounted . filter ( ( composition ) : composition is MountedComposition => composition !== null ) ;
863869}
0 commit comments