How Dynamic Content Works on a Static Site
A common misconception among newcomers to web development is that every multi-article blog requires a live application server and a relational database. In this article, I'll deconstruct how I built the original static-first blog engine for the first iteration of this portfolio—along with its security and performance trade-offs that eventually led me to migrate to Next.js App Router.
The Architectural Contrast
In a traditional Server-Rendered Setup, the server does the assembly per request:
- Browser requests
/blog/post-1. - Server queries a SQL database.
- Server renders HTML via a template engine or RSC tree.
- Browser receives ready-to-paint HTML.
In a pure Client-Rendered Static Setup, static hosting serves raw files and the browser assembles the view:
- Browser visits
/post.html?id=post-1. - CDN serves a static
post.htmlshell. - Client-side JavaScript reads the query parameter and validates it against a static
posts.jsonmanifest. - JavaScript fetches the matching
.mdfile and renders sanitized HTML into the DOM.
Step 1: Query-Parameter Routing & Allowlist Lookup
Instead of trusting arbitrary file paths from the URL, we read the id parameter and match it strictly against a known posts.json manifest:
const params = new URLSearchParams(window.location.search);
const slug = params.get("id");
const manifestRes = await fetch("/assets/data/blog/posts.json");
const posts = await manifestRes.json();
const entry = posts.find((p) => p.id === slug);
Step 2: The Static JSON Manifest
Rather than running a database for ten articles, posts.json acts as a lightweight, version-controlled index mapping each slug to its metadata and verified asset path:
{
"id": "static-site-dynamic-content",
"title": "How Dynamic Content Works on a Static Site",
"file": "/assets/data/blog/posts/static-site-dynamic-content.md"
}
Step 3: Markdown Parsing with XSS Sanitization
A critical detail many tutorials get wrong: Markdown parsers like marked.js do not sanitize output HTML by default. Piping marked.parse() straight into innerHTML creates a textbook DOM-based XSS vector if raw HTML or untrusted content ever enters a Markdown file. Always pair client-side HTML injection with DOMPurify:
import { marked } from "marked";
import DOMPurify from "dompurify";
async function loadArticle(entry) {
const res = await fetch(entry.file);
if (!res.ok) throw new Error("Failed to fetch markdown");
const markdown = await res.text();
const rawHtml = await marked.parse(markdown);
document.getElementById("content").innerHTML = DOMPurify.sanitize(rawHtml);
}
Honest Trade-Offs: Where It Wins—and Why Next.js Eventually Replaced It
- Zero Server Compute (Pro): Every HTML, JSON, and Markdown file is cached at the CDN edge—no database credentials to leak, no SQL injection surface, and zero idle server bills.
- Git-Native Publishing (Pro): Publishing an article is as simple as committing a Markdown file and updating
posts.json. - Client Fetch Waterfall (Con): Unlike server-rendered HTML, the browser must sequentially load
post.html→app.js→posts.json→article.mdbefore painting text. - SEO & OpenGraph Limits (Con): Because
<title>and<meta property="og:*">tags aren't pre-rendered per article on the server, social preview crawlers only see the genericpost.htmlshell.
Summary
Client-side Markdown rendering on static hosting is a clean, zero-cost pattern for lightweight prototypes and documentation tools—provided you sanitize HTML output properly. Once per-article OpenGraph metadata, instant first-contentful paint, and server-side API routes became priorities for this portfolio, graduating to Next.js Static Site Generation (generateStaticParams) and React Server Components gave us the best of both worlds: pre-rendered HTML at the edge with zero client waterfalls.