Skip to content

Styles and CSS

A site's stylesheet is data, not worker code. The worker links a versioned, long-cached stylesheet in <head> and serves the bytes from cms-api on demand. Which stylesheet it links is resolved per request from the site's settings.

Source: src/layout/index.ts (buildStyle), src/assets/serve.ts (serveStyleCss / serveTemplateCss), src/index.ts (asset routing + applyCssPolicy), src/types.ts, wrangler.toml.

The old per-worker CSS override is gone

Earlier the worker supported a CSS_OVERRIDE_SLUG / CSS_OVERRIDE_VERSION pair of env vars that cosmetically rewrote active_template in memory so a single worker could canary a different design. That hack has been removed from the code -- no env sets it and no code reads it. Styling is now driven entirely by the DB-backed styles registry (AISD-168/AISD-169). To change a site's skin you change its style in the CMS, not a worker var. This page replaces the former css-override.md.

buildStyle (src/layout/index.ts) emits the <link rel="stylesheet"> for every page. It resolves the link in a strict priority order off the resolved SiteSettings:

  1. Styles registry (active_style) -- the DB-driven registry. When site_settings.active_style carries a { slug, version }, the page links /_assets/styles/style/<slug>/<version>.css.
  2. Per-template CSS (active_template) -- the older per-template stylesheet. When there is no active_style but active_template has a slug and css_version, the page links /_assets/styles/<slug>/<css_version>.css.
  3. Bundled fallback -- when neither is set, the page links the worker's bundled single-file stylesheet /_assets/styles/site.css?v=<STYLESHEET_VERSION>, so a page is never left without CSS.
const activeStyle = ss.active_style;
const styleSlug = typeof activeStyle?.slug === "string" ? activeStyle.slug : "";
const styleVersion = typeof activeStyle?.version === "string" ? activeStyle.version : "";
const active = ss.active_template;
const slug = typeof active?.slug === "string" ? active.slug : "";
const cssVersion = typeof active?.css_version === "string" ? active.css_version : "";
let linkTag: string;
if (styleSlug && styleVersion) {
  linkTag = `<link rel="stylesheet" href="/_assets/styles/style/${encodeURIComponent(styleSlug)}/${encodeURIComponent(styleVersion)}.css">`;
} else if (slug && cssVersion) {
  linkTag = `<link rel="stylesheet" href="/_assets/styles/${encodeURIComponent(slug)}/${encodeURIComponent(cssVersion)}.css">`;
} else {
  linkTag = `<link rel="stylesheet" href="/_assets/styles/site.css?v=${STYLESHEET_VERSION}">`;
}

buildStyle also emits a small inline <style> block for per-request CSS variables (--primary, --font-family, --font-size from the layout block's settings) plus any per-site CSS carried in the layout template block's html_content. Those vary per site/request, so they stay inline rather than in the long-cached external file.

The styles registry (active_style) -- how a site picks its skin

The registry decouples CSS from the template shell. A site's presentation is a template_styles pairing selected by site_settings.template_style_id (not a column on sites). cms-api resolves that pairing to a template + style and surfaces active_style: { id, slug, version } (and active_template) on /settings. The worker links the style asset from active_style. Switching a site's skin is a settings change to template_style_id -- no worker redeploy for the CSS bytes, but you still need the propagation recipe so cached HTML picks up the new <link>.

Today every env resolves style from the registry (see SKIP_STYLES_REGISTRY for the retired opt-out).

Where the CSS bytes live (S3)

Style CSS is stored as versioned objects in the per-env cms-templates bucket (styles/<slug>/<version>.css). Uploading a new file via cms-api POST /admin/v1/styles/{id}/file writes S3, stamps styles.s3_key / styles.version, and the next /settings response carries the new version.

Local edit surface (git mirrors of the active versions only):

cms-cf-worker/templates/styles/<slug>/<version>.css
cms-cf-worker/templates/templates/<slug>/<version>/shell.html

Keep those paths byte-identical to the registry's active S3 keys (AISD-227 snapshot + post-AISD-206 sync). Re-sync procedure is in cms-cf-worker/templates/README.md.

How the CSS bytes are served

Two /_assets/styles/... shapes are parsed in src/index.ts and served from cms-api. The style-registry path is checked first because its extra style/ segment makes it more specific:

Request path Handler cms-api subrequest
/_assets/styles/style/<slug>/<version>.css serveStyleCss (step 1b) GET /public/v2/styles/<slug>/css?v=<version>
/_assets/styles/<slug>/<version>.css serveTemplateCss (step 1c) GET /public/v2/templates/<slug>/css?v=<version>
/_assets/styles/site.css bundled from the worker (step 1d) --

Both serveStyleCss and serveTemplateCss (src/assets/serve.ts):

  • sanitise slug/version to a path-safe charset before building the URL;
  • fetch from cms-api with an 86400 s (24 h) edge subrequest cache (cmsFetch(path, env, { cacheTtl: 86400 })) -- the L4 cf layer (see Caching). The ?v=<version> is part of the URL, so bumping the version is a new cache key: new CSS propagates without waiting out the TTL and without a worker redeploy;
  • return the CSS with content-type: text/css, cache-control: public, max-age=31536000, immutable, and an x-cms-css-source: db header that makes the DB origin observable;
  • return 404 on any miss/upstream error, so src/index.ts falls back to the bundled stylesheet -- a missing asset never 500s a page.
const path = `/public/v2/styles/${encodeURIComponent(safeSlug)}/css?v=${encodeURIComponent(safeVersion)}`;
// Long edge cache on the subrequest; the versioned ?v busts it on change.
const resp = await cmsFetch(path, env, { cacheTtl: 86400 });

SKIP_STYLES_REGISTRY (opt-out)

The worker still supports one escape hatch. applyCssPolicy (src/index.ts) strips active_style from the resolved settings when the env var SKIP_STYLES_REGISTRY is "true" (or "1"):

function applyCssPolicy(settings: SiteSettings, env: SiteEnv): SiteSettings {
  const skip = env.SKIP_STYLES_REGISTRY === "true" || env.SKIP_STYLES_REGISTRY === "1";
  if (!skip || !settings.site_settings.active_style) {
    return settings;
  }
  return { ...settings, site_settings: { ...settings.site_settings, active_style: undefined } };
}

With active_style stripped, buildStyle falls through to the per-template stylesheet (priority 2). No env sets this var today -- it was removed from [env.site-stage.vars] (AISD-169) so stage uses the registry like every other env. The knob remains only as a per-worker way to pin a deploy to its legacy template CSS if a registry rollout ever needs to be paused.

Static-page CSS

The static builder pages (about, contact, privacy, terms, do-not-sell) are rendered through the same renderLayout path, so they link the same buildStyle stylesheet as every other page. They add no CSS of their own; their markup and any inline styling come from the cms-api template block. See Architecture for how their content resolves.

Changing a design, end to end

Because a rendered page bakes its stylesheet <link> into the cached HTML, publishing a new design is a two-part act -- change the source of truth, then make the change visible past the caches:

  1. Change the style in the CMS. Upload a new CSS file for the style (POST /admin/v1/styles/{id}/file) and/or point the site at a different template_style_id pairing. A new active_style.version is a new asset URL, so the L4 subrequest cache serves the new CSS immediately.
  2. Propagate past the page caches. Already-cached pages keep the old <link> until they are purged or expire. Follow the full recipe in Caching -> making a change visible: POST /__cms/purge with site: true, a site worker redeploy (clears the isolate memMap), plus a zone edge purge (purge_everything). Verify with the plain URL (a ?query bypasses the edge cache and can mislead you).

For prepared content image variants (image_sm / image_opt), body HTML normalization, the mobile overflow checklist, and the current four-skin inventory, see Responsive images.