We run Tool & Till, a small editorial site with guides and comparisons for ecommerce sellers. It has no React, no Next.js and no build step for pages. Content lives in one JSON file, the browser renders it with plain ES modules, and a single Cloudflare Pages Function makes every URL crawlable.
This post walks through the setup: what the middleware does, how we moved from query-string URLs to clean paths without losing anything, and the small scripts that generate cover images, Pinterest pins, RSS and IndexNow pings from the same JSON file. The code snippets are simplified from the real site.
The constraint
We wanted three things that usually pull in different directions:
-
Editing without a database. Non-developers edit articles in a git-based CMS (Sveltia CMS), which commits to
content/posts.json. - No framework or page build. Adding an article should be a JSON change, not a rebuild of hundreds of HTML files.
-
Proper SEO. Every article needs a real
<title>, meta description, canonical URL, Open Graph tags, JSON-LD and the article text in the initial HTML, so crawlers and link previews do not depend on JavaScript.
A client-rendered SPA satisfies 1 and 2 and fails 3. Static site generators satisfy 1 and 3 but add a build pipeline. We ended up with a middle path: one HTML shell, server-side rewriting at the edge.
One shell, rewritten per request
The site has a single index.html. The browser app reads /api/posts, works out the route and renders the page. On the server, a Pages Function in functions/_middleware.js intercepts page requests, fetches that same shell from static assets and streams it through HTMLRewriter, filling in the per-page parts:
export async function onRequest(context) {
const url = new URL(context.request.url);
const route = parseRoute(url);
if (!route.view) return context.next(); // images, CSS, JS: untouched
// Every page is the same shell
const shell = await context.env.ASSETS.fetch(new URL("/", url));
const meta = getPageMetadata(url, categories, posts);
const rewritten = new HTMLRewriter()
.on("title", { element(e) { e.setInnerContent(meta.title); } })
.on('meta[name="description"]', new Attr("content", meta.description))
.on('link[rel="canonical"]', new Attr("href", meta.canonical))
.on('meta[property="og:image"]', new Attr("content", meta.image))
.on("main#main-content", {
element(e) { e.setInnerContent(renderPageFallback(meta), { html: true }); },
})
.on("head", {
element(e) { e.append(`<script type="application/ld+json">${jsonLd(meta)}</script>`, { html: true }); },
})
.transform(shell);
// Unknown slugs still render the shell, but with a real 404 status
return meta.valid ? rewritten : new Response(rewritten.body, { status: 404, headers: rewritten.headers });
}
HTMLRewriter streams, so there is no DOM parsing and the overhead is small. The posts JSON is imported into the function bundle at deploy time, so there is no fetch per request.
The key detail is that the server and the browser share the same modules. assets/markdown.js, assets/routes.js and assets/seo.js are plain ES modules imported both by the browser app and by the middleware. The server-rendered article body and the client-rendered one come from the same Markdown renderer, which avoids drift between what Google sees and what readers see.
Returning real 404s
A classic SPA problem is the "soft 404": every unknown path returns the shell with status 200, and Search Console fills up with junk. Two fixes:
- The middleware sets status 404 when
getPageMetadatacannot find the slug (last line above). - A
404.htmlwithnoindexhandles paths the middleware does not route at all.
Moving from ?view=article&slug=x to /guides/x/
The first version used query-string URLs. Google indexed them, but they look poor in results and a few were reported as duplicates. We moved to clean paths with one shared route module:
const PREFIX = { category: "category", article: "guides", comparison: "compare", review: "reviews" };
export function pathFor(view, slug = "") {
if (!PREFIX[view] || !slug) return "/";
return `/${PREFIX[view]}/${encodeURIComponent(slug)}/`;
}
export function parseRoute(url) {
const m = url.pathname.match(/^\/(category|guides|compare|reviews)\/([a-z0-9-]+)\/?$/i);
if (m) return { view: VIEW_BY_PREFIX[m[1]], slug: m[2].toLowerCase(), legacy: false };
const view = url.pathname === "/" && url.searchParams.get("view");
if (view) return { view, slug: url.searchParams.get("slug") || "", legacy: true };
return { view: url.pathname === "/" ? "home" : null, slug: "", legacy: false };
}
The middleware then 301-redirects anything legacy, plus www and missing trailing slashes, so each page has exactly one URL:
if (url.hostname === "www.toolandtill.com" || route.legacy) {
const target = new URL(url);
target.hostname = "toolandtill.com";
if (route.legacy) {
target.pathname = pathFor(route.view, route.slug);
target.searchParams.delete("view");
target.searchParams.delete("slug");
}
return Response.redirect(target.href, 301);
}
Two gotchas we hit:
-
_routes.json. Pages Functions only run for paths listed in_routes.jsoninclude. New prefixes (/guides/*,/compare/*, ...) must be added, or the function never sees the request and you get the raw shell. -
Old links inside content. Many internal links in the Markdown still pointed at the query form. Rather than trusting a one-off search-and-replace, the Markdown renderer also runs every
hrefthrough acleanInternalHref()helper, so a stray legacy link written later still renders as a clean URL.
One thing we deliberately did not change: the RSS <guid>. Feed consumers (Pinterest auto-publish in our case) use the guid to decide what is new. Changing it would have re-published every article, so the guid keeps the legacy URL while <link> uses the clean one.
Generated images: covers, pins and diagrams without a design tool
Each article has a 960x520 SVG illustration. A Node script, scripts/covers.mjs, combines the title, category colour and that illustration into:
- a 1200x630 cover (SVG for the page, PNG for Open Graph),
- a 1000x1500 Pinterest image,
- a second, lighter Pinterest image that reuses the article's first diagram.
There is no layout engine, so titles are wrapped with a rough width table for the font and the font size steps down until the title fits:
for (const size of [84, 76, 68, 62, 56]) {
lines = wrap(title, size, 840); // approximate em widths per character
if (lines.length <= 6) break;
}
The illustration is inlined as a nested <svg> with every id prefixed, so gradients from different SVGs cannot clash, and rsvg-convert turns the result into PNG. A GitHub Action runs the script whenever content/posts.json changes and commits the new images, so an article saved in the CMS gets its cover without anyone opening a design tool.
In-article diagrams (flows, grids, stacks) come from a small Python script that takes a JSON spec and emits a consistent 960x540 SVG. The reason is consistency: every diagram on the site has the same typography and spacing, and changing the style means changing one script.
Feeds and indexing from the same JSON
The middleware also serves, straight from the posts data:
-
/sitemap.xmlwith image entries, -
/rss.xmlplus one feed per category (/rss/<category>.xml), each mapped to its own Pinterest board, -
/rss/fresh.xml, which releases each article's second pin a week after publication.
A second GitHub Action pings IndexNow with changed URLs on every push, so search engines that support IndexNow (such as Bing) hear about new pages right away instead of waiting for a crawl.
Was it worth it?
What we like:
- Publishing is a JSON commit. The CMS, the scheduled content jobs and manual edits all write the same file.
- One renderer. Server and browser share modules, so there is no hydration mismatch to debug.
- Cheap and fast. Static assets on Pages, one function for HTML, no origin server.
What it costs:
- Everything is bundled into the function. Fine at a few hundred posts; at tens of thousands you would move the content to KV or D1 and fetch per request.
- You own the SEO plumbing. Canonicals, 404s, redirects, sitemaps and structured data are your code, not a framework's defaults, so they need tests or at least a checklist.
If you run a content site and want to keep the stack small, an edge rewriter over a single shell is a practical alternative to adopting a full framework. You can see the result on any article at toolandtill.com; view the page source to see the server-rendered HTML.
Top comments (0)