Rendering modes
A page is answered in one of a few modes. The default renders on the server and hydrates in the browser. Every other mode trades something for something: less JavaScript, a faster first byte, an answer that is a file, or a page that never touches the server. This page lists the modes, when to pick each one, how to set it and what the presets and adapters do with it. Islands and streaming goes deeper on the parts that stream, defer or hydrate late.
| Mode | Set by | The server answers | The document ships |
|---|---|---|---|
| SSR | the default | rendered HTML | the app entry and the page chunks |
| Streamed SSR | rule stream: true, the default except in Vue | the head first, the body as it renders, the payload after | the same |
| SPA | ssr: false | the shell | the same |
"data-only" | ssr: "data-only" | the layouts rendered, the page's place empty or pending, its data in the payload | the same |
| Prerendered | rule prerender: true | a file written at build | the same as SSR |
| Cached | rule swr or cache | one finished answer, kept | the same as SSR |
| Script-free | scripts: false | HTML, sent whole | no script |
| Island page | meta.hydrate: "islands" | HTML with <hamur-island> elements | the island runtime and the islands it uses |
| Server-only page | meta.serverOnly: true | HTML that is never hydrated | the app entry without the page's code |
| Partial prerendering | rule prerender: "shell" | a stored shell at once, then the holes | the same as SSR |
A switch that holds for a path is a route rule. A page sets a rule for itself in meta.rules, as prerender and swr are set. Only what the build must know about one module to split its code has a page meta key of its own: hydrate and serverOnly. The rules of this page, in one config:
import { defineConfig } from "hamur"
export default defineConfig({
framework: "react",
experimental: { islands: true },
app: { htmlLimitedBots: /facebookexternalhit|Slackbot|Twitterbot/i, streamTimeout: 5000 },
routeRules: {
"/report/**": { stream: false },
"/account/**": { ssr: false },
"/docs/**": { prerender: true, ensureStatic: true },
"/news/**": { swr: 60 },
"/legal/**": { scripts: false },
"/product/**": { prerender: "shell" },
},
})
Which mode to pick
- Start with SSR. It works everywhere and needs nothing.
- The page waits for one slow value: stream it and defer the value. The rest of the page is not held back.
- The content is the same for everyone and changes rarely: prerender it. If it changes often but not per visitor, cache it with
swr. - The page needs a browser API and has nothing to show without it:
ssr: false. - The page's data is fetched on the server but its markup depends on the browser:
"data-only". - The page is text and needs no JavaScript: script-free, or an island page when a button or two must work.
- The page is a static shell around one personal or slow part: partial prerendering, or a server island on a cached page.
Server-rendered pages
The server runs the load of the page and its layouts, renders the tree with the adapter's library, writes the data into the payload and sends the document. The browser hydrates it and reads the same data from the payload, so no load runs in the browser for the first paint.
export function load() {
return { name: "Lamp", price: 24 }
}
import { useData } from "@hamur/adapter-react"
export default function Product() {
const { name, price } = useData<{ name: string; price: number }>()
return (
<main>
<h1>{name}</h1>
<p>{`$${price}`}</p>
</main>
)
}
Pick it when the page depends on the request or on fresh data. Every preset runs it, and on static it is the page written at build.
Streamed pages
A streamed answer leaves the server while the page still renders. The document starts with the head, then the body as the adapter writes it, then the payload and the entry script, then one resolve script for each deferred value as it settles. A page streams when its load returns a promise at the top level, and the Await of the page shows a fallback until it arrives.
const fetchReviews = (): Promise<string[]> =>
new Promise((resolve) => setTimeout(() => resolve(["Bright", "Sturdy"]), 300))
export function load() {
return { title: "Lamp", reviews: fetchReviews() }
}
import { Await, useData } from "@hamur/adapter-react"
export default function Streamed() {
const { title, reviews } = useData<{ title: string; reviews: Promise<string[]> }>()
return (
<main>
<h1>{title}</h1>
<Await resolve={reviews} fallback={<p>Loading reviews</p>} error={(error) => <p>{error.message}</p>}>
{(list) => (
<ul>
{list.map((text) => (
<li key={text}>{text}</li>
))}
</ul>
)}
</Await>
</main>
)
}
Streaming is on in react, preact, solid, svelte and html. Vue sends a string unless the page opts in with meta.rules: { stream: true }. A rule stream: false turns it off for a path in every adapter. The status and headers are those set before the first byte: a setResponseStatus or a redirect from a useAsyncData after that is dropped, with a dev warning.
Some answers are sent whole whatever the rule says: HEAD, a path under swr or cache, the prerender, a request of an HTML-limited bot, a script-free page, a hash-mode CSP page and the answer of a form post that restores its fields. A sent-whole answer waits for every deferred value first, so the HTML holds the values. Streaming per adapter lists what each library does.
Single-page app pages
With ssr: false the server sends the shell, an empty app root with the entry script, and the browser renders the page, runs its load and fills it in. Set it in the config for the whole app, in a route rule for a path, or in meta.ssr for one page.
import type { PageMeta } from "hamur/app"
export const meta = { ssr: false } satisfies PageMeta
export default function Editor() {
return <textarea defaultValue={localStorage.getItem("draft") ?? ""} />
}
Pick it for a page that cannot render without the browser, such as an editor that reads localStorage. The cost is an empty first paint. On static the shell is 200.html, and a path listed in prerender.routes is written as a full page. See Route rules.
Data-only pages
With ssr: "data-only" the server runs the middleware and every load of the chain, and puts their data in the payload. The layouts and the app root render, and the page's place renders its pending component or nothing. The browser hydrates that tree, then renders the page from the payload data. No load runs in the browser for the first paint.
export function load() {
return {
rows: [
{ id: 1, label: "Visits" },
{ id: 2, label: "Signups" },
],
}
}
import { useData } from "@hamur/adapter-react"
import type { PageMeta } from "hamur/app"
export const meta = { ssr: "data-only" } satisfies PageMeta
export default function Chart() {
const { rows } = useData<{ rows: Array<{ id: number; label: string }> }>()
return (
<ul>
{rows.map((row) => (
<li key={row.id}>{row.label}</li>
))}
</ul>
)
}
Pick it when the data must be fetched on the server (a token, a private API) but the page's markup depends on the browser, as a chart does. meta.ssr also takes a function, (ctx: { params, query }) => boolean | "data-only", run on the server for each request. The order is true, then "data-only", then false: a page less strict than its parent is raised to the parent's value, with one dev warning, and meta.ssr: true never relaxes a rule or the config. A script-free page has no client to finish a data-only page, so it renders in full.
Prerendered pages
A prerendered page is written as a file at build and served as a file, so the answer needs no render. prerender: true in a rule, or meta.rules: { prerender: true } on the page, marks it. hamur generate and hamur build write what the server answers for each seed path.
import type { PageMeta } from "hamur/app"
export const meta = { rules: { prerender: true } } satisfies PageMeta
export default function About() {
return <h1>About us</h1>
}
Pick it for a page that is the same for everyone. Every preset writes the file; on static every page without params is prerendered. A prerendered page cannot read the request. Add the rule ensureStatic: true and the build fails when a load, or a handler it calls, reads a cookie, a header, the session or the client address: app/pages/x.load.ts reads the cookie "x"; a static page cannot read the request. hamur dev logs the same line as a warning. See Route rules for the seeds, the reports and the redirect pages.
Cached pages
A rule swr or cache keeps one finished answer of a path and serves it to the next requests. A cached answer is sent whole, so it is never streamed.
import type { PageMeta } from "hamur/app"
export const meta = { rules: { swr: 60 } } satisfies PageMeta
export default function News() {
return <h1>Latest news</h1>
}
Pick it for a page that changes but not per visitor. The answer is shared, so it cannot hold anything of one visitor: put a personal part in a server island. On static the rule is dropped and listed, since no server answers. See Route rules and Storage and cache.
Script-free pages
scripts: false, as an app option, a route rule or meta.scripts, sends HTML and CSS only: no import map, entry, modulepreload, payload or csp-nonce meta. A page that sets meta.scripts and meta.rules.scripts fails the scan: set one of them. app.scripts: false skips the client build in every adapter.
import type { PageMeta } from "hamur/app"
export const meta = { scripts: false } satisfies PageMeta
export default function Terms() {
return <h1>Terms of use</h1>
}
Pick it for a page that is only text: terms, a receipt, a printed report. A script-free page is sent whole and waits for its deferred values. A link to or from it is a document load. An Island, ServerIsland, lazy component or ClientOnly on it renders its server HTML or its fallback and nothing more.
Island pages
An island page renders on the server like any page, but its document ships no app entry. Only the components you mark as islands hydrate, each as a root of its own, so a text page with one interactive button ships the button's code and a small runtime. It needs experimental.islands.
import { useState } from "react"
export default function Counter({ start }: { start: number }) {
const [count, setCount] = useState(start)
return (
<button type="button" onClick={() => setCount(count + 1)}>
{`Likes ${count}`}
</button>
)
}
import { Island } from "@hamur/adapter-react"
import type { PageMeta } from "hamur/app"
import Counter from "../islands/Counter"
export const meta = { hydrate: "islands" } satisfies PageMeta
export default function Post() {
return (
<article>
<h1>A post</h1>
<p>Only the button below ships JavaScript.</p>
<Island component={Counter} props={{ start: 2 }} on="visible" />
</article>
)
}
Pick it for content with a few interactive parts. A navigation from or to an island page is a document load, unless a swap takes it. See Islands.
Server-only pages
A server-only page renders on the server and never hydrates. Its code and its imports are left out of the client build, and the layouts and the app root still hydrate, so a navigation to it keeps the layout's state.
import type { PageMeta } from "hamur/app"
export const meta = { serverOnly: true } satisfies PageMeta
export default function Terms() {
return (
<article>
<h1>Terms</h1>
<p>This page is rendered on the server and never hydrated.</p>
</article>
)
}
Pick it for a long page whose code you do not want to ship, such as a legal page or a rendered Markdown document. A client navigation fetches the target URL as a document and takes the page's markup from it, so middleware, route rules and caches apply as on a reload, and a prerendered server-only page works on static. See Server-only components and pages.
Partial prerendering
A rule prerender: "shell" renders the page once at build with every deferred value and server island as its fallback. That is the shell. At request time the server sends the stored shell as the first chunk, with no render, then fills the holes of this visitor in the same answer: the resolve script of each deferred value and the HTML of each server island. It needs experimental.islands.
const fetchStock = (): Promise<number> => new Promise((resolve) => setTimeout(() => resolve(12), 300))
export function load() {
return { name: "Lamp", stock: fetchStock() }
}
import { Await, ServerIsland, useData } from "@hamur/adapter-react"
import type { PageMeta } from "hamur/app"
export const meta = { rules: { prerender: "shell" } } satisfies PageMeta
export default function Product() {
const { name, stock } = useData<{ name: string; stock: Promise<number> }>()
return (
<main>
<h1>{name}</h1>
<Await resolve={stock} fallback={<p>Checking stock</p>}>
{(left) => <p>{`${left} left`}</p>}
</Await>
<ServerIsland name="Cart" fallback={<p>Loading your cart</p>} />
</main>
)
}
Pick it for a page whose frame is static and whose parts are personal or slow. What the page shows outside its holes is the build's, so add ensureStatic: true to check it. The answer is always cache-control: private, no-store, since it holds a visitor's holes.
- Presets.
bun,node,cloudflare,vercelandnetlifykeep the shell in the server bundle and stream the holes. Onvercelandnetlifythe build also writes the platform's no-store headers for the path.staticwrites the shell as the page file with no_payload.json, and the browser runs the route'sloadand fills each deferred hole; a server island on that page fails the build:Server islands need a server preset (page /x uses Cart). - Adapters. Only react can build a shell today. A shell needs an
Awaitthat writes a held value as its fallback, and the others do not yet. In any other adapter the build fails:routeRules["/x"].prerender: "shell" needs an adapter whose Await writes a held value as its fallback. - Refused. A shell with
server.cspon a server preset, a script-free page, a document template whose body marker is not followed by</body>, and a rule with its owncache-controlall fail the build with a message that names the path. - Not built. The
render:streamhook does not run on a stored shell. A server island'sheadand cookie writes are dropped in a hole, since the headers are sent. A path with a query string is rendered for its request. An htmlAwaitpart and a deferred value on an island page keep their fallback.
What each preset supports
| Mode | bun, node | cloudflare, vercel, netlify | static |
|---|---|---|---|
| SSR, streamed SSR | rendered for each request; streamed as it renders | the same: the function or Worker streams | pages are written at build; nothing streams |
| SPA | the shell | the same | the shell is 200.html; a path in prerender.routes is written in full |
| Prerendered | files written at build | the same | the whole app is files |
Cached (swr, cache) | the server keeps the answer | the function keeps it | dropped and listed |
| Server-only page | rendered for each request | the same | works when the page is prerendered, since its document is a file |
| Server islands | the endpoint /_hamur/islands/<name> | the same | the build fails |
| Partial prerendering | shell in the server bundle, holes streamed | the same | the shell is the file, the browser fills the holes |
What each adapter supports
| react | preact | vue | svelte | solid | html | |
|---|---|---|---|---|---|---|
| Streaming | streams | streams, boundaries in the order they settle | opt-in with stream: true | one chunk | streams | one chunk |
Await shows its fallback in the stream | yes | yes | no, the stream waits in order | in the HTML; the browser renders the value (with experimental.svelteAsync, the value is in the HTML) | yes | yes, as a placeholder the core swaps |
| Lazy hydration | yes | yes | yes | yes | yes | no, on is ignored |
| Server-only components and pages | yes | yes | yes | yes | yes | yes |
| Island pages and server islands | yes | yes | yes | yes | yes | yes |
| Partial prerendering | yes | no | no | no | no | no |
An html app can also host islands of a second library; see Islands.