Skip to content

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented Aug 26, 2025

Description

In old days, we have externalized react on ssr environment, so it's not possible to inject through transform, but now it's possible.

@hi-ogawa hi-ogawa marked this pull request as ready for review August 27, 2025 00:33
Comment on lines +962 to +969
// for build, we cannot use `import` as it confuses rollup commonjs plugin.
return (
(this.environment.mode === 'build' && !isRolldownVite
? `const __viteRscAyncHooks = require("node:async_hooks");`
: `import * as __viteRscAyncHooks from "node:async_hooks";`) +
`globalThis.AsyncLocalStorage = __viteRscAyncHooks.AsyncLocalStorage;` +
code
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sadly, it feels as hacky as before, but probably alright.

Copy link
Contributor

Choose a reason for hiding this comment

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

Hey, maybe you'd like to be warned about a little typo : __viteRscAyncHooks (should probably be __viteRscAsyncHooks)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Aha, good catch. PR welcome :)

@hi-ogawa hi-ogawa merged commit 2f255ad into main Aug 27, 2025
20 checks passed
@hi-ogawa hi-ogawa deleted the 08-27-fix_rsc_inject_asynclocalstorage_global_via_transform branch August 27, 2025 00:47
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.

inject AsyncLocalStorage properly on non runnable environment
2 participants