Skip to content

[Flight] Assume __turbopack_load_by_url__ returns a cached Promise #33792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

eps1lon
Copy link
Collaborator

@eps1lon eps1lon commented Jul 15, 2025

React doesn't have a signal it can hook into to dispose of chunk cache. Bundlers used to re-evaluate the React Client module to get a fresh cache. However, this is problematic if React Client is in the same bundle (but different module layer) as React Server.

Re-evaluating React Server is not safe since it contains side-effects (console patching and async_hooks).

We can drop the chunk cache in react-server-dom-turbopack by assuming Turbopack returns a cached Promise from __turbopack_load_by_url__ which is implemented in vercel/next.js#81663.

As a follow-up, Turbopack will return already instrumented Promises (setting status).

@meta-cla meta-cla bot added the CLA Signed label Jul 15, 2025
@github-actions github-actions bot added the React Core Team Opened by a member of the React Core Team label Jul 15, 2025
Comment on lines 165 to 166
const instrumentedChunks: WeakSet<string> = new WeakSet();
const loadedChunks: WeakSet<string> = new WeakSet();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question is we still need this or if we're ok with Promise.all([Promise.resolve(undefined)]) instead of Promise.all([null]). Or we change Turbopack to return null if the chunk is already loaded.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It avoids potentially a lot of unnecessary overhead when we end up blocking a large number of chunks downstream for just a microtask. Especially on the server where all of these are either fully sync for basically every request or every request will have to go through some of this.

However, if we trust that Turbopack returns cached promises, then we can also instrument those promises with our .status/.value convention, or even better, have Turbopack yield already instrumented Promises.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I misread the code originally. It's not Promise.all([null]) if the chunk is already loaded. For sync chunks, preloadModule becomes fully sync. I only accounted for that later in https://github.com/facebook/react/compare/e6ed827a0a52671417a7e34a1707bfda1a209c44..6a4296437e7eb12b18e7b5ee10d213a03cc1b6a1

@react-sizebot
Copy link

react-sizebot commented Jul 15, 2025

The size diff is too large to display in a single comment. The GitHub action for this pull request contains an artifact called 'sizebot-message.md' with the full message.

Generated by 🚫 dangerJS against 690f10d

@eps1lon eps1lon force-pushed the sebbie/flight-client-turbopack-assume-cached-load-async-chunk branch from e6ed827 to 6a42964 Compare July 15, 2025 15:16
@eps1lon eps1lon changed the title [Flight] Assume __turbopack_load__ returns a cached Promise [Flight] Assume __turbopack_load_by_url__ returns a cached Promise Jul 15, 2025
@eps1lon eps1lon force-pushed the sebbie/flight-client-turbopack-assume-cached-load-async-chunk branch from 6a42964 to 690f10d Compare July 15, 2025 17:35
@eps1lon eps1lon marked this pull request as ready for review July 15, 2025 18:16
@eps1lon eps1lon requested a review from sebmarkbage July 15, 2025 18:17
@eps1lon eps1lon merged commit d85ec5f into facebook:main Jul 16, 2025
244 of 245 checks passed
@eps1lon eps1lon deleted the sebbie/flight-client-turbopack-assume-cached-load-async-chunk branch July 16, 2025 11:20
github-actions bot pushed a commit to code/lib-react that referenced this pull request Jul 20, 2025
github-actions bot pushed a commit to code/lib-react that referenced this pull request Jul 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants