Server rendering for React and Vite
Name a file.
Get a route.
Kiln turns a directory of React components into a server-rendered app. Each file picks its own render mode, and the build keeps server code out of the browser.
| File | URL | Mode |
|---|---|---|
| app/pages/index.tsx | / | universal |
| app/pages/about.server.tsx | /about | server |
| app/pages/dashboard.client.tsx | /dashboard | client |
| app/pages/blog/[slug].tsx | /blog/:slug | universal |
| app/pages/docs/[...path].md | /docs/*path | universal |
Pick a mode per page
The file name sets the mode. Compare the three side by side.
| Mode | Example file | Server output | JavaScript | Use it for |
|---|---|---|---|---|
| universal | index.tsx | Full page | Hydrates the HTML | Most pages. Readable before JavaScript loads, interactive after. |
| server | about.server.tsx | Full page, streamed | None, unless islands | Content with no interactivity, such as legal and marketing text. |
| client | dashboard.client.tsx | Empty shell | Renders in the browser | Screens that depend on browser-only state. |
A page is a component
Export a component, and optionally a loader. Kiln runs the loader on the server, passes the result as a prop, and serializes it into the page for hydration.
- The loader runs on the server only, and never reaches the browser bundle.
- Titles come from a plain
kiln:titletag. - Layouts wrap every page in their folder and below.
// app/pages/blog/[slug].tsx
export async function loader({ params }) {
return getPost(params.slug);
}
export default function Post({ data }) {
return (
<article>
<kiln:title>{data.title}</kiln:title>
<h1>{data.title}</h1>
</article>
);
}What happens on a request
Every page view follows the same four steps. Server pages stop after the render.
- 01
Request
The server receives a URL and matches it against the route table.
- 02
Loader
If the page exports a loader, it runs on the server with params and the request.
- 03
Render
React renders the page to HTML. Server pages stop here.
- 04
Hydrate
The browser attaches handlers to the HTML it already has. Nothing renders twice.
Packages
Install only what you use. Most apps need the CLI, the core, the server, and the client.
| Package | What it does |
|---|---|
| @kiln-js/core | Route matching, titles, env validation, errors |
| @kiln-js/server | Request handler, API dispatch, CSRF and CORS |
| @kiln-js/client | Hydration, navigation, Link |
| @kiln-js/md | Markdown pages with styled tags |
| @kiln-js/vite-plugin | Route table, server-code removal, islands |
| @kiln-js/adapter-vercel | Edge output for Vercel |
| @kiln-js/cli | create, dev, build, start, doctor |
| @kiln-js/auth | Accounts, sessions, OAuth |
Start with one page
Scaffolding asks a few questions and writes the project. Then kiln dev starts the server with hot reload.
bunx @kiln-js/cli create my-app
cd my-app
bun run dev$ kiln dev
✔ Dev server ready in 412ms at http://localhost:3000
GET / 200 38ms
GET /about 200 4ms
GET /blog/hello 200 9ms
GET /api/users/1 404 2ms
changed app/pages/index.tsx (hot reload)
