Unverified Commit 86562e12 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[turbopack] Return cached Promise from `__turbopack_load_by_url__ ` (#81663)

parent af74b4a3
......@@ -346,6 +346,13 @@ export async function createHotReloaderTurbopack(
p.startsWith('server/app')
)
// Edge uses the browser runtime which already disposes chunks individually.
// TODO: process.env.NEXT_RUNTIME is 'nodejs' even though Node.js runtime is not used.
if ('__turbopack_clear_chunk_cache__' in globalThis) {
;(globalThis as any).__turbopack_clear_chunk_cache__()
}
// TODO: Stop re-evaluating React Client once it relies on Turbopack's chunk cache.
if (hasAppPaths) {
deleteFromRequireCache(
require.resolve(
......
......@@ -193,35 +193,43 @@ async function loadChunk(
return promise
}
async function loadChunkByUrl(source: SourceInfo, chunkUrl: ChunkUrl) {
try {
await BACKEND.loadChunk(chunkUrl, source)
} catch (error) {
let loadReason
switch (source.type) {
case SourceType.Runtime:
loadReason = `as a runtime dependency of chunk ${source.chunkPath}`
break
case SourceType.Parent:
loadReason = `from module ${source.parentId}`
break
case SourceType.Update:
loadReason = 'from an HMR update'
break
default:
invariant(source, (source) => `Unknown source type: ${source?.type}`)
}
throw new Error(
`Failed to load chunk ${chunkUrl} ${loadReason}${
error ? `: ${error}` : ''
}`,
error
? {
cause: error,
}
: undefined
)
const instrumentedBackendLoadChunks = new WeakMap<Promise<any>, Promise<any>>()
// Do not make this async. React relies on referential equality of the returned Promise.
function loadChunkByUrl(source: SourceInfo, chunkUrl: ChunkUrl): Promise<any> {
const thenable = BACKEND.loadChunk(chunkUrl, source)
let entry = instrumentedBackendLoadChunks.get(thenable)
if (entry === undefined) {
entry = thenable.catch((error) => {
let loadReason
switch (source.type) {
case SourceType.Runtime:
loadReason = `as a runtime dependency of chunk ${source.chunkPath}`
break
case SourceType.Parent:
loadReason = `from module ${source.parentId}`
break
case SourceType.Update:
loadReason = 'from an HMR update'
break
default:
invariant(source, (source) => `Unknown source type: ${source?.type}`)
}
throw new Error(
`Failed to load chunk ${chunkUrl} ${loadReason}${
error ? `: ${error}` : ''
}`,
error
? {
cause: error,
}
: undefined
)
})
// TODO: Free the Promise once it resolves.
instrumentedBackendLoadChunks.set(thenable, entry)
}
return entry
}
async function loadChunkPath(
......
......@@ -93,6 +93,13 @@ function loadChunk(chunkData: ChunkData, source?: SourceInfo): void {
}
const loadedChunks = new Set<ChunkPath>()
const unsupportedLoadChunk = Promise.resolve(undefined)
const loadedChunk = Promise.resolve(undefined)
const chunkCache = new Map<ChunkPath, Promise<any> | null>()
function clearChunkCache() {
chunkCache.clear()
}
function loadChunkPath(chunkPath: ChunkPath, source?: SourceInfo): void {
if (!isJs(chunkPath)) {
......@@ -136,21 +143,10 @@ function loadChunkPath(chunkPath: ChunkPath, source?: SourceInfo): void {
}
}
async function loadChunkAsync(
async function loadChunkAsyncUncached(
source: SourceInfo,
chunkData: ChunkData
): Promise<any> {
const chunkPath = typeof chunkData === 'string' ? chunkData : chunkData.path
if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk.
return
}
if (loadedChunks.has(chunkPath)) {
return
}
chunkPath: ChunkPath
): Promise<void> {
const resolved = path.resolve(RUNTIME_ROOT, chunkPath)
try {
......@@ -187,7 +183,6 @@ async function loadChunkAsync(
}
}
}
loadedChunks.add(chunkPath)
} catch (e) {
let errorMessage = `Failed to load chunk ${chunkPath}`
......@@ -201,7 +196,30 @@ async function loadChunkAsync(
}
}
async function loadChunkAsyncByUrl(source: SourceInfo, chunkUrl: string) {
function loadChunkAsync(
source: SourceInfo,
chunkData: ChunkData
): Promise<any> {
const chunkPath = typeof chunkData === 'string' ? chunkData : chunkData.path
if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk.
return unsupportedLoadChunk
}
let entry = chunkCache.get(chunkPath)
if (entry === undefined) {
const resolve = chunkCache.set.bind(chunkCache, chunkPath, null)
// A new Promise ensures callers that don't handle rejection will still trigger one unhandled rejection.
// Handling the rejection will not trigger unhandled rejections.
entry = loadChunkAsyncUncached(source, chunkPath).then(resolve)
chunkCache.set(chunkPath, entry)
}
// TODO: Return an instrumented Promise that React can use instead of relying on referential equality.
return entry === null ? loadedChunk : entry
}
function loadChunkAsyncByUrl(source: SourceInfo, chunkUrl: string) {
const path = url.fileURLToPath(new URL(chunkUrl, RUNTIME_ROOT)) as ChunkPath
return loadChunkAsync(source, path)
}
......@@ -363,6 +381,9 @@ function isJs(chunkUrlOrPath: ChunkUrl | ChunkPath): boolean {
return regexJsUrl.test(chunkUrlOrPath)
}
// For hot-reloader
;(globalThis as any).__turbopack_clear_chunk_cache__ = clearChunkCache
module.exports = {
getOrInstantiateRuntimeModule,
loadChunk,
......
......@@ -496,6 +496,12 @@ function loadChunk(chunkData, source) {
}
}
const loadedChunks = new Set();
const unsupportedLoadChunk = Promise.resolve(undefined);
const loadedChunk = Promise.resolve(undefined);
const chunkCache = new Map();
function clearChunkCache() {
chunkCache.clear();
}
function loadChunkPath(chunkPath, source) {
if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js.
......@@ -532,16 +538,7 @@ function loadChunkPath(chunkPath, source) {
});
}
}
async function loadChunkAsync(source, chunkData) {
const chunkPath = typeof chunkData === 'string' ? chunkData : chunkData.path;
if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk.
return;
}
if (loadedChunks.has(chunkPath)) {
return;
}
async function loadChunkAsyncUncached(source, chunkPath) {
const resolved = path.resolve(RUNTIME_ROOT, chunkPath);
try {
const contents = await fs.readFile(resolved, 'utf-8');
......@@ -571,7 +568,6 @@ async function loadChunkAsync(source, chunkData) {
}
}
}
loadedChunks.add(chunkPath);
} catch (e) {
let errorMessage = `Failed to load chunk ${chunkPath}`;
if (source) {
......@@ -582,7 +578,25 @@ async function loadChunkAsync(source, chunkData) {
});
}
}
async function loadChunkAsyncByUrl(source, chunkUrl) {
function loadChunkAsync(source, chunkData) {
const chunkPath = typeof chunkData === 'string' ? chunkData : chunkData.path;
if (!isJs(chunkPath)) {
// We only support loading JS chunks in Node.js.
// This branch can be hit when trying to load a CSS chunk.
return unsupportedLoadChunk;
}
let entry = chunkCache.get(chunkPath);
if (entry === undefined) {
const resolve = chunkCache.set.bind(chunkCache, chunkPath, null);
// A new Promise ensures callers that don't handle rejection will still trigger one unhandled rejection.
// Handling the rejection will not trigger unhandled rejections.
entry = loadChunkAsyncUncached(source, chunkPath).then(resolve);
chunkCache.set(chunkPath, entry);
}
// TODO: Return an instrumented Promise that React can use instead of relying on referential equality.
return entry === null ? loadedChunk : entry;
}
function loadChunkAsyncByUrl(source, chunkUrl) {
const path1 = url.fileURLToPath(new URL(chunkUrl, RUNTIME_ROOT));
return loadChunkAsync(source, path1);
}
......@@ -710,6 +724,9 @@ const regexJsUrl = /\.js(?:\?[^#]*)?(?:#.*)?$/;
*/ function isJs(chunkUrlOrPath) {
return regexJsUrl.test(chunkUrlOrPath);
}
// For hot-reloader
;
globalThis.__turbopack_clear_chunk_cache__ = clearChunkCache;
module.exports = {
getOrInstantiateRuntimeModule,
loadChunk
......
......@@ -482,28 +482,35 @@ async function loadChunk(source, chunkData) {
}
return promise;
}
async function loadChunkByUrl(source, chunkUrl) {
try {
await BACKEND.loadChunk(chunkUrl, source);
} catch (error) {
let loadReason;
switch(source.type){
case 0:
loadReason = `as a runtime dependency of chunk ${source.chunkPath}`;
break;
case 1:
loadReason = `from module ${source.parentId}`;
break;
case 2:
loadReason = 'from an HMR update';
break;
default:
invariant(source, (source)=>`Unknown source type: ${source?.type}`);
}
throw new Error(`Failed to load chunk ${chunkUrl} ${loadReason}${error ? `: ${error}` : ''}`, error ? {
cause: error
} : undefined);
const instrumentedBackendLoadChunks = new WeakMap();
// Do not make this async. React relies on referential equality of the returned Promise.
function loadChunkByUrl(source, chunkUrl) {
const thenable = BACKEND.loadChunk(chunkUrl, source);
let entry = instrumentedBackendLoadChunks.get(thenable);
if (entry === undefined) {
entry = thenable.catch((error)=>{
let loadReason;
switch(source.type){
case 0:
loadReason = `as a runtime dependency of chunk ${source.chunkPath}`;
break;
case 1:
loadReason = `from module ${source.parentId}`;
break;
case 2:
loadReason = 'from an HMR update';
break;
default:
invariant(source, (source)=>`Unknown source type: ${source?.type}`);
}
throw new Error(`Failed to load chunk ${chunkUrl} ${loadReason}${error ? `: ${error}` : ''}`, error ? {
cause: error
} : undefined);
});
// TODO: Free the Promise once it resolves.
instrumentedBackendLoadChunks.set(thenable, entry);
}
return entry;
}
async function loadChunkPath(source, chunkPath) {
const url = getChunkRelativeUrl(chunkPath);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment