Static files and static generation
Images, fonts, and HTML that you made as files in advance can be served from Cloudflare Static Assets. Requests for these files are answered without going through the Worker.
Images and fonts (static/)
static/
fonts/serif-0.woff2
img/logo.png- Put only files that anyone may see here. They are served without going through the Gate, so do not put members-only images here.
- Only images (png, jpg, webp, avif, gif, ico, svg) and fonts (woff2, woff) are allowed.
- In templates, use
hermet::static_url!("img/logo.png"). A missing file is a compile error. - Fonts are referenced in
global.css, inside@font-face, for exampleurl("fonts/serif-0.woff2"). - Hermet adds the public directory to the configuration by itself. After you create or remove the
static/orpublic/folder itself, build withhermet build,hermet devorhermet deploy. If you usecargo buildor a similar command directly, the build may not notice a new folder until the next change to the sources. - Files that you want at a fixed URL, such as
/favicon.icoor/robots.txt, go inpublic/(files instatic/get a hash in their names, so their URLs change).
Files at a fixed URL (public/)
public/
favicon.ico → /favicon.ico
robots.txt → /robots.txt
.well-known/security.txt → /.well-known/security.txt- As with
static/, these files are served without going through the Gate. Put only files that anyone may see here. - Only images, a Web App Manifest (
.webmanifest), plain text (.txt) and XML (.xml) are allowed. Write the extension in lowercase. HTML, JS, CSS and JSON are not allowed. Fonts go instatic/. At most 30 files are allowed. - The names have no hash, so the browser asks again each time. Put an image that only your own pages reference in
static/. - If you register a page at the same URL as a file in
public/, the Worker stops at startup. - The example is
examples/min-form.
For audio and video, form submissions to other sites, and similar cases, declare them in Cargo.toml, under [package.metadata.hermet], and they are added to the CSP (for example media = ["self"]). Without the declaration, the browser blocks them. In public/, images and a Web App Manifest (.webmanifest) need no declaration: Hermet adds them to the CSP.
When and where HTML is rendered
For a page that shows the latest data in D1, the Worker renders the HTML on request and uses the cache as the route specifies. For a page whose content is fixed when you deploy, such as a written article, the build can make the HTML file. This is called static generation (prerendering).
| On request (the usual case) | At build time (static generation) | |
|---|---|---|
| Typical use | Pages that show database content, pages with per-user parts | Guides and articles made from text in the repository |
| How to write it | .cache(…) or .until_deploy() on the route |
cloudflare.rs: .build(Build::prerender()); Cargo.toml: native; a render program; the route's .prerender(…) |
| Served by | The Worker. Whether the cache is used depends on the route | Static Assets. It returns the HTML file directly |
| Server Islands, members-only pages | Supported | Not supported |
| Client Islands | Supported | Supported |
| Node.js and Wrangler | Not needed | Not needed |
| Sample | examples/shop, examples/law |
examples/guide, examples/prerender-worker |
If the render program is in a package of its own (examples/guide), Client Islands run on the generated pages, but the render package cannot emit their JavaScript. Images and fonts in static/ are not supported in the render package yet either.
Render at build time (static generation)
hermet build first builds the Worker. Then it makes the HTML files with a Rust program that the site provides. The word native means that this program runs directly on the build machine. Hermet checks the generated HTML and applies it to the public directory.
Below is an example setup with the render program inside my-site. You write in the manifest which pages to generate. For working examples, see examples/prerender-worker and examples/status.
Choose static generation in the cloudflare.rs configuration.
fn default(_: &Ctx) -> Result<Config, ConfigError> {
Ok(Config::new()
.compatibility_date("2026-11-13")
.build(Build::prerender())
.worker(Worker::new("my-site")))
}In Cargo.toml, use native to name the package that holds the program that makes the HTML. If the program is in your site's own package, use the same value as the [package] section's name. The [lib] below updates your existing one. Do not write the same section heading twice.
[lib]
crate-type = ["cdylib", "rlib"] # rlib: the render program uses the site's manifest
[[bin]]
name = "render"
path = "src/bin/render.rs"
[package.metadata.hermet.prerender]
native = "my-site" # the site's own package nameIn src/manifest.rs, add .prerender(paths) to the route to generate statically. paths is a function that returns the list of URLs to generate.
// src/manifest.rs
pub fn app() -> App {
App::new(hermet::generated!())
.page(Page::at("/notes/{slug}", pages::note::load).prerender(pages::note::paths))
}
// src/pages/note.rs
pub fn paths() -> Vec<String> {
vec!["/notes/hello/".into(), "/notes/second/".into()]
}
pub async fn load(params: RouteParams) -> Result<Option<Note>> { // called at build time, once per URL of paths()
Ok(find(params.get("slug").unwrap_or_default()))
}src/bin/render.rs is just the one line below. It walks the manifest. For each page with .prerender(…), it calls that page's load for each URL and writes the HTML (my_site is the package name my-site with - replaced by _).
hermet::render_main!(my_site::manifest::app);- The same package is built twice: for the Worker and for your machine. Even so, you write the manifest, the pages'
loadand the Worker's entry point (#[event(fetch)]) only once, and you do not need to split them with#[cfg(target_arch = "wasm32")]. Keep theworkerdependency in[dependencies]too. - In
src/lib.rs, changemod manifest;topub mod manifest;so that the render program can use it. - The render program uses the manifest's list as it is, so the routes and the generated files cannot differ. If a request for a URL that was not generated reaches a prerendered route, the Gate returns 404.
- The generation stops and names the URL when a URL in the list does not match the route's path, when another route answers that URL (for example a page at
/notes/searchbeside/notes/{slug}), or whenloadreturnsNoneor an error. - A
loadcalled at build time cannot takeShared. It cannot use the time (worker::Date::now()), random numbers orfetcheither. The build machine has no D1 or KV, so build the page from what is deployed with the code (constants,include_str!and the like).
hermet buildneeds no Node.js, no Wrangler and no Cloudflare token. If your templates include Client Islands, it also outputs their JavaScript and the CSP that allows the browser to run it.- HTML served this way is a public file. A page that needs a member check at request time or a Server Island must be a route that the Worker answers.
- Hermet checks the HTML's CSP and the public files it references. If generation fails, Hermet restores the public directory to its previous state and aborts the deploy. No build record is written for this build either, so
deploy --no-buildrefuses the output of the failed build. - On Cloudflare,
_headers, which sets response headers, is limited to 100 rules. If there are more rules than that, Hermet collapses the rules that meet certain conditions. If there are still too many, Hermet shows an error. - You can also put the render program in a package of its own. For example,
examples/guidekeeps the code that converts Markdown to HTML in a separate package.
native lineIf you use Build::prerender(), native is required. Without it, hermet check and hermet build report site.prerender_native_missing. Follow its guidance and write the render program's package name in Cargo.toml.