Skip to content

Rendering modes

Every page in Kiln has a render mode. The mode decides whether the server renders HTML, whether the browser runs the component, and whether the browser needs any JavaScript for that page. You choose the mode with the file name.

The three modes

ModeFile nameServer HTMLBrowser JavaScriptWhen to use it
universalabout.tsxFull pageHydrates the HTMLMost pages. Readable before JavaScript loads, interactive after.
serverabout.server.tsxFull page, streamedNone, unless the page has islandsContent with no interactivity: legal pages, marketing text, documentation.
clientdashboard.client.tsxAn empty shellRenders the page from scratchScreens that depend on browser-only state, such as a canvas or a window size.

The default mode is universal. A file with no suffix is universal.

Universal pages

A universal page runs twice:

  1. On the server, when the request arrives. The loader runs, React renders the page to a string, and the string is sent in the response with the loader's data in a boot script.
  2. In the browser, after the HTML loads. The client entry loads the same component, and React attaches event handlers to the existing HTML. This is called hydration. The page is not rendered again from scratch.

Because of this, a universal component must render the same output on the server and in the browser for the same data. Anything that differs, such as the current time or a random number, can cause a hydration mismatch. Use ClientOnly or useIsClient for those values. See @kiln-js/core.

tsx
// app/pages/index.tsx
import { ClientOnly } from "@kiln-js/core";

export default function Home() {
  return (
    <main>
      <h1>Welcome</h1>
      <ClientOnly fallback={<p>…</p>}>
        <p>Your local time is {new Date().toLocaleTimeString()}</p>
      </ClientOnly>
    </main>
  );
}

Server pages

A .server page renders on the server and sends no page JavaScript. Its loader and components never reach the browser bundle. Kiln compiles any import from a .server file out of client code, and fails the build if client code imports one.

tsx
// app/pages/terms.server.tsx
export default function Terms() {
  return (
    <article>
      <h1>Terms of service</h1>
      <p>…</p>
    </article>
  );
}

Server pages are streamed. The server sends the document shell first, and then the rest of the page as React renders it. This matters when a page waits on slow data, because the visitor sees the layout early. See Streaming.

A .server page with islands still gets the client script, but only the islands hydrate. The rest of the page stays static.

Client pages

A .client page sends an empty <div id="root"> and boot data with no loader data. The browser loads the component and renders it with createRoot. Nothing is rendered on the server, so the page works only in the browser.

tsx
// app/pages/dashboard.client.tsx
export default function Dashboard() {
  const width = window.innerWidth;
  return <p>Viewport width: {width}px</p>;
}

Client pages can be reached by a client-side link from another page, or by a direct visit. A direct visit to a client page loads the shell first, then the page appears.

Client pages are excluded from the test runner, because they have no server output to check. The CLI kiln test skips them.

Mode and layouts

Layouts follow the page's mode. A .server page uses the layouts above it, and those layouts render on the server. A layout that uses browser-only APIs must be safe in the mode of every page that uses it. If you need browser-only code in a layout, wrap that part in ClientOnly.

Mode and navigation

Client-side navigation through Link behaves like this:

  • Moving to a universal or client page loads that page's code and loader data, then renders it in the browser.
  • Moving to a .server page is not possible in the client router. The browser does a full page load to fetch the server HTML. Kiln handles this for you.

Choosing a mode

Start with universal. Move a page to .server when it has no interactivity and you want the smallest possible JavaScript. Move a page to .client only when the server cannot render it, because it reads browser state on the first render.

The generator and kiln create accept --mode or ask for it:

bash
bunx kiln generate page pricing --mode server

Summary

  • The mode is part of the file name: name.server.tsx, name.client.tsx, or name.tsx.
  • Universal pages render on the server and hydrate in the browser.
  • Server pages render on the server and send no page JavaScript.
  • Client pages render only in the browser.
  • Islands let a server page include small interactive parts. See Islands.