You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| Execution model | All code runs in iframe | Server code runs in Web Worker, client code in iframe |
33
33
|`'use client'` directive | Ignored (everything is client) | Required to mark client components |
34
-
|`'use server'` directive | Not supported | Marks server actions callable from client |
34
+
|`'use server'` directive | Not supported | Marks Server Functions callable from client |
35
35
|`async` components | Not supported | Supported (server components can be async) |
36
36
| External dependencies | Supported via `package.json`| Not supported (only React + react-dom) |
37
37
| Entry point |`App.js` with `export default`|`src/App.js` with `export default`|
@@ -47,9 +47,7 @@ Files are classified by the directive at the top of the file:
47
47
|-----------|--------------|-------|
48
48
| (none) | Web Worker (server) | Default. Can be `async`. Can import other server files. Cannot use hooks, event handlers, or browser APIs. |
49
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 actions. Can be module-level (all exports are actions) or function-level. Callable from client via props or form `action`. |
51
-
52
-
**Directive detection** is a string match on the first non-comment statement. The directive must appear exactly as `'use client';` or `'use server';` (single or double quotes, semicolon optional).
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`. |
53
51
54
52
---
55
53
@@ -200,56 +198,13 @@ These files are automatically injected by `sandpack-rsc-setup.ts` and should nev
200
198
|`/src/rsc-server.js`| Wraps pre-bundled worker runtime as ES module |
|`/node_modules/__rsdw_client__/index.js`|`react-server-dom-webpack/client` as local dependency |
203
-
|`/src/styles.css`| Base styles |
204
201
205
202
### No External Dependencies
206
203
207
204
`<SandpackRSC>` does not support external npm packages. Only `react` and `react-dom` are available. Do not include `package.json` in RSC examples.
208
205
209
206
---
210
207
211
-
## Constraints and Pitfalls
212
-
213
-
### What Server Components Cannot Do
214
-
215
-
| Cannot | Reason |
216
-
|--------|--------|
217
-
| Use hooks (`useState`, `useEffect`, etc.) | Server components are stateless, run once |
218
-
| Attach event handlers (`onClick`, `onChange`) | No interactivity on server |
219
-
| Access browser APIs (`window`, `document`, `localStorage`) | Runs in Web Worker, not browser |
220
-
| Import client components without `'use client'`| Client code must be explicitly marked |
221
-
| Use `useContext`| Context is a client concept |
222
-
223
-
### What Client Components Cannot Do
224
-
225
-
| Cannot | Reason |
226
-
|--------|--------|
227
-
| Be `async`| Client components render synchronously |
228
-
| Import server-only files directly | Would execute server code in browser |
229
-
| Use `'use server'` at module level | That marks a server actions file |
230
-
| Read from databases or file systems | Runs in browser |
231
-
232
-
### Sucrase Compilation Limitations
233
-
234
-
| Limitation | Detail |
235
-
|------------|--------|
236
-
| No TypeScript type erasure for complex generics | Sucrase handles basic TS but not all edge cases |
237
-
| No decorators | Not supported by Sucrase |
238
-
| No dynamic `import()`| Worker module system uses synchronous `require()`|
239
-
| CommonJS output only (server side) | Server files compiled to CJS with `var` declarations |
240
-
241
-
### Other Constraints
242
-
243
-
| Constraint | Detail |
244
-
|------------|--------|
245
-
| FormData serialization | FormData is not structurally cloneable for `postMessage`; serialized as `[key, value]` entries and reconstructed in Worker |
246
-
| Module resolution | Custom `resolvePath()` — only relative paths (`./`, `../`) with `.js`, `.jsx`, `.ts`, `.tsx` extensions. No `node_modules` resolution. |
247
-
| CSS imports |`require('*.css')` returns empty object. No CSS bundling in the Worker. |
248
-
| Circular dependencies | Partially supported — partially populated exports returned during circular `require()`|
249
-
| React version | Client: `^19.2.1` via Sandpack CDN. Server: pinned to pre-bundled version in `worker-bundle.source.js`. Must stay in sync. |
250
-
251
-
---
252
-
253
208
## Architecture Reference
254
209
255
210
### Three-Layer Architecture
@@ -275,46 +230,27 @@ react.dev page (Next.js)
275
230
└─────────────────────────────────────────┘
276
231
```
277
232
278
-
### Data Flow (14 Steps)
279
-
280
-
1. MDX parser extracts code blocks from `<SandpackRSC>` children
281
-
2.`createFileMap()` converts code blocks to Sandpack file map
The strings are necessary to provide to Sandpack as local files (skips Sandpack bundling).
362
270
363
-
## Debugging
364
-
365
-
### Common Errors
366
-
367
-
| Error | Cause | Fix |
368
-
|-------|-------|-----|
369
-
|`Cannot find module './Component'`| Missing file or wrong path in import | Check file names match exactly (case-sensitive) |
370
-
|`X is not a function`| Server component imported without `'use client'` in client file | Add `'use client'` directive to the importing file, or restructure imports |
371
-
| Flight stream parse error | Server code threw during render | Check server component for runtime errors (bad data, missing imports) |
372
-
|`__webpack_require__ is not defined`| Worker bundle not rebuilt after shim changes | Run `node scripts/buildRscWorker.mjs`|
373
-
| Blank preview, no errors | Infrastructure files not injected | Verify `sandpack-rsc-setup.ts` loads all `.source.js` files |
374
-
|`FormData is not defined`| Using FormData in server action without proper serialization | The system handles this automatically; check for custom FormData usage |
375
-
| Hooks in server component |`useState`/`useEffect` used without `'use client'`| Move interactive code to a client component |
376
-
377
-
### Debugging Steps
378
-
379
-
1.**Check browser console** — Flight stream errors and Worker errors surface here
380
-
2.**Check the Worker** — In DevTools, navigate to Sources > Worker threads to inspect the Worker
381
-
3.**Verify directives** — Ensure `'use client'` / `'use server'` are the first statement (no imports before them)
382
-
4.**Test in isolation** — Create a minimal `<SandpackRSC>` with just `App.js` to rule out file interaction issues
383
-
5.**Rebuild worker bundle** — After any changes to `.source.js` files: `node scripts/buildRscWorker.mjs`
384
271
385
272
### Development Commands
386
273
387
274
```bash
388
275
node scripts/buildRscWorker.mjs # Rebuild worker bundle after source changes
389
276
yarn dev # Start dev server to test examples
390
-
yarn build # Full production build (includes worker)
391
-
```
392
-
393
-
---
394
-
395
-
## Anti-Patterns
396
-
397
-
| Pattern | Problem | Fix |
398
-
|---------|---------|-----|
399
-
|`'use client'` in `App.js`| Makes entire app client-rendered, defeats RSC purpose | Keep `App.js` as server component; extract interactive parts to separate client files |
400
-
| Hooks in server component | Runtime error — hooks not available in Worker | Move to `'use client'` component |
401
-
|`import` before `'use client'`| Directive not detected (must be first statement) | Move `'use client'` to line 1 |
402
-
|`package.json` in `<SandpackRSC>`| External dependencies not supported | Remove; use only React built-ins |
403
-
|`window`/`document` in server file | Not available in Web Worker | Move to `'use client'` component |
404
-
| Server component importing client component without directive | Client code executes in Worker and fails | Add `'use client'` to the client file |
405
-
| Passing non-serializable props to client components | Flight protocol can only serialize JSON-compatible values + React elements + server references | Use serializable data; pass server actions for functions |
406
-
|`async` client component | Client components cannot be async | Only server components can be `async`|
407
-
|`<Sandpack>` instead of `<SandpackRSC>`| Standard Sandpack has no RSC support | Use `<SandpackRSC>` for RSC examples |
0 commit comments