@@ -82,8 +82,8 @@ export const trigger = () => {
82
82
Math . max ( 1 , config . $maxIdlePreloads$ * probability )
83
83
: // While the graph is not available, we limit to 2 preloads
84
84
2 ;
85
- // When we're 100 % sure, everything needs to be queued
86
- if ( probability === 1 || preloadCount < allowedPreloads ) {
85
+ // When we're 99 % sure, everything needs to be queued
86
+ if ( probability >= 0.99 || preloadCount < allowedPreloads ) {
87
87
queue . shift ( ) ;
88
88
preloadOne ( bundle ) ;
89
89
} else {
@@ -142,17 +142,27 @@ const preloadOne = (bundle: BundleImport) => {
142
142
doc . head . appendChild ( link ) ;
143
143
} ;
144
144
145
+ /**
146
+ * Adjust the probability of a bundle based on the probability of its dependent bundles, and queue
147
+ * it if it's likely enough to be preloaded.
148
+ *
149
+ * Note that if the probability is 100%, we treat the dynamic imports as 99% sure, and both will be
150
+ * preloaded without limit.
151
+ *
152
+ * We also limit "organic" probability to 98% so they don't get unlimited preloads.
153
+ */
145
154
export const adjustProbabilities = (
146
155
bundle : BundleImport ,
147
- adjustFactor : number ,
156
+ newInverseProbability : number ,
148
157
seen ?: Set < BundleImport >
149
158
) => {
150
159
if ( seen ?. has ( bundle ) ) {
151
160
return ;
152
161
}
153
162
154
163
const previousInverseProbability = bundle . $inverseProbability$ ;
155
- bundle . $inverseProbability$ *= adjustFactor ;
164
+ bundle . $inverseProbability$ = newInverseProbability ;
165
+ // Don't propagate tiny changes
156
166
if ( previousInverseProbability - bundle . $inverseProbability$ < 0.01 ) {
157
167
return ;
158
168
}
@@ -178,6 +188,10 @@ export const adjustProbabilities = (
178
188
const probability = 1 - bundle . $inverseProbability$ ;
179
189
for ( const dep of bundle . $deps$ ) {
180
190
const depBundle = getBundle ( dep . $name$ ) ! ;
191
+ if ( depBundle . $inverseProbability$ === 0 ) {
192
+ // it's already at max probability
193
+ continue ;
194
+ }
181
195
const prevAdjust = dep . $factor$ ;
182
196
/**
183
197
* The chance that a dep won't be loaded is 1-(the chance that the dep will be loaded)*(the
@@ -189,14 +203,20 @@ export const adjustProbabilities = (
189
203
* But when we're very likely to load the current bundle, make the dynamic imports very likely
190
204
* too.
191
205
*/
192
- const newInverseProbability =
193
- dep . $probability$ !== 1 && adjustFactor < 0.1 ? 0.01 : 1 - dep . $probability$ * probability ;
206
+ let newInverseProbability : number ;
207
+ if ( probability >= 0.99 ) {
208
+ // we're loaded at max probability, so elevate dynamic imports to 99% sure
209
+ newInverseProbability = Math . min ( 0.01 , 1 - dep . $importProbability$ ) ;
210
+ } else {
211
+ const newInverseImportProbability = 1 - dep . $importProbability$ * probability ;
212
+ const factor = newInverseImportProbability / prevAdjust ;
213
+ // limit organic probability to 98%
214
+ newInverseProbability = Math . max ( 0.02 , depBundle . $inverseProbability$ * factor ) ;
215
+ dep . $factor$ = factor ;
216
+ }
194
217
195
218
/** We need to undo the previous adjustment */
196
- const factor = newInverseProbability / prevAdjust ;
197
- dep . $factor$ = factor ;
198
-
199
- adjustProbabilities ( depBundle , factor , seen ) ;
219
+ adjustProbabilities ( depBundle , newInverseProbability , seen ) ;
200
220
}
201
221
}
202
222
} ;
0 commit comments