From 7249b8b8976bdb247a5b9ac217bd0b7aad3c95fd Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 20 Feb 2025 17:19:39 +0900 Subject: [PATCH 1/2] fix: fix async local storage global import side effect --- .../react-server/src/features/request-context/utils.ts | 2 ++ packages/react-server/src/plugin/index.ts | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/react-server/src/features/request-context/utils.ts b/packages/react-server/src/features/request-context/utils.ts index ff0e84ba9..49f152a54 100644 --- a/packages/react-server/src/features/request-context/utils.ts +++ b/packages/react-server/src/features/request-context/utils.ts @@ -1,3 +1,5 @@ +import "virtual:inject-async-local-storage"; + // we don't require async hooks and fallbacks to sync context // (see virtual:inject-async-local-storage) diff --git a/packages/react-server/src/plugin/index.ts b/packages/react-server/src/plugin/index.ts index 6d7ade0ca..5af87e4d9 100644 --- a/packages/react-server/src/plugin/index.ts +++ b/packages/react-server/src/plugin/index.ts @@ -365,6 +365,9 @@ export function vitePluginReactServer( }), createVirtualPlugin( ENTRY_SERVER_WRAPPER.slice("virtual:".length), + // the first import doesn't necessarily guarantee the execution order depending on chunk. + // so, we also have it on packages/react-server/src/features/request-context/utils.ts. + // TODO: this might not be enough for React.cache () => ` import "virtual:inject-async-local-storage"; export { handler } from "${entryServer}"; @@ -373,8 +376,8 @@ export function vitePluginReactServer( ), // make `AsyncLocalStorage` available globally for React.cache from edge build // https://github.com/facebook/react/blob/f14d7f0d2597ea25da12bcf97772e8803f2a394c/packages/react-server/src/forks/ReactFlightServerConfig.dom-edge.js#L16-L19 - createVirtualPlugin("inject-async-local-storage", () => { - if (options?.noAsyncLocalStorage) { + createVirtualPlugin("inject-async-local-storage", function () { + if (this.environment.name !== "rsc" || options?.noAsyncLocalStorage) { return "export {}"; } return ` From a29f46340f76733489a7834b1cdaa1e121bc7df3 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 20 Feb 2025 17:20:59 +0900 Subject: [PATCH 2/2] chore: comment --- packages/react-server/src/plugin/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/react-server/src/plugin/index.ts b/packages/react-server/src/plugin/index.ts index 5af87e4d9..17013ac85 100644 --- a/packages/react-server/src/plugin/index.ts +++ b/packages/react-server/src/plugin/index.ts @@ -365,7 +365,7 @@ export function vitePluginReactServer( }), createVirtualPlugin( ENTRY_SERVER_WRAPPER.slice("virtual:".length), - // the first import doesn't necessarily guarantee the execution order depending on chunk. + // the first import doesn't necessarily guarantee the execution order depending on chunking. // so, we also have it on packages/react-server/src/features/request-context/utils.ts. // TODO: this might not be enough for React.cache () => `