Fondu

Deploying

View as Markdown

Node

npm run build    # vite build
npm start        # node --env-file-if-exists=.env dist/server/index.js

One vite build writes the browser's files to dist/client, then the server to dist/server (its entry: dist/server/index.js, minified), then the static pages if any; the dependencies stay in node_modules, so deploy them with it. The server sets NODE_ENV to production when it is unset, so React uses its production build (a value you set is kept). PORT sets the port (3000 by default). On SIGTERM or SIGINT the server stops taking requests and waits for the running ones, up to 10 seconds.

Bun

The same build, with fondu({ target: 'bun' }); then NODE_ENV=production bun dist/server/index.js: Bun reads NODE_ENV as it starts, and the built server only sets it for React. Bun serves the app itself, on PORT too.

Cloudflare Workers

npm run deploy   # vite build && wrangler deploy

wrangler.jsonc's main is ./app/server.ts, the Worker. One vite build writes the browser's files and the Worker, minified as Workers start faster with less code to parse (build.minify: false in the Worker's Vite environment keeps it readable). wrangler deploy ships both: the files are served by Workers Static Assets.

Bindings (D1, KV, R2…) and variables are declared in wrangler.jsonc, secrets in .dev.vars locally and with wrangler secret put deployed. Server code reads them all from env, typed by npm run cf-typegen (worker-configuration.d.ts; c.env is untyped):

import { env } from 'cloudflare:workers'

const user = await env.DB.prepare('select * from users where id = ?').bind(id).first()

With static pages, the pages chosen are files too.

The Content Security Policy

Fondu's policy allows only the site's own scripts. A script that a host adds to every page is blocked until the policy allows it, such as Cloudflare Web Analytics when the zone turns it on:

// app/server.ts
import { NONCE } from 'hono/secure-headers'

createApp({
  secureHeaders: {
    contentSecurityPolicy: {
      scriptSrc: ["'self'", NONCE, 'https://static.cloudflareinsights.com'],
      connectSrc: ["'self'", 'https://cloudflareinsights.com'],
    },
  },
})

A directive given replaces Fondu's one, so keep 'self' and the nonce in scriptSrc: the nonce also allows the scripts React sends with a streamed Suspense boundary.

Pages are streamed, so a proxy in front of the app must not buffer responses for the visitor to get the first bytes early (nginx: proxy_buffering off).

Behind a proxy

A proxy that terminates TLS gives the app http:// requests, while the browser's Referer and Origin are https://. Fondu compares the Host header with them, not the scheme, and reads X-Forwarded-Proto to accept an https Origin. So the proxy must pass Host on and set X-Forwarded-Proto: https. X-Forwarded-Host is not read. Redirects Fondu makes (back to the page, the trailing slash, a new version) are paths, which resolve in the browser against the address it knows.

On Node and Bun, Fondu does not compress responses or answer conditional requests (If-None-Match, If-Modified-Since). That is the job of the proxy or CDN in front of the app.

Caching

The built assets in /assets/ (Vite's build.assetsDir) have hashed names, so they are served with a year-long Cache-Control and immutable: by the Bun and Node server, for the files it serves (a route of your own under the same URL keeps its own headers), and on Cloudflare through a generated _headers (unless public/_headers exists). Pages are not cached: Cache-Control: private, no-cache.

If you make pages cacheable, a cache must keep a document and a navigation apart. A document answers with Vary: X-Fondu, a navigation with Vary: X-Fondu, X-Fondu-Version. Some CDNs ignore custom Vary headers, so check yours before caching pages publicly.

A new version

Each build has a version. A navigation from a tab opened before a deploy gets a document load instead of the page, so the browser runs the new assets.

Last updated