Skip to content

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.

The file tree is the router
Files in app/pages and the URLs they serve
FileURLMode
app/pages/index.tsx/universal
app/pages/about.server.tsx/aboutserver
app/pages/dashboard.client.tsx/dashboardclient
app/pages/blog/[slug].tsx/blog/:sluguniversal
app/pages/docs/[...path].md/docs/*pathuniversal

Pick a mode per page

The file name sets the mode. Compare the three side by side.

ModeExample fileServer outputJavaScriptUse it for
universalindex.tsxFull pageHydrates the HTMLMost pages. Readable before JavaScript loads, interactive after.
serverabout.server.tsxFull page, streamedNone, unless islandsContent with no interactivity, such as legal and marketing text.
clientdashboard.client.tsxEmpty shellRenders in the browserScreens 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:title tag.
  • Layouts wrap every page in their folder and below.
app/pages/blog/[slug].tsx
// 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.

  1. 01

    Request

    The server receives a URL and matches it against the route table.

  2. 02

    Loader

    If the page exports a loader, it runs on the server with params and the request.

  3. 03

    Render

    React renders the page to HTML. Server pages stop here.

  4. 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.

PackageWhat it does
@kiln-js/coreRoute matching, titles, env validation, errors
@kiln-js/serverRequest handler, API dispatch, CSRF and CORS
@kiln-js/clientHydration, navigation, Link
@kiln-js/mdMarkdown pages with styled tags
@kiln-js/vite-pluginRoute table, server-code removal, islands
@kiln-js/adapter-vercelEdge output for Vercel
@kiln-js/clicreate, dev, build, start, doctor
@kiln-js/authAccounts, sessions, OAuth

Start with one page

Scaffolding asks a few questions and writes the project. Then kiln dev starts the server with hot reload.

terminal
bunx @kiln-js/cli create my-app
cd my-app
bun run dev
my-app
$ 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)

Next