| 201 | + | |
| 202 | + | export default async function* LiveClock() { |
| 203 | + | while (true) { |
| 204 | + | yield <div>{new Date().toLocaleTimeString()}</div>; |
| 205 | + | await new Promise(r => setTimeout(r, 1000)); |
| 206 | + | } |
| 207 | + | } |
| 208 | + | ``` |
| 209 | + | |
| 210 | + | ### Worker Function |
| 211 | + | |
| 212 | + | ```jsx |
| 213 | + | "use worker"; |
| 214 | + | |
| 215 | + | // All exports must be async functions |
| 216 | + | export async function fibonacci(n) { |
| 217 | + | if (n <= 1) return n; |
| 218 | + | return fibonacci(n - 1) + fibonacci(n - 2); |
| 219 | + | } |
| 220 | + | ``` |
| 221 | + | |
| 222 | + | ### Lexically Scoped RSC (inline directives) |
| 223 | + | |
| 224 | + | ```jsx |
| 225 | + | // No file-level directive — this is a server component |
| 226 | + | export default async function Page() { |
| 227 | + | const data = await fetchData(); |
| 228 | + | |
| 229 | + | // Inline client component — extracted at build time, no separate file needed |
| 230 | + | function InteractiveList({ items }) { |
| 231 | + | "use client"; |
| 232 | + | const [filter, setFilter] = useState(""); |
| 233 | + | return ( |
| 234 | + | <> |
| 235 | + | <input value={filter} onChange={e => setFilter(e.target.value)} /> |
| 236 | + | <ul>{items.filter(i => i.includes(filter)).map(i => <li key={i}>{i}</li>)}</ul> |
| 237 | + | </> |
| 238 | + | ); |
| 239 | + | } |
| 240 | + | |
| 241 | + | return <InteractiveList items={data} />; |
| 242 | + | } |
| 243 | + | ``` |
| 244 | + | |
| 245 | + | Server → Client → Server nesting is also supported: |
| 246 | + | |
| 247 | + | ```jsx |
| 248 | + | async function getData() { |
| 249 | + | "use server"; |
| 250 | + | |
| 251 | + | function Display({ value }) { |
| 252 | + | "use client"; |
| 253 | + | const [show, setShow] = useState(false); |
| 254 | + | return <button onClick={() => setShow(!show)}>{show ? value : "Reveal"}</button>; |
| 255 | + | } |
| 256 | + | |
| 257 | + | const value = await db.getSecret(); |
| 258 | + | return <Display value={value} />; |
| 259 | + | } |
| 260 | + | ``` |
| 261 | + | |
| 262 | + | ## HTTP Hooks |
| 263 | + | |
| 264 | + | Available in server components, middlewares, and API routes: |
| 265 | + | |
| 266 | + | ```jsx |
| 267 | + | import { |
| 268 | + | useUrl, // URL object |
| 269 | + | usePathname, // string |
| 270 | + | useSearchParams, // URLSearchParams |
| 271 | + | useRequest, // Request object |
| 272 | + | useResponse, // Response object |
| 273 | + | useFormData, // FormData (POST requests) |
| 274 | + | useHttpContext, // full context object |
| 275 | + | headers, // headers(name?) to read, headers({ key: value }) to set |
| 276 | + | cookie, // cookie(name?) to read, cookie(name, value, opts) to set |
| 277 | + | setCookie, // setCookie(name, value, opts) |
| 278 | + | deleteCookie, // deleteCookie(name) |
| 279 | + | status, // status(code, statusText?) |
| 280 | + | redirect, // redirect(url, statusCode?) |
| 281 | + | rewrite, // rewrite(url) |
| 282 | + | after, // after(callback) — runs after response is sent |
| 283 | + | } from "@lazarv/react-server"; |
| 284 | + | ``` |
| 285 | + | |
| 286 | + | ## Caching |
| 287 | + | |
| 288 | + | ```jsx |
| 289 | + | import { useResponseCache, useCache, withCache, revalidate, invalidate } from "@lazarv/react-server"; |
| 290 | + | |
| 291 | + | // Cache entire HTTP response for 30 seconds |
| 292 | + | useResponseCache(30_000); |
| 293 | + | |
| 294 | + | // HOC form |
| 295 | + | export default withCache(async function Page() { ... }, 30_000); |
| 296 | + | |
| 297 | + | // In-memory cache with compound key |
| 298 | + | const data = await useCache( |
| 299 | + | ["posts", category], |
| 300 | + | () => db.query("SELECT * FROM posts WHERE category = ?", [category]), |
| 301 | + | 60_000 |
| 302 | + | ); |
| 303 | + | |
| 304 | + | // Invalidate |
| 305 | + | revalidate(["posts", category]); |
| 306 | + | ``` |
| 307 | + | |
| 308 | + | Using the cache directive: |
| 309 | + | ```jsx |
| 310 | + | "use cache; ttl=30000; tags=posts"; |
| 311 | + | |
| 312 | + | export async function getPosts() { |
| 313 | + | return db.query("SELECT * FROM posts"); |
| 314 | + | } |
| 315 | + | |
| 316 | + | // Invalidate by tag |
| 317 | + | import { invalidate } from "@lazarv/react-server"; |
| 318 | + | await invalidate(getPosts); |
| 319 | + | ``` |
| 320 | + | |
| 321 | + | ## Configuration |
| 322 | + | |
| 323 | + | ```js |
| 324 | + | // react-server.config.mjs |
| 325 | + | import { defineConfig } from "@lazarv/react-server/config"; |
| 326 | + | |
| 327 | + | export default defineConfig({ |
| 328 | + | root: "src/pages", // File router root directory |
| 329 | + | public: "public", // Static assets directory |
| 330 | + | port: 3000, // Server port |
| 331 | + | adapter: "vercel", // Deployment adapter |
| 332 | + | // adapter: ["cloudflare", { serverlessFunctions: false }], |
| 333 | + | mdx: { // MDX support |
| 334 | + | remarkPlugins: [], |
| 335 | + | rehypePlugins: [], |
| 336 | + | components: "./src/mdx-components.jsx", |
| 337 | + | }, |
| 338 | + | cache: { |
| 339 | + | profiles: { |
| 340 | + | short: { ttl: 60_000 }, |
| 341 | + | }, |
| 342 | + | }, |
| 343 | + | telemetry: { // OpenTelemetry |
| 344 | + | enabled: true, |
| 345 | + | serviceName: "my-app", |
| 346 | + | }, |
| 347 | + | }); |
| 348 | + | ``` |
| 349 | + | |
| 350 | + | JSON config with schema validation: |
| 351 | + | ```json |
| 352 | + | { |
| 353 | + | "$schema": "https://react-server.dev/schema.json", |
| 354 | + | "port": 3000, |
| 355 | + | "adapter": "vercel" |
| 356 | + | } |
| 357 | + | ``` |
| 358 | + | |
| 359 | + | Extension configs are merged: `+tailwind.config.mjs`, `+auth.config.mjs`, etc. |
| 360 | + | Mode-specific: `.production.config.mjs`, `.development.config.mjs`, `.build.config.mjs`. |
| 361 | + | Env variables: `VITE_*` and `REACT_SERVER_*` prefixed vars are exposed via `import.meta.env`. |
| 362 | + | |
| 363 | + | ## Navigation |
| 364 | + | |
| 365 | + | ```jsx |
| 366 | + | import { Link, Refresh, ReactServerComponent } from "@lazarv/react-server/navigation"; |
| 367 | + | |
| 368 | + | // Client-side navigation |
| 369 | + | <Link href="/about" prefetch>About</Link> |
| 370 | + | |
| 371 | + | // Re-render current route |
| 372 | + | <Refresh>Reload</Refresh> |
| 373 | + | |
| 374 | + | // Render an outlet |
| 375 | + | <ReactServerComponent outlet="sidebar">{sidebar}</ReactServerComponent> |
| 376 | + | |
| 377 | + | // Programmatic navigation (client component only) |
| 378 | + | import { useClient } from "@lazarv/react-server/navigation"; |
| 379 | + | const { navigate, replace, prefetch } = useClient(); |
| 380 | + | ``` |
| 381 | + | |
| 382 | + | ## Error Handling |
| 383 | + | |
| 384 | + | ```jsx |
| 385 | + | import { ErrorBoundary } from "@lazarv/react-server/error-boundary"; |
| 386 | + | |
| 387 | + | <ErrorBoundary |
| 388 | + | fallback={<p>Loading...</p>} |
| 389 | + | component={({ error, resetErrorBoundary }) => ( |
| 390 | + | <div> |
| 391 | + | <p>{error.message}</p> |
| 392 | + | <button onClick={resetErrorBoundary}>Retry</button> |
| 393 | + | </div> |
| 394 | + | )} |
| 395 | + | > |
| 396 | + | <Content /> |
| 397 | + | </ErrorBoundary> |
| 398 | + | ``` |
| 399 | + | |
| 400 | + | File router conventions: `error.page.jsx` for error boundaries, `loading.page.jsx` for Suspense fallbacks. |