cms-cf-worker¶
A Cloudflare Worker that renders the public website at the edge from the CMS public v2 API. There are no per-page HTML files on a server: every response is built on demand by a single, generic, data-driven engine and cached close to the visitor.
Source:
src/index.ts,wrangler.toml,package.json.
One codebase, two workers¶
The repository ships two Workers from the same source tree, selected by the Wrangler environment that is deployed:
| Worker | Entry point | Role |
|---|---|---|
| Site worker | src/index.ts |
Renders pages, serves stylesheets and static text files, handles cache purge. Backed by a KV namespace. |
| Media worker | src/media/index.ts |
Streams media objects from S3 at the /cmp-media/* routes. Stateless; uses the edge Cache API only, no KV. |
This documentation is about the site worker (src/index.ts). The media
worker is mentioned only where it shares routes or deploy mechanics (see
Deploy and purge).
What the worker does¶
The worker is the public front door for one or more CMS-managed sites (for
example wantskicks.com) -- a single deployment resolves which site to serve
from the request Host by default, or from a per-worker CMS_SITE_ID
override for a single-tenant deploy (see Routing). For each
request it:
- resolves the site for the request and fetches its settings and page descriptors from the CMS public v2 API (cached aggressively at several layers);
- matches the request path to a page descriptor and renders HTML, either through a named builder or the generic declarative path;
- caches the rendered page and serves subsequent hits straight from the edge.
There is no hardcoded per-page code on the request path. Adding a new simple page type is configuration in the CMS, not a worker deploy. See Architecture.
Request lifecycle at a glance¶
handleRequest in src/index.ts walks a fixed sequence of steps. The numbered
comments in the source map one-to-one onto the stages below.
flowchart TD
A[Request] --> B{Admin endpoint?<br>/__cms/purge, /__cms/health}
B -- yes --> Z[Handle and stamp, bypass caches]
B -- no --> C{CSS asset path?<br>/_assets/styles/...}
C -- yes --> D[serveStyleCss / serveTemplateCss,<br>fall back to bundled CSS]
C -- no --> E{Preview?<br>valid ?_preview= secret}
E -- no --> F{Edge cache hit?<br>Cache API}
F -- hit --> G[Return cached, X-CMS-Cache: hit]
F -- miss --> H[resolveSite: mem to Cache API to origin to KV]
E -- yes --> H
H --> I[applyCssPolicy:<br>strip active_style if SKIP_STYLES_REGISTRY]
I --> J{Static text?<br>ads.txt, robots.txt, favicon.ico}
J -- yes --> K[Serve from settings]
J -- no --> L[matchDescriptor, else notFoundDescriptor]
L --> M{Preview?}
M -- yes --> N[Render fresh, never read or write cache]
M -- no --> O[getOrRender: Cache API, render, KV fallback]
The exact steps, keyed to the src/index.ts comments:
- Step 1 -- admin endpoints.
/__cms/purgeand/__cms/healthtake absolute priority and skip every cache layer. See Deploy and purge. - Step 1b -- styles-registry stylesheet. A request for
/_assets/styles/style/<slug>/<version>.cssis served byserveStyleCss(src/assets/serve.ts) from the DB-backed styles registry; checked first because its extrastyle/segment is more specific. On any miss it falls back to the bundled stylesheet. See Styles and CSS. - Step 1c -- per-template stylesheet. A request for
/_assets/styles/<slug>/<version>.cssis served byserveTemplateCss(src/assets/serve.ts) from cms-api; on any miss it falls back to the bundled stylesheet. See Styles and CSS. - Step 1d -- bundled stylesheet. The legacy single-file path
/_assets/styles/site.cssis served directly from the worker bundle. - Step 2 -- preview markers.
?_preview=<ADMIN_PURGE_SECRET>plus an optional?_ver=enables a fresh, cache-bypassing render for editors. - Step 3 -- cache-first short-circuit. For non-preview requests the worker checks the Cache API first and returns the cached body immediately on a hit. See Caching.
- Step 4 -- resolve site.
resolveSite(src/site/resolve.ts) loads the full site settings through its own three-tier cache (isolate memory, Cache API, origin) with KV as a fallback, resolved by the requestHostunless a per-workerCMS_SITE_IDoverride pins it to one site. An unknown host yields a clean 404 rather than falling through to any site's rendering. See Routing. - CSS policy.
applyCssPolicy(src/index.ts) stripsactive_stylefrom the resolved settings only whenSKIP_STYLES_REGISTRYis set, forcing the legacy per-template stylesheet. No env sets it today. (The old per-workerCSS_OVERRIDE_SLUG/CSS_OVERRIDE_VERSIONhack has been removed.) See Styles and CSS. - Step 5 -- static text files.
/ads.txt,/robots.txt, and/favicon.icoare served from the cached settings./favicon.ico302-redirects to the site's resolved branding favicon (resolveBrandingUrls,src/branding.ts), which prefers the cms-apiicon_url/default_faviconsettings over the/cmp-media/<acronym>/convention. - Step 6 -- route and dispatch.
matchDescriptor(src/engine/router.ts) picks the best descriptor for the path; an unmatched path falls back tonotFoundDescriptor(src/engine/settings.ts). See Routing. - Step 7 -- render and cache. For public requests
getOrRender(src/cache/index.ts) returns the cached page, or renders fresh and write-throughs to both the Cache API and KV. Preview requests render fresh and never touch the cache.
Every response carries observability headers X-CMS-Cache (which layer served
it) and X-CMS-Render-Ms (wall-clock time in the worker). See
Caching for the full set of header values.
Source tree map¶
| Path | Responsibility |
|---|---|
src/index.ts |
Site worker entry point and request lifecycle. |
src/engine/router.ts |
matchDescriptor / matchPattern -- path to descriptor. |
src/engine/render.ts |
renderPage -- dispatch a matched descriptor to a builder. |
src/engine/generic.ts |
renderGeneric -- the code-free declarative builder. |
src/engine/settings.ts |
DEFAULT_PAGE_TYPES, getPageTypes, notFoundDescriptor. |
src/engine/sources.ts |
Executes a descriptor's declared upstream API calls. |
src/site/resolve.ts |
resolveSite -- resolves the CMS site by host (or CMS_SITE_ID override) and the site-settings cache tiers. |
src/cache/index.ts |
getOrRender, purge, purgeSite -- page cache and purge. |
src/cms/client.ts |
cmsFetch -- authenticated GET against the CMS public v2 API. |
src/assets/serve.ts |
serveStyleCss / serveTemplateCss and their path parsers -- styles-registry and per-template CSS from cms-api. |
src/branding.ts |
resolveBrandingUrls -- logo/favicon URLs (cms-api icon_url/default_favicon, else the /cmp-media/<acronym>/ convention). |
src/layout/index.ts |
renderLayout, buildStyle, the Handlebars-like renderTemplate. |
src/admin/purge.ts |
POST /__cms/purge handler. |
src/pages/*.ts |
The named builders (homepage, article, category, author, ...). |
src/media/index.ts |
The separate media worker. |
wrangler.toml |
Worker names, routes, env vars, KV bindings for every env. |
Where to go next¶
- Architecture -- the generic engine and the template renderer.
- Routing -- how a path picks a descriptor.
- Caching -- the cache layers, their exact TTLs, and the change-propagation recipe.
- Styles and CSS -- the DB-backed styles registry and how CSS is served.
- Responsive images -- prepared
image_sm/image_opt, body normalize, S3 style publish. - Deploy and purge -- environments, deploy scripts, secrets, purge.
- Managing pages and designs -- the no-code operator guide for editors.
- Script and ad placeholders -- empty
data-slothooks for ads/scripts (AISD-233); worker does not inject yet. - Related API collections -- cms-api and cms-ai-forge references.