hermet hermet SSR. Islands. Rust.

Components that run in the browser

A Client Island adds behavior in the browser, for example to a button that changes a quantity, or to a form that submits without reloading the page. This component uses a browser feature called Custom Elements. You write it as your own HTML tag, such as <x-buy>.

How to write one

In src/components/, create <name>.html and write the HTML and one <script type="module"> in it. A <script> is allowed only in the HTML files in src/components/, and only one per file. Here is "Add to cart" from the shop sample.

src/components/buy.html
<x-buy>
  <form method="post" action="/cart/add">
    <input type="hidden" name="product_id" value="{id}">
    {counter}
    <button type="submit">Add to cart</button>
  </form>
</x-buy>
<script type="module">
customElements.define("x-buy", class extends HTMLElement {
  connectedCallback() { /* on submit, fetch("/api/cart", …); without JS, the form submits normally */ }
});
</script>
src/components/mod.rs and src/pages/product.rs
pub struct BuyForm {
    pub id: String,
    pub counter: hermet::Markup,   // another Client Island (the quantity counter) nested inside
}
hermet::template!(BuyForm, "components/buy.html");

// put it into the page view as Markup (product.html contains {buy})
let buy = Markup::of(&BuyForm { id: p.id.clone(), counter: Markup::of(&CounterView { label: "Quantity" }) });

How it differs from a Server Island

  • A Client Island is not registered anywhere: not in the manifest, not in a template's islands { … }. You put the component's HTML into the page's template as Markup. It is rendered together with the page: through the Renderer when the page is rendered per request, or at build time with static generation.
  • Values you put into the shared HTML are shared by everyone who views the same page. Pass per-user values through a Server Island (see Showing per-user content) or through an API that answers per user (see Forms, APIs, and cache purge).

Names and CSS

  • You choose both the element name (x-buy) and the file name (buy.html). The name passed to customElements.define must match the element name.
  • A custom element is the recommended form; the build does not enforce it. The reason to use one: the browser starts it for every such element, whenever the element enters the page. A script that runs once (with DOMContentLoaded and document.querySelector) reaches only the elements that are in the page at that moment. An element added later (for example, one that another Client Island adds by cloning a <template>) gets no behavior.
  • With <template shadowrootmode="open">, its CSS does not leak into the page (Shadow DOM).

How the JS is served

At build time, the JavaScript of a Client Island is written to .hermet/public/assets/, and Static Assets serves it. In a site configured with cloudflare.rs, Hermet finds the Client Islands and adds the public directory to the configuration itself. You do not need to write anything.

  • The file name contains a short hash computed from the file's content. Browsers are told to cache the file for up to one year. When you change the JavaScript, the new HTML refers to the new file name.
  • A page opened just before a deploy may not find the old JS and get a 404. Only that page's Client Islands stop working, and reloading the page fixes it.

Rules for writing the JS

  • Avoid code that makes the browser parse a string as HTML, such as assigning to innerHTML. Hermet's checks and Trusted Types in supporting browsers restrict such assignments. Trusted Types is part of CSP (Content Security Policy), which tells the browser what a page is allowed to do.
  • Create elements by cloning a <template> rendered by the server, and set values through textContent or attributes.
Inside the component's <script>
const item = template.content.firstElementChild.cloneNode(true);
item.querySelector("a").textContent = heading.textContent;
item.querySelector("a").setAttribute("href", "#" + heading.id);
list.replaceChildren(item);
Combining with a Server Island

For a component that shows a per-user value and also runs in the browser, the Server Island's HTML renders the element, and a <script> defines the behavior of that same element. You can write the <script> in the Server Island's own file, after its HTML. The cart count in the shop (src/components/cart.html) is an example. Changing a value from JS does not update other Islands that show the same value, so the Islands tell each other with an event such as cart:changed.

A Server Island's HTML arrives after the page and is then moved to its place in the page. When an element is moved, the browser may call its connectedCallback again, so register listeners for events such as clicks only once (the shop sample checks this with data-ready).

A component's <script> becomes a public file that anyone can read

The build takes a component's <script> out of the file and turns it into a public JavaScript file. This file is loaded on every page and is also sent to people who are not signed in. The same is true when you write it in a Server Island's file (only the HTML part is per-user). Do not put secrets or members-only logic in it.

A render program in a separate package, such as examples/guide/render, cannot write out this JavaScript yet (the build stops with static_site.client_island).