Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
f3beddf1
Unverified
Commit
f3beddf1
authored
Jul 18, 2025
by
Sebastian "Sebbie" Silbermann
Committed by
GitHub
Jul 18, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[turbopack] Reuse shared Promise for loaded chunks (#81755)
parent
b0280ebb
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
35 additions
and
23 deletions
+35
-23
dev-base.ts
...cmascript-runtime/js/src/browser/runtime/base/dev-base.ts
+1
-1
runtime-base.ts
...cript-runtime/js/src/browser/runtime/base/runtime-base.ts
+16
-5
runtime-backend-dom.ts
...runtime/js/src/browser/runtime/dom/runtime-backend-dom.ts
+1
-1
runtime-backend-edge.ts
...ntime/js/src/browser/runtime/edge/runtime-backend-edge.ts
+1
-1
runtime.ts
...tes/turbopack-ecmascript-runtime/js/src/nodejs/runtime.ts
+3
-3
[turbopack]_runtime.js
...ntime/default_build_runtime/output/[turbopack]_runtime.js
+2
-2
[turbopack]_runtime.js.map
...e/default_build_runtime/output/[turbopack]_runtime.js.map
+1
-1
a5fc1_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_73aab4ae.js
...pshot_runtime_default_dev_runtime_input_index_73aab4ae.js
+6
-5
b1abf_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_73aab4ae.js.map
...t_runtime_default_dev_runtime_input_index_73aab4ae.js.map
+4
-4
No files found.
turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/base/dev-base.ts
View file @
f3beddf1
...
...
@@ -607,7 +607,7 @@ function applyChunkListUpdate(update: ChunkListUpdate) {
switch
(
chunkUpdate
.
type
)
{
case
'added'
:
BACKEND
.
loadChunk
(
chunkUrl
,
{
type
:
SourceType
.
Update
})
BACKEND
.
loadChunk
Cached
(
chunkUrl
,
{
type
:
SourceType
.
Update
})
break
case
'total'
:
DEV_BACKEND
.
reloadChunk
?.(
chunkUrl
)
...
...
turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/base/runtime-base.ts
View file @
f3beddf1
...
...
@@ -74,7 +74,10 @@ type SourceInfo =
interface
RuntimeBackend
{
registerChunk
:
(
chunkPath
:
ChunkPath
,
params
?:
RuntimeParams
)
=>
void
loadChunk
:
(
chunkUrl
:
ChunkUrl
,
source
:
SourceInfo
)
=>
Promise
<
void
>
/**
* Returns the same Promise for the same chunk URL.
*/
loadChunkCached
:
(
chunkUrl
:
ChunkUrl
,
source
:
SourceInfo
)
=>
Promise
<
void
>
}
interface
DevRuntimeBackend
{
...
...
@@ -193,13 +196,22 @@ async function loadChunk(
return
promise
}
const
instrumentedBackendLoadChunks
=
new
WeakMap
<
Promise
<
any
>
,
Promise
<
any
>>
()
const
loadedChunk
=
Promise
.
resolve
(
undefined
)
const
instrumentedBackendLoadChunks
=
new
WeakMap
<
Promise
<
any
>
,
Promise
<
any
>
|
typeof
loadedChunk
>
()
// 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
)
const
thenable
=
BACKEND
.
loadChunk
Cached
(
chunkUrl
,
source
)
let
entry
=
instrumentedBackendLoadChunks
.
get
(
thenable
)
if
(
entry
===
undefined
)
{
entry
=
thenable
.
catch
((
error
)
=>
{
const
resolve
=
instrumentedBackendLoadChunks
.
set
.
bind
(
instrumentedBackendLoadChunks
,
thenable
,
loadedChunk
)
entry
=
thenable
.
then
(
resolve
).
catch
((
error
)
=>
{
let
loadReason
switch
(
source
.
type
)
{
case
SourceType
.
Runtime
:
...
...
@@ -225,7 +237,6 @@ function loadChunkByUrl(source: SourceInfo, chunkUrl: ChunkUrl): Promise<any> {
: undefined
)
})
// TODO: Free the Promise once it resolves.
instrumentedBackendLoadChunks.set(thenable, entry)
}
...
...
turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/dom/runtime-backend-dom.ts
View file @
f3beddf1
...
...
@@ -94,7 +94,7 @@ const chunkResolvers: Map<ChunkUrl, ChunkResolver> = new Map()
* Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded.
*/
loadChunk
(
chunkUrl
,
source
)
{
loadChunk
Cached
(
chunkUrl
,
source
)
{
return
doLoadChunk
(
chunkUrl
,
source
)
},
}
...
...
turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/edge/runtime-backend-edge.ts
View file @
f3beddf1
...
...
@@ -117,7 +117,7 @@ async function loadWebAssemblyModule(
}
},
loadChunk
(
_chunkUrl
,
_source
)
{
loadChunk
Cached
(
_chunkUrl
,
_source
)
{
throw
new
Error
(
'chunk loading is not supported'
)
},
}
...
...
turbopack/crates/turbopack-ecmascript-runtime/js/src/nodejs/runtime.ts
View file @
f3beddf1
...
...
@@ -95,7 +95,7 @@ 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
>()
const chunkCache = new Map<ChunkPath, Promise<any> |
typeof loadedChunk
>()
function clearChunkCache() {
chunkCache.clear()
...
...
@@ -209,14 +209,14 @@ function loadChunkAsync(
let entry = chunkCache.get(chunkPath)
if (entry === undefined) {
const resolve = chunkCache.set.bind(chunkCache, chunkPath,
null
)
const resolve = chunkCache.set.bind(chunkCache, chunkPath,
loadedChunk
)
// 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
return entry
}
function loadChunkAsyncByUrl(source: SourceInfo, chunkUrl: string) {
...
...
turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_build_runtime/output/[turbopack]_runtime.js
View file @
f3beddf1
...
...
@@ -587,14 +587,14 @@ function loadChunkAsync(source, chunkData) {
}
let entry = chunkCache.get(chunkPath);
if (entry === undefined) {
const resolve = chunkCache.set.bind(chunkCache, chunkPath,
null
);
const resolve = chunkCache.set.bind(chunkCache, chunkPath,
loadedChunk
);
// 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
;
return entry;
}
function loadChunkAsyncByUrl(source, chunkUrl) {
const path1 = url.fileURLToPath(new URL(chunkUrl, RUNTIME_ROOT));
...
...
turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_build_runtime/output/[turbopack]_runtime.js.map
View file @
f3beddf1
This diff is collapsed.
Click to expand it.
turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/a5fc1_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_73aab4ae.js
View file @
f3beddf1
...
...
@@ -482,13 +482,15 @@ async function loadChunk(source, chunkData) {
}
return
promise
;
}
const
loadedChunk
=
Promise
.
resolve
(
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
);
const
thenable
=
BACKEND
.
loadChunk
Cached
(
chunkUrl
,
source
);
let
entry
=
instrumentedBackendLoadChunks
.
get
(
thenable
);
if
(
entry
===
undefined
)
{
entry
=
thenable
.
catch
((
error
)
=>
{
const
resolve
=
instrumentedBackendLoadChunks
.
set
.
bind
(
instrumentedBackendLoadChunks
,
thenable
,
loadedChunk
);
entry
=
thenable
.
then
(
resolve
).
catch
((
error
)
=>
{
let
loadReason
;
switch
(
source
.
type
){
case
0
:
...
...
@@ -507,7 +509,6 @@ function loadChunkByUrl(source, chunkUrl) {
cause: error
} : undefined);
});
// TODO: Free the Promise once it resolves.
instrumentedBackendLoadChunks.set(thenable, entry);
}
return entry;
...
...
@@ -1089,7 +1090,7 @@ function applyChunkListUpdate(update) {
const
chunkUrl
=
getChunkRelativeUrl
(
chunkPath
);
switch
(
chunkUpdate
.
type
){
case
'added'
:
BACKEND
.
loadChunk
(
chunkUrl
,
{
BACKEND
.
loadChunk
Cached
(
chunkUrl
,
{
type
:
SourceType
.
Update
});
break
;
...
...
@@ -1530,7 +1531,7 @@ async function loadWebAssemblyModule(_source, wasmChunkPath, _edgeModule) {
/**
* Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded.
*/
loadChunk
(
chunkUrl
,
source
)
{
*/
loadChunk
Cached
(
chunkUrl
,
source
)
{
return
doLoadChunk
(
chunkUrl
,
source
);
}
};
...
...
turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/b1abf_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_73aab4ae.js.map
View file @
f3beddf1
This source diff could not be displayed because it is too large. You can
view the blob
instead.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment