Skip to content

Comments

Avoid proxying React modules through workUnitStore (#85486)#8

Open
MitchLewis930 wants to merge 1 commit intopr_038_beforefrom
pr_038_after
Open

Avoid proxying React modules through workUnitStore (#85486)#8
MitchLewis930 wants to merge 1 commit intopr_038_beforefrom
pr_038_after

Conversation

@MitchLewis930
Copy link

PR_038

Today the `captureOwnerStack()` function is provided to shared utilities
through an AsyncLocalStorage that scopes the method from the appropriate
React instance. This is so that external code like patches to sync IO
methods can still generate errors with the appropriate React owner
information even when the patched code itself is not bundled and can be
called from etiher SSR or RSC contexts.

This works but it makes plumbing the React instances around tricky.
There is a simpler way. Most of the time you can just try both React's.
If one gives you a non-null/undefined result then you know you are in
that scope. If neither do then you're outside a React scope altogether.

In this change I remove `captureOwnerStack()` from the workUnitStore
types and just call it from the shared server runtime which gives even
external code access to the appropriate React instances for bundled code
@MitchLewis930 MitchLewis930 requested a review from Copilot January 31, 2026 01:06
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors how React module instances are accessed in external modules by introducing a centralized registration system instead of proxying through the workUnitStore.

Changes:

  • Introduced runtime-reacts.external.ts to provide centralized getter/setter functions for client and server React instances
  • Removed captureOwnerStack and cacheSignal from workUnitStore, accessing them directly through the registered React instances instead
  • Updated registration logic in app-page/module.ts to use the new centralized system

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/next/src/server/runtime-reacts.external.ts New file providing registration and getter functions for client/server React instances
packages/next/src/server/route-modules/app-page/module.ts Updated to register React instances using the new centralized system
packages/next/src/server/node-environment-extensions/console-dim.external.tsx Refactored to access React's cacheSignal through centralized getters instead of registered callbacks
packages/next/src/server/node-environment-extensions/utils.tsx Updated applyOwnerStack to access captureOwnerStack through centralized React getters
packages/next/src/server/app-render/work-unit-async-storage.external.ts Removed captureOwnerStack property from PrerenderStoreModernCommon interface
packages/next/src/server/app-render/app-render.tsx Removed captureOwnerStack assignments from work unit store initializations
packages/next/src/server/route-modules/app-route/module.ts Removed captureOwnerStack assignments from work unit store initializations
packages/next/src/server/node-environment-extensions/console-dim.external.test.ts Updated test to use new registration API with mock React instances
test/production/next-server-nft/next-server-nft.test.ts Added new runtime-reacts.external.js file to expected NFT output

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +189 to +190
const signal =
getClientReact()?.cacheSignal() ?? getServerReact()?.cacheSignal()
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

The logic change removes the loop that checked multiple signals, but doesn't handle the case where both getClientReact() and getServerReact() return non-null React instances. Previously, the code would iterate through all registered signals until finding one. Now it will only use the first non-null result. Consider whether both React instances could be active simultaneously and whether the fallback logic is correct.

Suggested change
const signal =
getClientReact()?.cacheSignal() ?? getServerReact()?.cacheSignal()
const clientReact = getClientReact()
const serverReact = getServerReact()
const clientSignal = clientReact?.cacheSignal?.()
const serverSignal = serverReact?.cacheSignal?.()
let signal =
clientSignal !== null && clientSignal !== undefined
? clientSignal
: serverSignal !== null && serverSignal !== undefined
? serverSignal
: null
if (
clientSignal &&
serverSignal &&
clientSignal.aborted !== serverSignal.aborted
) {
// If both client and server signals are present but disagree on `aborted`,
// prefer the aborted one so that logs are dimmed/hidden conservatively.
signal = clientSignal.aborted ? clientSignal : serverSignal
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants