← All articlesEngineering

Zero-Gateway Architecture: Pairing D1, KV Versioning, and Durable Previews

How a Cloudflare Worker serves sub-50ms cached HTML while still reflecting editorial edits almost immediately—without sacrificing draft isolation.

Minimalist fluted glass prisms and dark anodized slate louvers refracting daylight across pale limestone
Minimalist fluted glass prisms and dark anodized slate louvers refracting daylight across pale limestone.

Static site generators taught the web to love fast TTFB, yet every editorial team knows the pain of waiting four minutes for a CI pipeline just to fix a typo in a headline. On the other extreme, hitting a primary database on every page view wastes latency and turns traffic spikes into pager alerts.

Versioned keys instead of blind purges

Rather than tracking every URL affected by an author rename or a category description change, Narew CMS writes a single monotonic token to Workers KV under narew:v:<databaseId> whenever a content mutation commits. Our Worker folds that token straight into the HTML cache key.

const version = await cacheVersion(site.databaseIds.DB, cache);
const key = `journal:${version}:${url.pathname}`;
// Old cached pages expire naturally; the next request renders fresh HTML.

Keeping drafts completely isolated

Public readers never see draft articles because every query filters on status = 'published'. When an editor opens a live preview from the CMS studio, the Worker verifies a short-lived ECDSA-signed token, binds a Durable Object session, bypasses the shared KV cache, and temporarily widens the query predicate for the selected record ID alone.

  1. Public GET requests read the KV version token and serve cached HTML in milliseconds.

  2. Authenticated preview requests bypass shared cache through withSharedContentCache.

  3. Batch media lookups resolve cover images and galleries in one SQL query, eliminating N+1 roundtrips.

Fast publishing is not about clever client hydration—it is about deleting layers that never needed to exist.

The result is an architecture small enough to read in one sitting, predictable under load, and resilient when editors publish ten revisions in an afternoon.