Cloudflare · Edge · DevOps · Performance
Static Site plus Edge Logic with Cloudflare Pages and Workers
Deploying a static site on Cloudflare Pages and Workers: caching with _headers, _redirects, advanced mode _worker.js, _routes.json and content negotiation.
sanderyaz.com is a set of static files built with Vite and deployed on Cloudflare. "Static" doesn't mean "dumb", though: a small piece of logic runs at the edge to serve Markdown to AI agents, redirect old URLs and send the right cache headers. This article shows how the setup works and the traps to avoid.
Why static plus edge?
- Speed: files are served from the data centre closest to the user, with no server-side rendering cost.
- Cost and maintenance: no servers, no OS patches, no scaling worries.
- Flexibility: when you need per-request logic (redirects, headers, content negotiation), it runs at the edge in milliseconds.
_headers: getting caching right
Cloudflare Pages applies the rules in the root _headers file to responses. The most important rule caches assets with a hash in their file name as immutable for a year:
/assets/*
Cache-Control: public, max-age=31536000, immutable
/sitemap.xml
Cache-Control: public, max-age=3600
This is safe because Vite changes the file name whenever the content changes (index-D0z6elPr.js, for example).
Trap: if you mark a file without a hash in its name (such as
/style.css) asimmutable, returning visitors may see the old version for a year after you update it. That's why the blog's own styles on this site live inside each page rather than in an external file.
Security headers (X-Content-Type-Options, Referrer-Policy, Strict-Transport-Security, Content-Security-Policy) can be applied to every page from the same file.
_redirects: don't lose old URLs
When the site structure changes, old links shouldn't return 404. Permanent (301) redirects carry both users and accumulated search value to the new address:
/os / 301
/os/* / 301
/29.08.2025.pdf /cv/Sander_Yazicioglu_CV_EN.pdf 301
The edge worker: per-request logic
Pages offers two ways to run server-side logic: Pages Functions via a functions/ folder, or advanced mode, which means putting a _worker.js at the root of the build output. This site uses the latter, because the same worker code also runs on Workers with static assets (wrangler.json).
What the worker does is simple:
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname.endsWith('/') && prefersMarkdown(request.headers.get('Accept'))) {
const markdown = await env.ASSETS.fetch(new URL(`${url.pathname}index.md`, url));
if (markdown.ok) return withHeaders(markdown, { 'Content-Type': 'text/markdown; charset=utf-8', Vary: 'Accept' });
}
return env.ASSETS.fetch(request);
}
};
env.ASSETS.fetch reads the static file; the worker only decides which file to return. The content negotiation I describe in making your website AI-agent ready runs on code this small.
_routes.json: don't run the worker for nothing
In advanced mode, every request goes through the worker by default. For JavaScript files, images and fonts that is a wasted hop that also eats your quota. _routes.json limits the paths the worker runs on:
{
"version": 1,
"include": ["/*"],
"exclude": ["/assets/*", "/images/*", "/robots.txt", "/sitemap.xml"]
}
On this site that file is generated at build time from the run_worker_first list in wrangler.json, so the two configurations can't drift apart.
Keeping the 404 page
With a worker in front, the asset store may return an empty 404 for a missing page. So that visitors still see the site's designed 404 page, the worker reads /404.html and returns it with a 404 status. The status code matters for SEO: a 200 would look like a "soft 404" to search engines.
Verifying the deploy
A small validation script at the end of the build checks: each page's canonical URL, description, a single h1, valid JSON-LD, a Markdown twin, the sitemap matching the pages, and _headers and _redirects being in the output. If anything is missing the build fails, and a broken version never ships.
Conclusion
A static site with a thin edge layer is an excellent balance of speed, cost and flexibility for personal and content sites. With _headers, _redirects, _routes.json and a small worker you get server-side behaviour without managing servers. To improve the front end's loading order, read the Core Web Vitals article.