-
-
Notifications
You must be signed in to change notification settings - Fork 189
fix(rsc): inject AsyncLocalStorage
global via transform
#785
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
Changes from 5 commits
d8e8a7f
3ce936d
c961f9a
edc2649
f66b3c9
cce8209
d3dbe78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,8 @@ import { validateImportPlugin } from './plugins/validate-import' | |
import { vitePluginFindSourceMapURL } from './plugins/find-source-map-url' | ||
import { parseCssVirtual, toCssVirtual, parseIdQuery } from './plugins/shared' | ||
|
||
const isRolldownVite = 'rolldownVersion' in vite | ||
|
||
const BUILD_ASSETS_MANIFEST_NAME = '__vite_rsc_assets_manifest.js' | ||
|
||
type ClientReferenceMeta = { | ||
|
@@ -945,27 +947,28 @@ import.meta.hot.on("rsc:update", () => { | |
}, | ||
), | ||
{ | ||
// make `AsyncLocalStorage` available globally for React request context on edge build (e.g. React.cache, ssr preload) | ||
// make `AsyncLocalStorage` available globally for React edge build (required for React.cache, ssr preload, etc.) | ||
// https://github.com/facebook/react/blob/f14d7f0d2597ea25da12bcf97772e8803f2a394c/packages/react-server/src/forks/ReactFlightServerConfig.dom-edge.js#L16-L19 | ||
name: 'rsc:inject-async-local-storage', | ||
async configureServer() { | ||
const __viteRscAyncHooks = await import('node:async_hooks') | ||
;(globalThis as any).AsyncLocalStorage = | ||
__viteRscAyncHooks.AsyncLocalStorage | ||
}, | ||
banner(chunk) { | ||
if ( | ||
(this.environment.name === 'ssr' || | ||
this.environment.name === 'rsc') && | ||
this.environment.mode === 'build' && | ||
chunk.isEntry | ||
) { | ||
return `\ | ||
import * as __viteRscAyncHooks from "node:async_hooks"; | ||
globalThis.AsyncLocalStorage = __viteRscAyncHooks.AsyncLocalStorage; | ||
` | ||
} | ||
return '' | ||
transform: { | ||
handler(code) { | ||
if ( | ||
(this.environment.name === 'ssr' || | ||
this.environment.name === 'rsc') && | ||
code.includes('typeof AsyncLocalStorage') && | ||
code.includes('new AsyncLocalStorage()') && | ||
!code.includes('__viteRscAyncHooks') | ||
) { | ||
// 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 | ||
) | ||
Comment on lines
+963
to
+970
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly, it feels as hacky as before, but probably alright. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Aha, good catch. PR welcome :) |
||
} | ||
}, | ||
}, | ||
}, | ||
...vitePluginRscMinimal(rscPluginOptions, manager), | ||
|
Uh oh!
There was an error while loading. Please reload this page.