Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions packages/plugin-rsc/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -946,27 +948,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
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 :)

}
},
},
},
...vitePluginRscMinimal(rscPluginOptions, manager),
Expand Down
Loading