|
| 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 | +``` |
0 commit comments