Seven stages · one request · one click

How rendering works

The server renders the page, and the browser keeps that DOM and loads each component’s code so it can handle events. Below is every stage between a request arriving and your first click, each with a link to the code that does it.

The links open in VS Code from a local checkout. Each one also shows its file path and line.

Build

Your component is split in two

Handlers become HTML attributes. Server-only code is deleted from the browser bundle.

Inbox.tsxone filemarkupdata-z-on:clickchunknot sent yetto the pagestays put
once, on your machine

The compiler rewrites onClick={() => this.remove(id)} into a data-z-on:click attribute carrying the method name and its arguments. It removes the body of every method without @client from the client bundle, and gives each component class its own chunk, which the page loads after the HTML.

Worth knowingA database query in an undecorated method stays on the server: the compiler removes that method's body from everything sent to the browser.

In the source

Server

load() runs before anything is rendered

Inside the Worker, reading D1 directly.

requestWorkerload()D1no hop
per request, in the Worker

A request arrives at the Worker. The page's load() runs first and puts what it fetched into signals. It reads the database through a repository, in the same isolate.

Worth knowingA relative fetch() in load() is a build error: inside a Worker a relative URL has no origin, and the page would render empty.

In the source

Server

template() renders the page on the server

It produces HTML, and the signals it read are serialized alongside it.

template()on the serverHTMLfinished markupsignal stateserialized beside it
per request, in the Worker

template() is called on the server and its output becomes HTML. The values of the signals it read are written into the page too, so the browser continues from the same state.

Worth knowingSignal state is serialized; plain instance fields are left out. A field set in load() renders correctly here and is undefined in the browser, so the build rejects reading one from a @client method.

In the source

Server

The response is HTML and the page's scripts

The finished markup, the resumer, and one module per component on the page.

markupresumercomponentsthe browserkeeps the markup
per request, in the Worker

The response carries the finished markup, the resumer (about 4 KB compressed), and a module for each component on the page. Each module imports that component's code and the shared runtime, so the browser starts fetching them with the HTML.

In the source

Browser

Components pick up where the server left off

Each component's code loads and its element is upgraded; the DOM stays as the server sent it.

the server’s DOMcomponents upgradedtemplate(): dry runone listenerper event type
after load, and on each event

The resumer upgrades a component with a window listener or a live subscription as soon as the page is parsed, and every other component when the browser is idle or the visitor first points, touches, focuses or types, so its code stays off the first paint. A click that arrives first is held and replayed once the code is there. The element restores its signals from the snapshot, runs onConnect, and runs template() once as a dry run to learn which signals it reads. The dry run's output is discarded, so the DOM the server sent stays in place. One delegated listener per event type the build found in the app handles events.

Worth knowingThe browser keeps the server's DOM as sent, so the markup the user sees is the markup the server rendered, until a signal changes.

In the source

Browser

A click calls the method

Read the attribute, find the component that owns it, call the method.

clickattributemethod + argscomponentalready loadedmethodruns
after load, and on each event

The delegated listener walks up from the clicked element to find the data-z-on attribute and the component that owns it, and calls the named method with the arguments the compiler recorded. The component's code is already loaded; a click that arrives before it has waits while the listener imports it.

Worth knowingThe arguments travel in an attribute, so they must be serializable: literals, loop variables and values read from this.

In the source

Browser

A signal write patches the DOM in place

template() re-runs, and the result is applied to the live DOM as edits.

signal.setone writetemplate()re-runsmatched by keyyour typing kept
after load, and on each event

Writing a signal re-runs the render for that component and applies the new output to the live DOM as a set of edits. Nodes are matched by their key, so a reordered list moves the same elements, and a form value is assigned only when it changed.

Worth knowingThose two behaviours are why typing into an input survives an unrelated update, and why focus follows a row when a list is reordered.

In the source

What that adds up to

Stages four and five decide what happens before you interact: the browser loads the page’s scripts, upgrades each component, and keeps the server’s DOM.

WhenWhat runs in the browserWhy
Page loadedeach component’s chunk, onConnect, a dry run of template()The DOM stays as the server sent it (stage 5).
First clickthe clicked methodThe attribute names the method and its arguments (stage 6).
Every click afterthe clicked methodThe same path as the first click.
State changesthat component onlyA signal write re-renders its own component (stage 7).

The shared page cache

Why: a page drawn on every visit waits on storage before its first byte. A page that is the same for every visitor who is not signed in can be drawn once and kept at the edge (Cloudflare’s Workers Cache in front of the Worker), and answered from there in milliseconds.

How: the app switches it on once with configurePageCache, and each page that may be shared calls sharePage() in its load(). A page that does not call it is never kept. An answer is kept only when it is a 200 to a GET or HEAD, drawn for no signed-in user, with no cookie set; it carries Cloudflare-CDN-Cache-Control, Cache-Tag and Vary, and never the session’s CSRF token. A change purges its pages by tag with the Worker’s own ctx.cache.purge.

What it costs

Handler arguments are written into an HTML attribute (stage 6), so they must be serialisable: literals, loop variables and values read from this. A handler is an arrow function that calls one method with those arguments. In return, the event wiring is part of the server’s markup, and the browser keeps that markup as it is.