Where state lives
When you build an SPA (single-page application: an app that changes screens within one page) with React or a similar library, you often keep fetched data and login information in browser memory. You then keep that copy in sync with the server so that the two do not differ. Common tools for this are a client-side store such as Redux, Zustand or React Context, and a cache of data in the browser. A site built with Hermet usually does not need them. Hermet puts together what each page needs on the server, on every request.
| What | Where it lives | How the browser gets it |
|---|---|---|
| Content that is the same for everyone (the text of a statute, product names, and so on) | Shared data (D1, KV, R2) | The Renderer renders it into shared HTML, and Workers Cache shares it |
| Content per user (cart, favorites, the signed-in name) | Sessions and user data (in the shop sample, the cart is a separate table in the same D1 as the products) | The Gate reads the session, and a Server Island renders the HTML and sends it later in the same response. The browser receives HTML fragments to insert into the page |
| The result of a form | The server. The handler checks the input and changes the data. If the change affects shared HTML, the handler purges that HTML from the cache | A 303 sends the browser to the next page. When the browser opens that page, it gets the page as it is after the change. When the input is not accepted (nothing was changed), the handler can return the same page with the input and the reasons (422) |
| What is happening on screen now (editing, reviewing, sending; a menu open or closed) | Variables inside the Client Island, and attributes of elements (hidden, disabled and so on). Typed values stay in the form's own fields |
It is used only on that page. It is never sent to the server or carried to another page |
Why you do not need a store
- Every time you move to another page, the server returns new HTML. Nothing in memory carries over from one page to the next. So there is no data to carry around, and no code to throw away stale data.
- The data has one correct copy, on the server. When a form (
Form::at) changes data on the server, it returns a 303 and keeps no copy in the browser. You never manage the data twice, as in "change the server, then fix the browser's copy too". This pattern is called PRG (Post/Redirect/Get: after a submission, redirect, and then get the page again with GET). - Per-user parts are also rendered into HTML on the server. The shared HTML comes from the cache, and a Server Island renders things like the cart count from the current data. To personalize a cached page, you do not need to fetch data in the browser and keep it in state.
- When the display changes without a page reload, the value still comes from the server's response. For "Add to cart" in the shop sample, a Client Island calls the API and announces the returned count with a DOM event (
cart:changed). Each Island that shows the count updates its own display. Nothing in the browser keeps the count.
examples/min-form)The browser's standard form fields hold the name and message that you type. The Client Island (x-contact-form) keeps only the state it needs for the current display and actions: whether it is editing, reviewing or sending. The server shows that the message was sent by sending the browser to /thanks.
The server decides the result, and the browser keeps only the state that the current action needs. With this split, you can build most screens without a client-side store.
What to watch for in the browser
- The page that Back shows is not always a newly rendered page. The browser can keep a page exactly as you left it (bfcache, the back-forward cache) and show it again when you press Back. The Server Island parts are also as you left them. Some state must not remain when the user comes back, such as "sending". Reset it in the
pageshowevent whenevent.persistedistrue. This event does not tell you whether the submission succeeded. Only the server knows that. - If you keep state across pages in the browser, the syncing problem comes back. For example, if you leave a "sent" flag in
sessionStorage, your site has to manage any difference between that flag and the facts on the server (whether the message was really accepted). First, consider whether you can keep it in the session (which the Gate reads) or in a hidden form value. - Some screens do need a client-side store. Examples are screens used offline, screens that several people edit at the same time, and screens such as a large editor that work with a lot of data without page reloads. Hermet has no feature for this, so keep that state inside the Client Island.