|
| 1 | +--- |
| 2 | +name: react-start/server-components |
| 3 | +description: >- |
| 4 | + Implement, review, debug, and refactor TanStack Start React Server |
| 5 | + Components in React 19 apps. Use when tasks mention |
| 6 | + @tanstack/react-start/rsc, renderServerComponent, |
| 7 | + createCompositeComponent, CompositeComponent, |
| 8 | + renderToReadableStream, createFromReadableStream, createFromFetch, |
| 9 | + Composite Components, React Flight streams, loader or query owned |
| 10 | + RSC caching, router.invalidate, structuralSharing: false, |
| 11 | + selective SSR, stale names like renderRsc or .validator, or |
| 12 | + migration from Next App Router RSC patterns. Do not use for |
| 13 | + generic SSR or non-TanStack RSC frameworks except brief |
| 14 | + comparison. |
| 15 | +type: sub-skill |
| 16 | +library: tanstack-start |
| 17 | +library_version: '1.166.2' |
| 18 | +requires: |
| 19 | + - react-start |
| 20 | + - start-core/server-functions |
| 21 | + - start-core/execution-model |
| 22 | +sources: |
| 23 | + - TanStack/router:docs/start/framework/react/guide/server-components.md |
| 24 | + - TanStack/router:docs/start/framework/react/guide/server-functions.md |
| 25 | + - TanStack/router:docs/start/framework/react/guide/execution-model.md |
| 26 | + - TanStack/router:docs/router/guide/data-loading.md |
| 27 | +--- |
| 28 | + |
| 29 | +# TanStack Start React Server Components |
| 30 | + |
| 31 | +Treat TanStack Start RSCs as fetchable React Flight payloads, not as a framework-owned server tree. Start from data ownership and cache ownership, then choose the smallest RSC primitive that fits. |
| 32 | + |
| 33 | +## When this skill is active |
| 34 | + |
| 35 | +1. Inspect `vite.config.*` for `tanstackStart({ rsc: { enabled: true } })`, `rsc()`, and `viteReact()`. |
| 36 | +2. Inspect route files for `loader`, `loaderDeps`, `staleTime`, `ssr`, and `errorComponent`. |
| 37 | +3. Inspect server boundaries: `createServerFn`, `createServerOnlyFn`, `.server.*`, and imports from `@tanstack/react-start/server`. |
| 38 | +4. Identify the cache owner: Router loader cache, TanStack Query, or HTTP/server cache. |
| 39 | +5. Identify the refresh path: `router.invalidate()`, `invalidateQueries`, `refetchQueries`, or GET cache headers. |
| 40 | + |
| 41 | +## Hard invariants |
| 42 | + |
| 43 | +- Route loaders are isomorphic. Do not put DB access, secrets, or Node-only APIs directly in a loader. If the loader itself must use browser APIs, make that route `ssr: false`. |
| 44 | +- `renderServerComponent(...)` returns a renderable fragment. It does not support slots. |
| 45 | +- `createCompositeComponent(...)` is for server-rendered UI that must accept client-provided `children`, render props, or component props. |
| 46 | +- Query-cached RSC values require `structuralSharing: false`. |
| 47 | +- Slot payloads are opaque on the server. Do not inspect, map, or clone `props.children`. |
| 48 | +- Render-prop and component-slot arguments must stay Flight-serializable. |
| 49 | +- Current server function validation API is `.inputValidator(...)`. Older snippets may still show `.validator(...)`; normalize them. |
| 50 | +- TanStack custom serialization does not apply inside RSCs yet. Stay inside native Flight-supported values. |
| 51 | + |
| 52 | +## Decide three things immediately |
| 53 | + |
| 54 | +### 1) Transport / composition primitive |
| 55 | + |
| 56 | +- No client slots needed -> `renderServerComponent` |
| 57 | +- Client interactivity must be inserted inside server-rendered markup -> `createCompositeComponent` + `<CompositeComponent src={...} />` |
| 58 | +- Need custom Flight streaming, API routes, or non-standard transport -> `renderToReadableStream`, `createFromReadableStream`, `createFromFetch` |
| 59 | + |
| 60 | +### 2) Cache owner |
| 61 | + |
| 62 | +- Route-shaped data keyed by pathname, params, or search -> Router cache |
| 63 | +- Independent key space, background refetch, or non-route ownership -> TanStack Query |
| 64 | +- Cross-request reuse on server or CDN -> GET `createServerFn` + response cache headers and/or external server cache |
| 65 | + |
| 66 | +### 3) Refresh owner |
| 67 | + |
| 68 | +- Router-owned RSC -> `router.invalidate()` |
| 69 | +- Query-owned RSC -> `queryClient.invalidateQueries(...)` or `refetchQueries(...)` |
| 70 | +- Mixed Router + Query -> invalidate both deliberately; do not assume one refreshes the other |
| 71 | + |
| 72 | +## Pattern chooser |
| 73 | + |
| 74 | +- Simple server fragment in a route loader -> `renderServerComponent` |
| 75 | +- Interactive slot inside server markup -> `createCompositeComponent` |
| 76 | +- Route component needs browser APIs but loader can still prefetch on the server -> `ssr: 'data-only'` |
| 77 | +- Loader itself needs browser APIs -> `ssr: false` |
| 78 | +- Route cache key must include search params -> `loaderDeps` |
| 79 | +- Query-managed RSC -> `useSuspenseQuery` + SSR `ensureQueryData` |
| 80 | +- Multiple independent RSCs -> separate server functions + `Promise.all` |
| 81 | +- Multiple RSCs sharing data or invalidating together -> one server function returning many renderables or sources |
| 82 | +- Need isolated widget failures or staggered reveal -> return promises from the loader and resolve with `use()` inside Suspense |
| 83 | + |
| 84 | +## Slot choice |
| 85 | + |
| 86 | +- `children`: free-form composition, no server-to-client data flow |
| 87 | +- render props: the server must pass serializable data into client-rendered UI |
| 88 | +- component props: reusable client slot with a stable typed prop surface |
| 89 | +- If you are about to use `Children.map`, `cloneElement`, or inspect `children` on the server, stop and convert it to a render prop |
| 90 | + |
| 91 | +## Review / refactor checklist |
| 92 | + |
| 93 | +- Is the chosen primitive the smallest one that fits? |
| 94 | +- Does the loader own the RSC, or should Query own it? |
| 95 | +- Are route cache keys complete (`params` + minimal `loaderDeps`)? |
| 96 | +- Is invalidation hitting the real cache owner? |
| 97 | +- Are query options using `structuralSharing: false` for any RSC value? |
| 98 | +- Are mutations explicit `createServerFn({ method: 'POST' })` calls instead of hidden server actions? |
| 99 | +- Are server-only imports kept inside server functions or server-only boundaries? |
| 100 | +- Are examples using current names (`renderServerComponent`, `.inputValidator`) instead of stale ones? |
| 101 | + |
| 102 | +## Debug fast |
| 103 | + |
| 104 | +- Setup, exports, or stale docs mismatch -> `docs/current-api-notes.md` |
| 105 | +- Composite Component design or slot bug -> `docs/composite-components.md` |
| 106 | +- Stale data, refetching, loader keys, Query vs Router ownership, or SSR mode -> `docs/caching-refresh-ssr.md` |
| 107 | +- Review, refactor, import leaks, error boundaries, or serialization bugs -> `docs/debugging-review.md` |
| 108 | +- Architecture and Next/App Router translation -> `docs/architecture.md` |
| 109 | + |
| 110 | +## Copy-paste patterns |
| 111 | + |
| 112 | +- `examples/01-renderable-route-loader.tsx` |
| 113 | +- `examples/02-composite-slots.tsx` |
| 114 | +- `examples/03-query-owned-rsc.tsx` |
| 115 | +- `examples/04-selective-ssr-data-only.tsx` |
| 116 | +- `examples/05-ssr-false-browser-loader.tsx` |
| 117 | +- `examples/06-low-level-flight-api-route.tsx` |
| 118 | + |
| 119 | +## Default implementation sequence |
| 120 | + |
| 121 | +1. Keep server-only work inside `createServerFn` or `createServerOnlyFn` |
| 122 | +2. Return an RSC from the server function |
| 123 | +3. Consume it through the route loader unless Query has a clear ownership advantage |
| 124 | +4. Add the smallest cache policy that satisfies freshness requirements |
| 125 | +5. Wire invalidation exactly once at the real cache owner |
| 126 | +6. Escalate to Composite Components only when the client must fill slots |
| 127 | +7. Escalate to low-level Flight APIs only when high-level helpers cannot express the transport |
0 commit comments