@@ -49,6 +49,8 @@ let base: string | undefined;
4949const doc = isBrowser ? document : undefined ! ;
5050const modulePreloadStr = 'modulepreload' ;
5151const preloadStr = 'preload' ;
52+ const pushStr = 'push' ;
53+ const unshiftStr = 'unshift' ;
5254
5355const makeBundle = ( path : string , imports : string [ ] , dynamicImports : string [ ] ) => {
5456 const url = path . endsWith ( '.js' ) ? new URL ( `${ base } ${ path } ` , doc . baseURI ) . toString ( ) : null ;
@@ -86,11 +88,11 @@ const rel =
8688let highCount = 0 ;
8789let lowCount = 0 ;
8890const trigger = ( ) => {
89- while ( highCount < 4 && high . length ) {
91+ while ( highCount < 6 && high . length ) {
9092 const bundle = high . pop ( ) ! ;
9193 makePreloadLink ( bundle ! , true ) ;
9294 }
93- while ( lowCount < 2 && low . length ) {
95+ while ( lowCount < 3 && low . length ) {
9496 const bundle = low . pop ( ) ! ;
9597 makePreloadLink ( bundle ! ) ;
9698 }
@@ -103,13 +105,11 @@ const makePreloadLink = (bundle: BundleImport, priority?: boolean) => {
103105 bundle . $state$ = BundleImportState . Loading ;
104106 const link = doc . createElement ( 'link' ) ;
105107 link . href = bundle . $url$ ! ;
108+ link . rel = rel ;
106109 if ( priority ) {
107110 highCount ++ ;
108- link . rel = rel ;
109111 } else {
110112 lowCount ++ ;
111- link . rel = preloadStr ;
112- link . fetchPriority = 'low' ;
113113 }
114114 link . as = 'script' ;
115115 link . onload = link . onerror = ( ) => {
@@ -126,16 +126,7 @@ const makePreloadLink = (bundle: BundleImport, priority?: boolean) => {
126126 return 1 ;
127127} ;
128128
129- const preloadBundle = ( bundle : BundleImport , priority : boolean ) => {
130- if ( bundle . $state$ > BundleImportState . Queued ) {
131- return ;
132- }
133- if ( bundle . $url$ ) {
134- ( priority ? high : low ) . unshift ( bundle ) ;
135- }
136- bundle . $state$ = priority ? BundleImportState . Queued : BundleImportState . Low ;
137- } ;
138-
129+ let method = unshiftStr ;
139130/**
140131 * Preload a bundle or bundles. Requires calling loadBundleGraph first.
141132 *
@@ -145,8 +136,11 @@ const preload = (name: string | string[], priority: boolean, seen?: Set<BundleIm
145136 if ( ! isBrowser || ! base ) {
146137 return ;
147138 }
139+ // todo reverse order
148140 if ( Array . isArray ( name ) ) {
141+ method = pushStr ;
149142 name . forEach ( ( n ) => preload ( n , priority ) ) ;
143+ method = unshiftStr ;
150144 return ;
151145 }
152146 let bundle = bundles . get ( name ) ;
@@ -159,7 +153,12 @@ const preload = (name: string | string[], priority: boolean, seen?: Set<BundleIm
159153 return ;
160154 }
161155 seen ?. add ( bundle ) ;
162- preloadBundle ( bundle , priority ) ;
156+ if ( bundle . $state$ < BundleImportState . Queued ) {
157+ if ( bundle . $url$ ) {
158+ ( priority ? high : low ) [ method as 'unshift' | 'push' ] ( bundle ) ;
159+ }
160+ bundle . $state$ = priority ? BundleImportState . Queued : BundleImportState . Low ;
161+ }
163162 for ( const importName of bundle . $imports$ ) {
164163 preload ( importName , priority , seen || new Set ( [ bundle ] ) ) ;
165164 }
0 commit comments