Showing per-user content
Parts that differ per user, such as the cart contents or "Hello, (name)", are added as a Server Island. In the page's HTML, you prepare a placeholder (a marker in the HTML that shows where a Server Island goes, written with the data-island attribute). In the Worker, you register a function that makes the content for that place.
-
Write the placeholder in the page's HTML. Until the content arrives, the page shows what is inside this element.
src/pages/product.html (excerpt: put it inside <body>) <div data-island="cart"><p>Checking your cart…</p></div> -
Write the type, the template and the render function. In
src/components/, put<name>.rsand<name>.html.src/components/cart.rs (shortened from the shop sample) pub struct CartView { greeting: String, count: usize } hermet::template!(CartView, "components/cart.html"); pub async fn render(deps: GetDeps, session: Option<Session>, params: RouteParams) -> Result<CartView> { let greeting = match &session { Some(s) => format!("{}'s cart", s.user.id), // signed in None => "Guest cart".to_string(), // not signed in }; let count = crate::api::cart::items(deps.bindings(), session.as_ref()).await?.len(); Ok(CartView { greeting, count }) }src/components/cart.htmlshows the fields it receives from the Rust type.<p>{greeting} · {count} items</p> -
Wire it to the template. In the declaration of the template that has the placeholder, list the name and the render function. The name (
"cart") must be the same asdata-islandin the HTML. You do not write it in the manifest.src/pages/product.rs hermet::template!(ProductView, "pages/product.html", islands { "cart": crate::components::cart::render });
Some mistakes are within one template: a misspelled name in islands { … }, a placeholder with no entry, the same placeholder twice, or the same name twice. hermet check --full stops on them and shows the file and the line. A placeholder that is only inside <template> does not count. If a page and its layout have placeholders with the same name, hermet build stops.
A template with placeholders needs islands { … }. Without the block, compilation stops. You cannot embed a template with placeholders in another template with Markup::of or Markup::each. If you try, compilation stops at that line. (To put a page's view into the layout, use .layout. See the law article example.) A render function also cannot return a template that has placeholders.
You cannot put a placeholder inside another placeholder. When an Island arrives, it replaces all the content of its placeholder. So a placeholder inside it is lost, whichever of the two arrives first. If you nest them within one template, compilation stops and shows the line of the inner one. If you write {main} (the page's HTML, PageMarkup) inside a placeholder of the layout, compilation also stops, at the line of the layout's declaration. Write {main} outside placeholders.
If the data-island attributes in the HTML still do not match the registrations, the Renderer returns 500 the first time it renders that page. This keeps the page from showing "loading" forever. If you chain two documents with .map and both have an Island with the same name, that Island is registered twice on the same page. That page returns 500 without running any Island. Other pages are not affected.
What render receives
| Argument | Contents |
|---|---|
deps: GetDeps |
Connections to storage (bindings()), shared data (shared()), cookies (cookie()), and the URL of the login page (login_url()) |
session: Option<Session> |
The signed-in user (who it is, and which member type). None if no one is signed in |
params: RouteParams |
Values from the URL path ({id} and so on). The query (?sort=…) is not passed as it is. If you declare names in islands { … }, the function receives the values of only those names, as a fourth argument (see "Receiving the query" below) |
GetDeps has no operation that signs the user out or purges the cache. A GET can arrive just because someone followed a link on another site, and such a GET must not cause these things. Write operations that require the user's intent, such as purchases or contract changes, in a form or a POST API. A write such as recording "this page was viewed" is fine in a Server Island.
Receiving the query
To change an Island's content with the URL's ?sort=…, for sorting or paging for example, declare the names to receive in the page's template.
hermet::template!(TagView, "pages/tag.html", islands {
"articles": crate::components::tag_listing::render with query("sort", "page"),
});- Only the values of the declared names arrive. The Island cannot see the rest of the query.
- The shared HTML is the same whatever the query, so the cache is still used. Only the content of the Island changes.
- The function receives the query as its fourth argument. If it returns
Ok(None), the content already in the placeholder stays as it is. For the full way to write it, see "Passing the page's query to a Server Island" in the README.
Things to know
| Item | Behavior |
|---|---|
| When it runs | The Gate runs Server Islands while it is still waiting for the shared HTML. They start even when the page later turns out to be a 404. So a component that writes data must check for itself that its target exists. |
| When it fails | "Could not load." is shown in its place. The other Islands and the rest of the page are delivered as usual |
| Time limit | 5 seconds by default. You can change it with App::new(…).island_timeout(Duration::from_secs(3)). Writes that the Island already issued may still complete after the time runs out |
| JavaScript is needed to show it | The Worker sends the component's HTML later, and Hermet's JavaScript in the browser shows it in its place. If JavaScript does not run, the page shows the content you wrote in the placeholder. |
| Not cached | It runs on every request. For heavy data that people rarely open, have a Client Island call an API when it is opened |