feat: lexically scoped React Server Components#355
Merged
Conversation
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ✅ Deployment successful! View logs |
react-server-docs | 30dd0d6 | Mar 11 2026, 07:55 AM |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Lexically Scoped React Server Components
Summary
This PR introduces inline
"use client"and"use server"directives at the function level, eliminating the requirement that these directives apply to entire modules. Developers can now define client components and server functions directly inside any function body — at arbitrary nesting depths — within a single file.No other React framework currently supports this capability.
Motivation
In the standard RSC model,
"use client"and"use server"are module-level declarations. This forces developers to split tightly-coupled server and client logic across separate files, leading to:What's New
Inline
"use client"inside server componentsInline
"use server"inside client componentsArbitrary nesting (server → client → server → …)
Directives can alternate at any depth. A server component can define an inline client component, which itself defines an inline server function, and so on.
Lexical scope capture
Variables from parent scopes are automatically forwarded to extracted modules:
createElementwrappers at the call site.Function.prototype.bindarguments prepended to the function parameters.Module state sharing
Top-level declarations (constants, mutable variables, class instances) are not duplicated into extracted modules. Instead, extracted virtual modules import them from the original module, preserving identity and mutation semantics.
Architecture
The implementation consists of three components:
use-directive-inline.mjsuse-client-inline.mjs"use client". Defines how captured variables become destructured props and how call sites becomecreateElementwrappers.use-server-inline.mjs"use server". Defines how captured variables become.bind()arguments and how call sites become direct references.The plugin is instantiated once with both configs and registered as a single Vite plugin (
react-server:use-directive-inline).Extraction algorithm
file.jsx?use-client-inline=FnNameor?use-server-inline=FnName) containing the extracted function with its directive, necessary imports, and captured variable injection.createElementwrapper /.bind()call if captured variables are present).resolveId/load/transformlifecycle.Build integration
use-client.mjsplugin's transform filter was updated to match query-parameterized module IDs (/\.m?[jt]sx?(\?.*)?$/).manifest.mjsnow handles virtual modules with query parameters, correctly resolving file paths by stripping queries beforerealpathSyncand re-appending them for consistent key generation.manifest.mjsprocessesbuildClientManifestentries that only appear as dynamic imports in the SSR build (inline"use client"modules).client.mjsandserver.mjsbuild configurations register the unifieduseDirectiveInlineplugin.