Concepts
Rendering
How a zobacz page is rendered on the server and resumed in the browser, and what it costs.
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, and each page that may be shared says so in its load(). Opting in is per page: a page that does not call sharePage is never kept, so forgetting costs speed, never another visitor's data.
import { configurePageCache, sharePage, keepPagePrivate, cacheTag, cacheFor } from 'zobacz';
// once, at the Worker's entry
configurePageCache({
variantHeader: 'x-page-variant', // set by the front door; shared pages Vary on it
shareable: (req) => isAnonymous(req), // may this request get, and give, a shared copy?
});
// in a page's load()
sharePage(); // this page may be kept
cacheTag(`product:${product.id}`); // what it shows, to purge it by
cacheFor(secondsUntilNextChange); // at most this long (a timed change)
keepPagePrivate(); // never keep this answer (wins over sharePage)An answer is kept only when it is a 200 to a GET or HEAD, the page called sharePage and nothing called keepPagePrivate, it was drawn for no signed-in user, it sets no cookie, and shareable(req) is true. It then carries Cloudflare-CDN-Cache-Control (public, an hour, a day stale while revalidating), Cache-Tag and Vary, and never the session's CSRF token.
With the cache on, a page view mints no session cookie (an answer with Set-Cookie is never kept). A session is made in one place: the api client asks POST /api/session for the session and its token before the page's first write. Only that call, /api/rpc and the /api/state/ sockets make one; the entity sockets, beacons and other calls run without one, so a second session made in parallel cannot replace the first. Purge with the Worker's own ctx.cache.purge({ tags }) after a change.
The style nonce stays on kept pages. The nonce and the page are drawn together, so markup injected into the page could not know the nonce of the render it appears in; replaying that render to other visitors replays both, and any change that purges the page draws it again with a new nonce.
The full text of this page: docs.commernauts.com/framework/rendering