Static pages
Fondu can write pages as static files at build time: static site generation (SSG). They are served without rendering them again, and navigations keep working. On Bun, Cloudflare Workers and Node; this site is built that way.
Choosing the pages
A route opts in with export const prerender; a _middleware.ts sets it for its directory and below. The nearest
setting wins. The build prerenders when any route or _middleware.ts exports prerender (even as false), and not
otherwise: there is no option in vite.config.ts, and fondu({ prerender: true }) fails with a message saying so.
// app/routes/(marketing)/_middleware.ts: every page below
export const prerender = true
// app/routes/(marketing)/contact.tsx: not this one
export const prerender = false
// app/routes/blog/[slug].tsx: a dynamic route lists its pages' params
export const prerender = () => posts.map((post) => ({ slug: post.slug }))app/routes/_middleware.ts with export const prerender = true makes every page static. Only pages are prerendered: a .ts route under it is still answered by the server, and one that exports prerender itself fails the build.
How it works
The build starts the server it built (node or bun dist/server/index.js, or the Worker in wrangler), asks it for the pages
to prerender, and writes each twice in the client build:
- as a document,
docs/intro.html, for a first visit; - as its page object,
_fondu/pages/docs/intro.json, for navigations: Fondu's client reads it instead of asking the Worker. It is asked for once the pointer rests on a link to the page, so the click has nothing to wait for.
Fondu keeps one prefix for its own URLs, /_fondu/: the page objects (/_fondu/pages/…) and, while the build runs, its list
of pages (/_fondu/prerender). An app has no route under it: one that a listed params value puts there stops the app at
startup. In development, Fondu also serves /@fondu/dev.css: the CSS the layouts import, in the document so a server-rendered page
is styled at first paint.
Then:
- On Cloudflare, Workers Static Assets serve the files without running the Worker.
- On Bun and Node, the built server serves the files before the app (Hono's
serveStatic).
The server still answers every other URL, a page that isn't static or one that doesn't exist. In development, it answers all of them.
On Cloudflare, the Worker the build starts runs locally, as in development: .dev.vars overrides the vars of
wrangler.jsonc. A static page is written with those values and deployed as it is, so a value meant for your
machine, a localhost site URL say, ends up in production. Keep what static pages show out of .dev.vars.
What a static page can be
A static page is the same for everyone. It is rendered once, with every Suspense boundary resolved, for a visitor with no cookies and no query string, shared props included. So the build stops, naming the route, when a page:
- answers with a status other than 200, such as a redirect;
- takes POSTs at its URL (an
actionoractions): Cloudflare answers those with 405 for a static file, so Fondu refuses them on every target; - has a URL ending in
index: see Limits; - lists a param value that is not a file name: one with a
/or\in it, or./..(each segment of a catch-all is one name); or two pages that would be the same file.
Headers
A static page is a file served with the headers the build decided: the security headers of the app's pages, one set
for all of them. The Content Security Policy has no nonce, as a file served to everyone can't have one: the scripts that
had it are allowed by their hashes instead, so an inline script of the document runs on a static
page too. A page object has the same headers, with X-Fondu.
The build keeps that set where the target serves the files from:
- On Cloudflare, in
_headers, which Workers Static Assets apply to static files. - On Bun and Node, in
dist/server/prerender.json, next to the built server, which reads it at start.
A new deploy
Each page records the build it belongs to. After a deploy, a navigation from a tab opened before it loads the page as a document, with the new assets.
Limits
A URL whose last segment is index (/index, /blog/index, a param's value, %69ndex included) can't be a static
page: its file would be its directory's page, index.html. The build stops if one is to be prerendered. Its route
still answers it: Fondu sends such URLs to the server on every target (on Cloudflare, it adds /index and
/*/index to the built wrangler.json's run_worker_first). routes/blog/index.tsx at /blog is not concerned.
On Cloudflare, one spelling still doesn't reach the Worker: with letters percent-encoded (/%69ndex), Static Assets
redirect the request to the directory before the Worker runs, as run_worker_first matches the raw path. A link to
it from a page still works.