hermet hermet SSR. Islands. Rust.

How it works

For example, take a page that shows the text of a statute together with the user's favorites. When the browser opens the page, this is what happens.

  1. For requests to images, JavaScript and other files in the public directory, Cloudflare's Static Assets returns the file directly. The Worker does not run for these responses.
  2. A request for a page goes to the Worker's entrypoint called the Gate. The Gate checks the login state and whether the user may view the page. When it needs the shared HTML, it uses the URL path (for example /laws/kenpo) to ask the Renderer for it.
  3. If Cloudflare's Workers Cache has a response that can be reused, that response goes back to the Gate (a cache hit). If the page is cached but no response is stored yet, the Renderer runs the page's code, and the HTML with the layout and the body together is stored in the cache.
  4. On a page with registered Server Islands, the Gate starts rendering the per-user parts while the shared HTML is being prepared.
  5. The Gate sends the shared HTML, and then the HTML of each finished Server Island, in the same response. In the browser, Hermet's JavaScript shows each part in its place.

This is the flow for a cached page that has a Server Island.

The browser opens a page
  ↓
The Gate receives the request
  ├─ Shared HTML → check Workers Cache
  │                ├─ found: reuse it
  │                └─ not found: the Renderer renders it
  └─ At the same time, render the Server Island
  ↓
The Gate sends the shared HTML, then the Server Island,
to the browser in the same response
The Gate and the Renderer are two entrypoints of one Worker. Only the Gate can be reached from outside.

The Gate is the Worker's public entrypoint, which receives requests from browsers. The Renderer is called only by the Gate. It renders the shared HTML when the cache has no response it can use. No public URL reaches it directly.

In the Worker's configuration, the cache is off for the Gate (exports.default), which receives public requests, and on for the Renderer (exports.Renderer), which returns the shared responses. Hermet generates this configuration. It also generates the location of the file the Worker starts from (main) and, when needed, the location of the public directory.

A Server Island that finishes early still waits until the shared HTML has been sent. If the Renderer returns anything other than 200, such as 404, the Gate sends no Server Island HTML and returns the Renderer's response as it is.

Four roles

Hermet's work is split into four roles, by when and where each one runs.

RoleWhere and when it runsWhat it does
Builder (hermet build) At build time, before you deploy (locally or in CI) Checks the HTML, CSS, JavaScript and configuration, collects the CSS, extracts the Client Island JavaScript, and builds the CSP. Static generation also happens here.
Gate In the Worker, on every request (cache off) Checks login and who may view the page; handles redirects, forms, APIs and webhooks; calls the Renderer; renders and sends Server Islands.
Renderer In the Worker, behind Workers Cache. Does not run on a cache hit Renders the shared HTML. Also checks that the Server Island placeholders match the registrations and that {hermet:head} is present. If not, it returns 500 and stores nothing. Purges the cache when the Gate asks.
Patcher (hermet-patch) In the user's browser Puts the Server Island HTML that arrives later into its placeholder on the page (data-island).

This table compares the Gate and the Renderer.

AspectGateRenderer
How it is called Receives requests from browsers (except files that Static Assets returns directly) Called only by the Gate. No public URL reaches it directly.
Cache Not used (runs every time) Used (on a cache hit, its code does not run)
What it receives Cookies, the session, the URL query string, form and API input, and so on Only the URL path. When a page has separate HTML for each member type, it also receives the name of that member type.

The session and cookies never reach the Renderer. This makes it hard for personal data to get into the HTML in the shared cache.

Why a small piece of JavaScript puts Server Islands in place

The Gate sends the shared HTML first, and the Server Island HTML after it. hermet-patch, about 1 KB of JavaScript, moves the HTML that arrives later into its placeholder. {hermet:head} writes it directly into <head> (the CSP allows it by its hash).

There are ways without JavaScript, but each one has its own problem. If the Gate embeds each Island at its placeholder, in page order, before it sends the page, a slow Island makes the rest of the page after it wait too. If the Islands go in through <slot> elements of Shadow DOM, the whole page sits inside a Shadow DOM. Then site-wide CSS, links within the page (#id) and other features do not work as you expect.

hermet new creates src/lib.rs, which defines both the Gate and the Renderer. src/manifest.rs declares what happens for each URL, and both entrypoints pass its app() to Hermet.

src/lib.rs
#[event(fetch)]
async fn gate(req: Request, env: Env, ctx: Context) -> Result<Response> {
    hermet::gate(manifest::app(), req, env, ctx).await
}

#[event(fetch, entrypoint = "Renderer")]
async fn renderer(req: Request, env: Env, ctx: Context) -> Result<Response> {
    hermet::renderer(manifest::app(), req, env, ctx).await
}