|
| 1 | +# React Router + RSC + StyleX |
| 2 | + |
| 3 | +⚠️ **EXPERIMENTAL**: This template demonstrates React Server Components with |
| 4 | +React Router. |
| 5 | + |
| 6 | +## Highlights |
| 7 | + |
| 8 | +- 🧪 React Server Components powered by Vite’s RSC plugin |
| 9 | +- 🧵 Styling via [`@stylexjs/stylex`](https://stylexjs.com/) compiled by |
| 10 | + [`@stylexjs/unplugin`](https://www.npmjs.com/package/@stylexjs/unplugin) |
| 11 | +- 🧭 React Router 7 data APIs + server actions |
| 12 | +- ⚡️ Instant HMR during `example:dev` |
| 13 | +- 🔣 TypeScript out of the box |
| 14 | + |
| 15 | +## Scripts |
| 16 | + |
| 17 | +```bash |
| 18 | +npm install # install dependencies |
| 19 | +npm run example:dev # start Vite dev server with RSC + StyleX |
| 20 | +rm -rf dist && npm run example:build # optional clean + production build |
| 21 | +npm run example:start # run the Express server using the dist/ output |
| 22 | +npm run example:typecheck # type-check without emitting files |
| 23 | +``` |
| 24 | + |
| 25 | +Dev server runs on `http://localhost:5173` by default. |
| 26 | + |
| 27 | +## StyleX integration |
| 28 | + |
| 29 | +The Vite config registers the StyleX unplugin before the RSC plugin so that both |
| 30 | +client and server bundles share the same compiled CSS: |
| 31 | + |
| 32 | +```ts |
| 33 | +import stylex from '@stylexjs/unplugin'; |
| 34 | + |
| 35 | +export default defineConfig({ |
| 36 | + plugins: [ |
| 37 | + stylex.vite({ useCSSLayers: true }), |
| 38 | + react(), |
| 39 | + rsc({ |
| 40 | + /* ... */ |
| 41 | + }), |
| 42 | + ], |
| 43 | +}); |
| 44 | +``` |
| 45 | + |
| 46 | +- `src/stylex.css` is imported from the root route. This ensures Vite always |
| 47 | + emits a CSS asset that the unplugin can append to. |
| 48 | +- During development the layout injects the virtual StyleX runtime and |
| 49 | + stylesheet so HMR picks up CSS changes without reloading: |
| 50 | + ```tsx |
| 51 | + { |
| 52 | + import.meta.env.DEV ? ( |
| 53 | + <> |
| 54 | + <script type="module" src="/@id/virtual:stylex:runtime" /> |
| 55 | + <link rel="stylesheet" href="/virtual:stylex.css" /> |
| 56 | + </> |
| 57 | + ) : null; |
| 58 | + } |
| 59 | + ``` |
| 60 | +- `@stylexjs/stylex` is used throughout the client components |
| 61 | + (`routes/root/client.tsx`, `routes/home/route.tsx`, etc.) instead of Tailwind |
| 62 | + classes. |
| 63 | + |
| 64 | +## Hot reloading gotcha |
| 65 | + |
| 66 | +React Router’s experimental RSC runtime exposes a `__reactRouterDataRouter` |
| 67 | +handle instead of the older `__router` object that Remix-based examples |
| 68 | +reference. To keep RSC + StyleX HMR working, the dev-only listener in |
| 69 | +`src/entry.browser.tsx` looks for either key before calling `revalidate()`: |
| 70 | + |
| 71 | +```ts |
| 72 | +if (import.meta.hot) { |
| 73 | + import.meta.hot.on('rsc:update', () => { |
| 74 | + const reactDataRouter = |
| 75 | + (window as { __router?: DataRouter }).__router ?? |
| 76 | + (window as { __reactRouterDataRouter?: DataRouter }) |
| 77 | + .__reactRouterDataRouter; |
| 78 | + reactDataRouter?.revalidate(); |
| 79 | + }); |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +Without this fallback, RSC updates would silently no-op during dev. |
| 84 | + |
| 85 | +## React Server Components recap |
| 86 | + |
| 87 | +Three entry points orchestrate the RSC flow: |
| 88 | + |
| 89 | +- **`entry.rsc.tsx`** — React Server request handler |
| 90 | +- **`entry.ssr.tsx`** — wraps the RSC payload in HTML for SSR |
| 91 | +- **`entry.browser.tsx`** — hydrates the RSC payload on the client |
| 92 | + |
| 93 | +Routes are defined in `src/routes/config.ts` using `unstable_RSCRouteConfig`, |
| 94 | +and shared layout/UI lives in `src/routes/root`. |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +Built with ❤️ using React Router + StyleX. |
0 commit comments