Running a Personal Blog on Cloudflare Pages and Self-Hosted Supabase
Why moving Markdown bodies inline into Postgres removed a whole failure domain, plus the PostgREST sorting, schema isolation, and edge XSS details that actually mattered.
Contents 8
When I first put this site together, I treated it like a miniature SaaS app: SvelteKit on Cloudflare Pages at the edge, a self-hosted Supabase stack running in Docker at home behind a Cloudflare Tunnel, and Markdown files stored in an S3-compatible bucket served through supabase-storage.
It worked fine during the initial deploy. Then a few weeks later, every single article opened to an empty page.
How an S3 Sidecar Turned Every Post Blank
The failure chain was embarrassingly simple. The posts table only stored content_en_path and content_zh_path pointing at blog-content/*.md. On every article request, the Cloudflare Worker made two hops back through the tunnel:
Browser -> Cloudflare Pages (SvelteKit SSR)
├─ 1) GET /rest/v1/posts?slug=eq.... -> PostgREST -> Postgres (200 OK)
└─ 2) GET /storage/v1/object/public/... -> Storage -> rustfs (500 ENOTFOUND)The external volume backing rustfs had unmounted after a reboot, the container exited, and supabase-storage started answering 500 getaddrinfo ENOTFOUND rustfs. Because the fetch helper swallowed non-200 storage responses and returned an empty string, the page rendered the title, date, and tags — followed by "This article has no body text yet."
Worse still, the list page tried to estimate reading time by fetching every post's storage object on the fly. One dead S3 container meant every visit to /blog fanned out N doomed HTTP requests through the tunnel just to compute reading minutes.
The Fix: Keep Text Inline in Postgres
Blog posts are a few kilobytes of UTF-8 text, not gigabyte media blobs. Postgres compresses wide TEXT columns via TOAST automatically, keeps metadata and body in a single transaction, and turns an article load back into one PostgREST query.
ALTER TABLE website.posts ADD COLUMN IF NOT EXISTS slug TEXT UNIQUE;
ALTER TABLE website.posts ADD COLUMN IF NOT EXISTS content TEXT;
ALTER TABLE website.posts ADD COLUMN IF NOT EXISTS content_zh TEXT;
ALTER TABLE website.posts ADD COLUMN IF NOT EXISTS source_url TEXT;
ALTER TABLE website.posts ADD COLUMN IF NOT EXISTS published_at TIMESTAMPTZ;
ALTER TABLE website.posts ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ;Keeping the change additive meant legacy rows with storage paths did not break migrations, while any post with content or content_zh populated never touches the storage service at all. Object storage is still useful for images, where a missing thumbnail degrades gracefully instead of wiping out the entire post.
Three PostgREST Details Easy to Miss
Once the data model lived entirely in Postgres, three small PostgREST quirks surfaced almost immediately.
1. Schema Isolation via Accept-Profile
On a self-hosted Supabase instance that backs multiple personal projects, dumping tables into public gets messy fast. Putting the blog in its own website schema and exposing it in PostgREST's PGRST_DB_SCHEMAS lets the Worker stay cleanly isolated using request headers:
function restHeaders(env: SupabaseEnv, extra: Record<string, string> = {}) {
return {
apikey: env.anonKey,
Authorization: `Bearer ${env.anonKey}`,
'Accept-Profile': env.schema,
'Content-Profile': env.schema,
...extra
};
}Row Level Security still enforces read-only public access (published = true) for the anon role, while publishing scripts use either a direct DB connection or service_role upsert with Prefer: resolution=merge-duplicates on on_conflict=slug.
2. ORDER BY published_at DESC Puts NULL First
PostgreSQL treats NULL as larger than any concrete timestamp when sorting descending. When we added published_at as an additive column, older rows still had published_at = NULL, and querying order=published_at.desc pinned those legacy rows to the top of the blog above freshly published posts.
PostgREST supports explicit null placement directly in the query string:
/rest/v1/posts?select=*&published=eq.true&order=published_at.desc.nullslast,created_at.descIt is a one-line fix, and one of those defaults that only bites you after your schema evolves.
3. UUID vs. Slug Lookups
Older links used the row UUID (/blog/<uuid>), while newer links use readable ASCII slugs (/blog/go-async-task-framework-notes). Filtering id=eq.<slug> on a uuid column causes PostgREST to return 400 Bad Request rather than an empty array. Trying id=eq. and falling through to slug=eq. on 400 keeps old links alive without maintaining a separate redirect table.
Server-Side Markdown Rendering on Cloudflare Workers
Rendering Markdown on the server keeps the client bundle small and makes RSS, Open Graph tags, and crawler indexing work out of the box. The constraint on Cloudflare Workers is bundle size and runtime compatibility: pulling in jsdom + DOMPurify + the full highlight.js bundle bloats the worker quickly.
Instead, the renderer registers only the 24 languages actually used in backend and infrastructure writing (highlight.js/lib/core) and handles raw HTML with a strict default-deny allowlist:
| Layer | Choice | Why |
|---|---|---|
| Markdown parser | marked (GFM enabled) |
Fast, zero Node-only dependencies |
| Syntax highlighting | highlight.js/lib/core + 24 langs |
Keeps compressed worker bundle ~315 KB |
| HTML sanitization | Narrow tag/attribute allowlist | No DOM dependency; unknown tags escaped as text |
| URL scheme check | Entity-decode + http/https/mailto |
Blocks javascript: and entity-obfuscated payloads |
For bilingual posts, rendering both en and zh HTML + TOC on the server during load() adds negligible CPU time (a few milliseconds) and lets the language toggle in the header switch the title, prose, reading time, and sticky table of contents instantaneously with zero round-trips.
Trade-offs of This Setup
This architecture is intentionally boring, and it has clear boundaries:
- Latency floor: Every uncached SSR hit crosses Cloudflare's edge to a home server through Cloudflare Tunnel. For a personal technical blog with modest traffic, 80–150 ms TTFB is fine; for high-concurrency traffic, you would add stale-while-revalidate caching at the edge or prerender static pages at build time.
- Single-node database: Keeping Supabase on one local machine is cheap and gives full Postgres control, at the cost of availability if home power or ISP drops. Keeping all post Markdown files in Git alongside a deterministic sync script means the content layer can be rebuilt on a fresh Postgres instance in seconds.
If I were starting from scratch for a purely manual blog, static Markdown files compiled into the repo would be even simpler. The reason Postgres stays here is programmatic publishing: automated research pipelines and drafts can upsert into website.posts without triggering a full CI rebuild for every edit.