Skip to content

Commit 427f24d

Browse files
authored
Add RSC Sandboxes (#8300)
* Add RSC Sandboxes * Recover from errors, parse directives in worker with AST * Fix client edits * fast refresh * hardening * make rsc-sandbox-test dev-only * add more examples to rsc-sandbox-test * remove rsc-sandbox-text from sidebar * Add inline server actions * fix lint * pin deps, update skill * remove [RSC Client Error] from logs * log error directly in worker * update worker
1 parent 11e4ad5 commit 427f24d

23 files changed

+34665
-24
lines changed

.claude/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"Skill(docs-voice)",
1717
"Skill(docs-components)",
1818
"Skill(docs-sandpack)",
19+
"Skill(docs-rsc-sandpack)",
1920
"Skill(docs-writer-learn)",
2021
"Skill(docs-writer-reference)",
2122
"Bash(yarn lint:*)",
Lines changed: 277 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,277 @@
1+
---
2+
name: docs-rsc-sandpack
3+
description: Use when adding interactive RSC (React Server Components) code examples to React docs using <SandpackRSC>, or when modifying the RSC sandpack infrastructure.
4+
---
5+
6+
# RSC Sandpack Patterns
7+
8+
For general Sandpack conventions (code style, naming, file naming, line highlighting, hidden files, CSS guidelines), see `/docs-sandpack`. This skill covers only RSC-specific patterns.
9+
10+
## Quick Start Template
11+
12+
Minimal single-file `<SandpackRSC>` example:
13+
14+
```mdx
15+
<SandpackRSC>
16+
17+
` ` `js src/App.js
18+
export default function App() {
19+
return <h1>Hello from a Server Component!</h1>;
20+
}
21+
` ` `
22+
23+
</SandpackRSC>
24+
```
25+
26+
---
27+
28+
## How It Differs from `<Sandpack>`
29+
30+
| Feature | `<Sandpack>` | `<SandpackRSC>` |
31+
|---------|-------------|-----------------|
32+
| Execution model | All code runs in iframe | Server code runs in Web Worker, client code in iframe |
33+
| `'use client'` directive | Ignored (everything is client) | Required to mark client components |
34+
| `'use server'` directive | Not supported | Marks Server Functions callable from client |
35+
| `async` components | Not supported | Supported (server components can be async) |
36+
| External dependencies | Supported via `package.json` | Not supported (only React + react-dom) |
37+
| Entry point | `App.js` with `export default` | `src/App.js` with `export default` |
38+
| Component tag | `<Sandpack>` | `<SandpackRSC>` |
39+
40+
---
41+
42+
## File Directives
43+
44+
Files are classified by the directive at the top of the file:
45+
46+
| Directive | Where it runs | Rules |
47+
|-----------|--------------|-------|
48+
| (none) | Web Worker (server) | Default. Can be `async`. Can import other server files. Cannot use hooks, event handlers, or browser APIs. |
49+
| `'use client'` | Sandpack iframe (browser) | Must be first statement. Can use hooks, event handlers, browser APIs. Cannot be `async`. Cannot import server files. |
50+
| `'use server'` | Web Worker (server) | Marks Server Functions. Can be module-level (all exports are actions) or function-level. Callable from client via props or form `action`. |
51+
52+
---
53+
54+
## Common Patterns
55+
56+
### 1. Server + Client Components
57+
58+
```mdx
59+
<SandpackRSC>
60+
61+
` ` `js src/App.js
62+
import Counter from './Counter';
63+
64+
export default function App() {
65+
return (
66+
<div>
67+
<h1>Server-rendered heading</h1>
68+
<Counter />
69+
</div>
70+
);
71+
}
72+
` ` `
73+
74+
` ` `js src/Counter.js
75+
'use client';
76+
77+
import { useState } from 'react';
78+
79+
export default function Counter() {
80+
const [count, setCount] = useState(0);
81+
return (
82+
<button onClick={() => setCount(count + 1)}>
83+
Count: {count}
84+
</button>
85+
);
86+
}
87+
` ` `
88+
89+
</SandpackRSC>
90+
```
91+
92+
### 2. Async Server Component with Suspense
93+
94+
```mdx
95+
<SandpackRSC>
96+
97+
` ` `js src/App.js
98+
import { Suspense } from 'react';
99+
import Albums from './Albums';
100+
101+
export default function App() {
102+
return (
103+
<Suspense fallback={<p>Loading...</p>}>
104+
<Albums />
105+
</Suspense>
106+
);
107+
}
108+
` ` `
109+
110+
` ` `js src/Albums.js
111+
async function fetchAlbums() {
112+
await new Promise(resolve => setTimeout(resolve, 1000));
113+
return ['Abbey Road', 'Let It Be', 'Revolver'];
114+
}
115+
116+
export default async function Albums() {
117+
const albums = await fetchAlbums();
118+
return (
119+
<ul>
120+
{albums.map(album => (
121+
<li key={album}>{album}</li>
122+
))}
123+
</ul>
124+
);
125+
}
126+
` ` `
127+
128+
</SandpackRSC>
129+
```
130+
131+
### 3. Server Functions (Actions)
132+
133+
```mdx
134+
<SandpackRSC>
135+
136+
` ` `js src/App.js
137+
import { addLike, getLikeCount } from './actions';
138+
import LikeButton from './LikeButton';
139+
140+
export default async function App() {
141+
const count = await getLikeCount();
142+
return (
143+
<div>
144+
<p>Likes: {count}</p>
145+
<LikeButton addLike={addLike} />
146+
</div>
147+
);
148+
}
149+
` ` `
150+
151+
` ` `js src/actions.js
152+
'use server';
153+
154+
let count = 0;
155+
156+
export async function addLike() {
157+
count++;
158+
}
159+
160+
export async function getLikeCount() {
161+
return count;
162+
}
163+
` ` `
164+
165+
` ` `js src/LikeButton.js
166+
'use client';
167+
168+
export default function LikeButton({ addLike }) {
169+
return (
170+
<form action={addLike}>
171+
<button type="submit">Like</button>
172+
</form>
173+
);
174+
}
175+
` ` `
176+
177+
</SandpackRSC>
178+
```
179+
180+
---
181+
182+
## File Structure Requirements
183+
184+
### Entry Point
185+
186+
- **`src/App.js` is required** as the main entry point
187+
- Must have `export default` (function component)
188+
- Case-insensitive fallback: `src/app.js` also works
189+
190+
### Auto-Injected Infrastructure Files
191+
192+
These files are automatically injected by `sandpack-rsc-setup.ts` and should never be included in MDX:
193+
194+
| File | Purpose |
195+
|------|---------|
196+
| `/src/index.js` | Bootstraps the RSC pipeline |
197+
| `/src/rsc-client.js` | Client bridge — creates Worker, consumes Flight stream |
198+
| `/src/rsc-server.js` | Wraps pre-bundled worker runtime as ES module |
199+
| `/node_modules/__webpack_shim__/index.js` | Minimal webpack compatibility layer |
200+
| `/node_modules/__rsdw_client__/index.js` | `react-server-dom-webpack/client` as local dependency |
201+
202+
### No External Dependencies
203+
204+
`<SandpackRSC>` does not support external npm packages. Only `react` and `react-dom` are available. Do not include `package.json` in RSC examples.
205+
206+
---
207+
208+
## Architecture Reference
209+
210+
### Three-Layer Architecture
211+
212+
```
213+
react.dev page (Next.js)
214+
┌─────────────────────────────────────────┐
215+
│ <SandpackRSC> │
216+
│ ┌─────────┐ ┌──────────────────────┐ │
217+
│ │ Editor │ │ Preview (iframe) │ │
218+
│ │ App.js │ │ Client React app │ │
219+
│ │ (edit) │ │ consumes Flight │ │
220+
│ │ │ │ stream from Worker │ │
221+
│ └─────────┘ └──────────┬───────────┘ │
222+
└───────────────────────────┼─────────────┘
223+
│ postMessage
224+
┌───────────────────────────▼─────────────┐
225+
│ Web Worker (Blob URL) │
226+
│ - React server build (pre-bundled) │
227+
│ - react-server-dom-webpack/server │
228+
│ - webpack shim │
229+
│ - User server code (Sucrase → CJS) │
230+
└─────────────────────────────────────────┘
231+
```
232+
233+
### Key Source Files
234+
235+
| File | Purpose |
236+
|-----------------------------------------------------------------|--------------------------------------------------------------------------------|
237+
| `src/components/MDX/Sandpack/sandpack-rsc/RscFileBridge.tsx` | Monitors Sandpack; posts raw files to iframe |
238+
| `src/components/MDX/Sandpack/SandpackRSCRoot.tsx` | SandpackProvider setup, custom bundler URL, UI layout |
239+
| `src/components/MDX/Sandpack/templateRSC.ts` | RSC template files |
240+
| `.../sandbox-code/src/__react_refresh_init__.js` | React Refresh shim |
241+
| `.../sandbox-code/src/rsc-server.js` | Worker runtime: module system, Sucrase compilation, `renderToReadableStream()` |
242+
| `.../sandbox-code/src/rsc-client.source.js` | Client bridge: Worker creation, file classification, Flight stream consumption |
243+
| `.../sandbox-code/src/webpack-shim.js` | Minimal `__webpack_require__` / `__webpack_module_cache__` shim |
244+
| `.../sandbox-code/src/worker-bundle.dist.js` | Pre-bundled IIFE (generated): React server + RSDW/server + Sucrase |
245+
| `scripts/buildRscWorker.mjs` | esbuild script: bundles rsc-server.js into worker-bundle.dist.js |
246+
247+
---
248+
249+
## Build System
250+
251+
### Rebuilding the Worker Bundle
252+
253+
After modifying `rsc-server.js` or `webpack-shim.js`:
254+
255+
```bash
256+
node scripts/buildRscWorker.mjs
257+
```
258+
259+
This runs esbuild with:
260+
- `format: 'iife'`, `platform: 'browser'`
261+
- `conditions: ['react-server', 'browser']` (activates React server export conditions)
262+
- `minify: true`
263+
- Prepends `webpack-shim.js` to the output
264+
265+
### Raw-Loader Configuration
266+
267+
In `templateRSC.js` files are loaded as raw strings with the `!raw-loader`.
268+
269+
The strings are necessary to provide to Sandpack as local files (skips Sandpack bundling).
270+
271+
272+
### Development Commands
273+
274+
```bash
275+
node scripts/buildRscWorker.mjs # Rebuild worker bundle after source changes
276+
yarn dev # Start dev server to test examples
277+
```

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ scripts
22
plugins
33
next.config.js
44
.claude/
5+
worker-bundle.dist.js

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"scripts": {
77
"analyze": "ANALYZE=true next build",
88
"dev": "next-remote-watch ./src/content",
9-
"build": "next build && node --experimental-modules ./scripts/downloadFonts.mjs",
9+
"prebuild:rsc": "node scripts/buildRscWorker.mjs",
10+
"build": "node scripts/buildRscWorker.mjs && next build && node --experimental-modules ./scripts/downloadFonts.mjs",
1011
"lint": "next lint && eslint \"src/content/**/*.md\"",
1112
"lint:fix": "next lint --fix && eslint \"src/content/**/*.md\" --fix",
1213
"format:source": "prettier --config .prettierrc --write \"{plugins,src}/**/*.{js,ts,jsx,tsx,css}\"",
@@ -38,6 +39,7 @@
3839
"next": "15.1.12",
3940
"next-remote-watch": "^1.0.0",
4041
"parse-numeric-range": "^1.2.0",
42+
"raw-loader": "^4.0.2",
4143
"react": "^19.0.0",
4244
"react-collapsed": "4.0.4",
4345
"react-dom": "^19.0.0",
@@ -65,6 +67,7 @@
6567
"babel-eslint": "10.x",
6668
"babel-plugin-react-compiler": "^1.0.0",
6769
"chalk": "4.1.2",
70+
"esbuild": "^0.24.0",
6871
"eslint": "7.x",
6972
"eslint-config-next": "12.0.3",
7073
"eslint-config-react-app": "^5.2.1",
@@ -88,6 +91,7 @@
8891
"postcss-flexbugs-fixes": "4.2.1",
8992
"postcss-preset-env": "^6.7.0",
9093
"prettier": "^2.5.1",
94+
"react-server-dom-webpack": "^19.2.4",
9195
"reading-time": "^1.2.0",
9296
"remark": "^12.0.1",
9397
"remark-external-links": "^7.0.0",

scripts/buildRscWorker.mjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
import * as esbuild from 'esbuild';
9+
import fs from 'fs';
10+
import path from 'path';
11+
import {fileURLToPath} from 'url';
12+
13+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
14+
const root = path.resolve(__dirname, '..');
15+
const sandboxBase = path.resolve(
16+
root,
17+
'src/components/MDX/Sandpack/sandpack-rsc/sandbox-code/src'
18+
);
19+
20+
// 1. Bundle the server Worker runtime (React server build + RSDW/server.browser + Sucrase → IIFE)
21+
// Minified because this runs inside a Web Worker (not parsed by Sandpack's Babel).
22+
const workerOutfile = path.resolve(sandboxBase, 'worker-bundle.dist.js');
23+
await esbuild.build({
24+
entryPoints: [path.resolve(sandboxBase, 'rsc-server.js')],
25+
bundle: true,
26+
format: 'iife',
27+
platform: 'browser',
28+
conditions: ['react-server', 'browser'],
29+
outfile: workerOutfile,
30+
define: {'process.env.NODE_ENV': '"production"'},
31+
minify: true,
32+
});
33+
34+
// Post-process worker bundle:
35+
// Prepend the webpack shim so __webpack_require__ (used by react-server-dom-webpack)
36+
// is defined before the IIFE evaluates. The shim sets globalThis.__webpack_require__,
37+
// which is accessible as a bare identifier since globalThis IS the Worker's global scope.
38+
let workerCode = fs.readFileSync(workerOutfile, 'utf8');
39+
40+
const shimPath = path.resolve(sandboxBase, 'webpack-shim.js');
41+
const shimCode = fs.readFileSync(shimPath, 'utf8');
42+
workerCode = shimCode + '\n' + workerCode;
43+
44+
fs.writeFileSync(workerOutfile, workerCode);

src/components/MDX/MDXComponents.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import BlogCard from './BlogCard';
2626
import Link from './Link';
2727
import {PackageImport} from './PackageImport';
2828
import Recap from './Recap';
29-
import Sandpack from './Sandpack';
29+
import {SandpackClient as Sandpack, SandpackRSC} from './Sandpack';
3030
import SandpackWithHTMLOutput from './SandpackWithHTMLOutput';
3131
import Diagram from './Diagram';
3232
import DiagramGroup from './DiagramGroup';
@@ -551,6 +551,7 @@ export const MDXComponents = {
551551
Recap,
552552
Recipes,
553553
Sandpack,
554+
SandpackRSC,
554555
SandpackWithHTMLOutput,
555556
TeamMember,
556557
TerminalBlock,

src/components/MDX/Sandpack/Console.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export const SandpackConsole = ({visible}: {visible: boolean}) => {
119119
setLogs((prev) => {
120120
const newLogs = message.log
121121
.filter((consoleData) => {
122-
if (!consoleData.method) {
122+
if (!consoleData.method || !consoleData.data) {
123123
return false;
124124
}
125125
if (

0 commit comments

Comments
 (0)