Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply 62 React and Next.js performance optimization rules from Vercel Engineering
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/server-no-shared-module-state.md
1---2title: Avoid Shared Module State for Request Data3impact: HIGH4impactDescription: prevents concurrency bugs and request data leaks5tags: server, rsc, ssr, concurrency, security, state6---78## Avoid Shared Module State for Request Data910For React Server Components and client components rendered during SSR, avoid using mutable module-level variables to share request-scoped data. Server renders can run concurrently in the same process. If one render writes to shared module state and another render reads it, you can get race conditions, cross-request contamination, and security bugs where one user's data appears in another user's response.1112Treat module scope on the server as process-wide shared memory, not request-local state.1314**Incorrect (request data leaks across concurrent renders):**1516```tsx17let currentUser: User | null = null1819export default async function Page() {20currentUser = await auth()21return <Dashboard />22}2324async function Dashboard() {25return <div>{currentUser?.name}</div>26}27```2829If two requests overlap, request A can set `currentUser`, then request B overwrites it before request A finishes rendering `Dashboard`.3031**Correct (keep request data local to the render tree):**3233```tsx34export default async function Page() {35const user = await auth()36return <Dashboard user={user} />37}3839function Dashboard({ user }: { user: User | null }) {40return <div>{user?.name}</div>41}42```4344Safe exceptions:4546- Immutable static assets or config loaded once at module scope47- Shared caches intentionally designed for cross-request reuse and keyed correctly48- Process-wide singletons that do not store request- or user-specific mutable data4950For static assets and config, see [Hoist Static I/O to Module Level](./server-hoist-static-io.md).51