Streaming#
Some data is slow. A report, a search over a large table, or a call to an external service can take seconds. Streaming lets the server send the page layout straight away and send the slow part when it is ready, so visitors see something sooner.
Kiln streams .server pages. The server sends the document shell as soon as React has rendered it, then sends the deferred content when each promise settles.
Deferring a value#
defer(promise) starts the work and returns an object you pass to the page. It does not wait:
// app/pages/reports.server.tsx
import { defer } from "@kiln-js/core";
export async function loader() {
return {
summary: await getSummary(), // fast: awaited normally
detail: defer(getSlowDetail()), // slow: starts now, read later
};
}defer returns a value with a read() method. Calling read() returns the result when the promise has settled, and throws the promise until then. React understands that protocol and suspends the component.
Reading a deferred value#
Use Await to read it. The child is a function that receives the value:
import { Await } from "@kiln-js/core";
export default function Reports({ data }: { data: { summary: string; detail: ReturnType<typeof defer<string>> } }) {
return (
<main>
<h1>Reports</h1>
<p>{data.summary}</p>
<Await resource={data.detail}>
{(text) => <pre>{text}</pre>}
</Await>
</main>
);
}Wrap Await in a Suspense boundary to show a fallback while the value loads:
import { Suspense } from "react";
<Suspense fallback={<p>Loading the detail…</p>}>
<Await resource={data.detail}>{(text) => <pre>{text}</pre>}</Await>
</Suspense>What the visitor sees#
- The browser receives the top of the document. Stylesheets load, and the page starts to paint.
- The server sends the rendered shell, including anything not waiting on deferred data.
- When each deferred promise settles, the server sends the content that waited for it.
If a deferred promise rejects, read() throws the error. The error reaches the nearest error boundary on the server.
The title in streams#
The page title must be known before the shell is sent. Kiln reads it from the first render. A <kiln:title> that renders immediately is used. A title that depends on deferred data cannot be known in time, so use data you awaited for the title.
Limits#
- Streaming is for
.serverpages. Universal pages are rendered to a string in one pass, soread()on an unfinished promise does not stream. Await the data in the loader if the page is universal. - The client never hydrates a streamed server page. Streamed content has no interactivity. Add islands for the interactive parts.
- Create deferred values in the loader, so each request starts its own work.
When to use it#
Use streaming when one part of a page is slow and the rest can be shown without it. Keep every other value awaited, so the page renders with complete information where it matters most.

