1
1
import { CacheSignal } from '../cache-signal'
2
2
import { isThenable } from '../../../shared/lib/is-thenable'
3
3
4
- /** Tracks all in-flight async imports and chunk loads. */
5
- const moduleLoadingSignal = new CacheSignal ( )
4
+ /**
5
+ * Tracks all in-flight async imports and chunk loads.
6
+ * Initialized lazily, because we don't want this to error in case it gets pulled into an edge runtime module.
7
+ */
8
+ let _moduleLoadingSignal : CacheSignal | null
9
+ function getModuleLoadingSignal ( ) {
10
+ if ( ! _moduleLoadingSignal ) {
11
+ _moduleLoadingSignal = new CacheSignal ( )
12
+ }
13
+ return _moduleLoadingSignal
14
+ }
6
15
7
16
export function trackPendingChunkLoad ( promise : Promise < unknown > ) {
17
+ const moduleLoadingSignal = getModuleLoadingSignal ( )
8
18
moduleLoadingSignal . trackRead ( promise )
9
19
}
10
20
11
21
export function trackPendingImport ( exportsOrPromise : unknown ) {
22
+ const moduleLoadingSignal = getModuleLoadingSignal ( )
23
+
12
24
// requiring an async module returns a promise.
13
25
// if it's sync, there's nothing to track.
14
26
if ( isThenable ( exportsOrPromise ) ) {
@@ -28,6 +40,8 @@ export function trackPendingImport(exportsOrPromise: unknown) {
28
40
* So if we see one, we want to extend the duration of `cacheSignal` at least until the import/chunk-load is done.
29
41
*/
30
42
export function trackPendingModules ( cacheSignal : CacheSignal ) : void {
43
+ const moduleLoadingSignal = getModuleLoadingSignal ( )
44
+
31
45
// We can't just use `cacheSignal.trackRead(moduleLoadingSignal.cacheReady())`,
32
46
// because we might start and finish multiple batches of module loads while waiting for caches,
33
47
// and `moduleLoadingSignal.cacheReady()` would resolve after the first batch.
0 commit comments