Cloudflare Workers, static
--preset cloudflare-static builds an app that is only files, prerendered, for Cloudflare Workers static assets: .output/public/ with _headers, _redirects and 404.html, and .output/wrangler.json with the assets block for it. There is no Worker. The build selects it for hamur generate on Workers Builds (WORKERS_CI), and --preset cloudflare-static names it. Deploy with wrangler deploy --config .output/wrangler.json; the build ends by printing the command.
Cloudflare's own default for a page written as about/index.html is to redirect /about to /about/, one more round trip on every first load. The preset writes assets.html_handling from app.trailingSlash, so the page is answered at the form the app links to:
app.trailingSlash | assets.html_handling | The link is | The other form |
|---|---|---|---|
"ignore", "redirect" | "drop-trailing-slash" | /about | /about/ redirects to /about (307) |
"always" | "force-trailing-slash" | /about/ | /about redirects to /about/ (307) |
"strict" | "drop-trailing-slash" | /about | /about/ redirects to /about (307) |
"strict" is the one that differs from the server presets: there /about/ of a prerendered page is a 404, but Workers assets answers the file before Hamur sees the request and can only redirect or serve nothing, so the form is canonical by redirect. A path with no file is a 404 from 404.html.
Reference
| Preset | cloudflare-static |
| Runs on | Cloudflare Workers static assets |
event.runtime.name | static |
| Server target | bun |
| Lifetime, startup | process, load |
Output
.output/
hamur.json
wrangler.json your wrangler config with the keys below
public/ the prerendered pages, 404.html, _headers and _redirects
Config keys
This preset has no key of its own; the prerender keys are listed under Static. cloudflare.env selects an environment of your wrangler config, as on cloudflare.
Your wrangler config
Put name, routes, vars and the rest in a wrangler.json, wrangler.jsonc or wrangler.toml in the app root, and the build copies them into .output/wrangler.json. Never edit that file: every build rewrites it from your config, never from the previous output.
{
"name": "my-site",
"routes": [{ "pattern": "example.com", "custom_domain": true }],
"vars": { "ENVIRONMENT": "production" },
}
The preset owns main (never written: there is no Worker), compatibility_date (as on cloudflare) and assets, written as { html_handling, not_found_handling: "404-page", ...your assets keys, directory: "public" }. assets.binding and assets.run_worker_first are not written, since nothing runs. Your own not_found_handling wins, for example "single-page-application" for an app with hash history, and so does your own html_handling. directory is always "public", relative to .output/wrangler.json; another value is replaced and the build warns: assets.directory ".output/public" in the app's wrangler config is replaced by "public".
A kept html_handling that redirects the link, or serves no file at it, warns once:
assets.html_handling "auto-trailing-slash" in the app's wrangler config makes Cloudflare redirect /about to /about/ (307), while app.trailingSlash "ignore" links to /about; the preset would write "drop-trailing-slash", so remove the key to get it
The effect part is redirect /about to /about/ (307), redirect /about/ to /about (307) or serve no file at /about. There is no warning for the value the preset would write, nor for "auto-trailing-slash" with prerender.format: "file" (the page is about.html, which that mode answers at /about) unless app.trailingSlash is "always".
hamur build --preset cloudflare-static
bunx wrangler deploy --config .output/wrangler.json
Detection
None of its own. hamur generate picks it when the build selects cloudflare (WORKERS_CI is set); --preset cloudflare-static names it.
Deploy command
hamur deploy builds, then runs wrangler deploy --config .output/wrangler.json; with --preview it runs wrangler versions upload --config .output/wrangler.json. See hamur deploy. The preview command of the preset, which hamur preview runs or prints, is bunx wrangler dev --config .output/wrangler.json.
Limits
- Does not run: WebSockets and
server.syncedState,server.pubsub, streamed answers,server.metrics, image transforms at run time, privateapp/env.tsentries read at run time, Durable Object classes. - It is the
staticoutput: server routes,proxy,cors,swrandcacherules andserver.fallbackProxyare dropped with a warning._headersand_redirectscarry the headers and redirect rules, within Cloudflare's limits (the build warns past them).
The levels are the features of the preset: hamur build warns or fails when the app uses a feature the preset does not run (see Preset features).