How to serve Markdown for every page with SvelteKit and Cloudflare
Serve each SvelteKit page as HTML or Markdown on Cloudflare Pages, with a direct .md URL, an Accept header rewrite, and a build check for missing pages.
CrabTalk Team
On CrabTalk, /pricing serves HTML to a browser and Markdown to a client that sends Accept: text/markdown. SvelteKit builds the Markdown at /pricing.md, and a Cloudflare URL Rewrite Rule sends the Markdown request there while keeping /pricing in the client’s URL.
Give each page a direct .md URL
Append .md to the public page path. The home page uses /index.md so it has a filename too.
/→/index.md/pricing→/pricing.md/docs/guides/quickstart→/docs/guides/quickstart.md
The direct URL works with a plain GET, so it can go in a link or a site index such as /llms.txt. Our marketing and docs layouts advertise the URL in the HTML head, with <link rel="alternate" type="text/markdown" href="/pricing.md"> on the pricing page.
Build the Markdown in SvelteKit
Put the HTML at src/routes/about/+page.svelte and its Markdown source beside it at src/routes/about/page.md. The adjacent route src/routes/about.md/+server.ts imports that source as text and serves it at /about.md. SvelteKit endpoint docs
// src/routes/about.md/+server.ts
import body from "../about/page.md?raw";
export const prerender = true;
export function GET() {
return new Response(body, {
headers: { "Content-Type": "text/markdown; charset=utf-8" },
});
}prerender = true saves the response body at build time. Our @sveltejs/adapter-cloudflare setup places the Markdown files alongside the prerendered HTML in .svelte-kit/cloudflare. Cloudflare Pages setup for SvelteKit
We use the same Markdown handler across marketing routes. It loads written page.md files with Vite’s import.meta.glob and selects the source by stripping the URL suffix. Vite glob imports
Docs and blog posts use the same MDX source for both formats. The docs route at docs/[...slug].md/+server.ts exports entries() from the docs index so SvelteKit knows which nested .md paths to generate. SvelteKit’s entries option
For data-driven pages, reuse the data that feeds the HTML. Our pricing Markdown pulls its plan table from the same data as the pricing page, so a price change reaches both versions on the next build.
Rewrite Markdown requests in Cloudflare
The rewrite lives on the Cloudflare zone for the site’s custom domain. To reproduce it, attach your domain to your Pages project, then open Rules → Overview → Create rule → URL Rewrite Rule on that zone. Pages custom domains · Cloudflare dashboard instructions
For paths other than /, match the host, an Accept value containing text/markdown, and paths with no dot. The dot check keeps /robots.txt, /sitemap.xml, and direct .md URLs on their existing paths.
http.host eq "crabtalk.ai"
and http.request.uri.path ne "/"
and not (http.request.uri.path contains ".")
and any(http.request.headers["accept"][*] contains "text/markdown")Set Path → Rewrite to → Dynamic and keep Query → Preserve. The path expression appends .md to the request path. Cloudflare’s concat function
concat(http.request.uri.path, ".md")For the home page, keep the host and header checks, match http.request.uri.path eq "/", and set the path to the static value /index.md. The rewrite happens inside Cloudflare, so a client calling / receives the Markdown body in the same request. Cloudflare URL Rewrite Rules
Check the response and the build
Make both requests to the same URL and inspect the status and response headers. Then compare the Markdown body with the explicit .md URL.
curl -sS -D - -o /dev/null -H 'Accept: text/html' https://crabtalk.ai/pricing
curl -fsS -D - -o pricing.md -H 'Accept: text/markdown' https://crabtalk.ai/pricing
curl -fsS https://crabtalk.ai/pricing.md -o pricing-direct.md
cmp pricing.md pricing-direct.mdThe Markdown response should be 200 with Content-Type: text/markdown; charset=utf-8. Both responses carry Vary: Accept so HTTP caches know the representation depends on that request header. Cloudflare’s guide to Vary
Our Pages _headers file adds Vary: Accept to static responses, and the SvelteKit server hook adds it to responses generated at request time. Cloudflare Pages applies _headers on the static asset path. Cloudflare Pages headers
At build time, the sitemap generator checks that each listed marketing page has a Markdown renderer. Docs and posts are generated from their content indexes, so adding this post also creates its own .md URL.