Getting Started
@fastify/react is @fastify/vite's core renderer for React. It is accompanied by @fastify/react/plugin, a Vite plugin that complements the renderer package.
It implements all of the features specified in Core Renderers, including automated routing, universal data fetching and head management. It's meant to be a lightweight Fastify-flavored replacement to Next.js and other full blown SSR React frameworks. It is Fastify-first in the sense that your Fastify server is responsible for setting everything up via @fastify/vite.
Below is an overview of all individual documentation topics and the order in which it makes the most sense to read them.
- Project Structure covers the structure of a
@fastify/reactproject, configuration, special folders and others conventions employed. - Router Setup covers how route modules get registered as actual routes, both on the server and the client.
- Route Modules covers what makes up route modules, what special exports they have and how they work.
- Route Context covers the universal route context initialization module and the
useRouteContext()hook, available to all route modules and route layouts. - Route Layouts covers route layout modules.
- Rendering Modes covers all different route module rendering modes.
Runtime requirements
- Node.js v18 — you should upgrade to v20 LTS to benefit from massive performance improvements!
- We recommend PNPM as a package manager.
Support for other JavaScript environments with HTTP support based on the Fetch and Service Worker standards is coming when fastify-edge is finished.
Starter templates
Since @fastify/vite is not a framework but rather a Fastify plugin, it can't run your application on its own, you need to have your Fastify server, a Vite configuration file, and the basic file structure that make up your frontend.
react-base
The react-base starter template includes just about the minimum set of files to get your @fastify/react application going. It contains no embedded examples other than pages/index.jsx, and no additional dependencies.
Download
We recommend using giget to download straight from GitHub.
giget gh:fastify/fastify-vite/starters/react-base#dev your-appgiget gh:fastify/fastify-vite/starters/react-base#dev your-appDependencies
fastifyas the Node.js server.vitefor the client application bundling.@fastify/vitefor Vite integration in Fastify.- And its peer dependencies:
devaluehistoryminipassreactreact-domreactr-routeruniheadvaltio
- And its peer dependencies:
@fastify/reactfor the React application shell.@vitejs/plugin-reactfor React support in Vite.@fastify/one-line-loggerfor better logging in development.
react-kitchensink
The react-kitchensink starter template includes all of react-base plus a sample context.js initialization file and same additional example routes under pages/.
Download
We recommend using giget to download straight from GitHub.
giget gh:fastify/fastify-vite/starters/react-kitchensink#dev your-appgiget gh:fastify/fastify-vite/starters/react-kitchensink#dev your-appDependencies
All dependencies from react-base plus:
postcss-preset-envfor CSS Nesting support.
Known limitations
It's currently impossible to run multiple Vite development server middleware in your Fastify server, which means
@fastify/vitecan only be registered once. Configuration for this is somewhat tricky and there isn't documentation on how to do it. Once #108 is completed and merged, it will open the path to have a Vite development server factory that can create instances on-demand, but that approach still remains to be tested.If you're looking into a microfrontend setup, consider this approach.
@fastify/reactcurrently has no support for producing a fully functional static bundle, that is, even when you useclientOnly, you'd need to be running the Fastify server integrated with the@fastify/viterenderer. SPA support is planned for the next major release, see the project roadmap.There's not hot reload for the
context.jsfile. This should be addressed in the next major release, see the project roadmap.