8000 avoid errors if "track-module-loading" ends up in an edge module · vercel/next.js@406ccfd · GitHub
[go: up one dir, main page]

Skip to content

Commit 406ccfd

Browse files
committed
avoid errors if "track-module-loading" ends up in an edge module
1 parent cc40e9a commit 406ccfd

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

packages/next/src/server/app-render/module-loading/track-module-loading.instance.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import { CacheSignal } from '../cache-signal'
22
import { isThenable } from '../../../shared/lib/is-thenable'
33

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+
}
615

716
export function trackPendingChunkLoad(promise: Promise<unknown>) {
17+
const moduleLoadingSignal = getModuleLoadingSignal()
818
moduleLoadingSignal.trackRead(promise)
919
}
1020

1121
export function trackPendingImport(exportsOrPromise: unknown) {
22+
const moduleLoadingSignal = getModuleLoadingSignal()
23+
1224
// requiring an async module returns a promise.
1325
// if it's sync, there's nothing to track.
1426
if (isThenable(exportsOrPromise)) {
@@ -28,6 +40,8 @@ export function trackPendingImport(exportsOrPromise: unknown) {
2840
* So if we see one, we want to extend the duration of `cacheSignal` at least until the import/chunk-load is done.
2941
*/
3042
export function trackPendingModules(cacheSignal: CacheSignal): void {
43+
const moduleLoadingSignal = getModuleLoadingSignal()
44+
3145
// We can't just use `cacheSignal.trackRead(moduleLoadingSignal.cacheReady())`,
3246
// because we might start and finish multiple batches of module loads while waiting for caches,
3347
// and `moduleLoadingSignal.cacheReady()` would resolve after the first batch.

0 commit comments

Comments
 (0)
0