The Problem: Hosting an Ad-Monetized Side Project Without a $20/mo Tax
Last week I launched a lightweight web app, BFF Quiz Maker, where friends build an 8-question friendship test in 20 seconds, share a short link (/q/abc1234), and track scores on a live leaderboard, alongside 5 clean browser party game decks (Would You Rather, Truth or Dare, Never Have I Ever, Most Likely To, and Charades).
I started on Vercel Hobby + Upstash Redis because it was the fastest path to ship. Within a few days, two practical walls showed up:
- Commercial terms: Vercel's free Hobby plan is strictly non-commercial. If you plan to put AdSense or any monetization on a small tool site, you need Pro ($20/month per seat) before making your first dollar.
- External database overhead: Keeping a separate Redis vendor just to store small JSON quiz payloads and ranked score rows felt unnecessary when the traffic pattern is 95% static pages plus 3 tiny API endpoints.
Cloudflare Pages + Pages Functions + D1 solved both cleanly:
- Cloudflare Pages allows commercial sites on the free tier with unlimited static bandwidth.
- Cloudflare D1 gives a serverless SQLite database right next to your edge functions (5M row reads and 100k writes per day on the free tier).
- Zero vendor sprawl: DNS, SSL, static assets, API routes, and SQLite live in one dashboard.
Here is the exact migration pattern I used, including the one tricky part with dynamic Open Graph tags under Next.js static export.
Step 1: Switch Next.js to Pure Static Export (out/)
Instead of wrapping Next.js SSR in a heavy worker adapter, I switched next.config.ts to plain static export:
import type { NextConfig } from "next"; const nextConfig: NextConfig = { output: "export", trailingSlash: false, images: { unoptimized: true }, }; export default nextConfig; Gotcha #1: Next.js metadata routes (sitemap.ts, robots.ts, manifest.ts, opengraph-image.tsx) will fail next build under output: "export" unless you add:
export const dynamic = "force-static"; Once added, npm run build outputs all 17 static landing pages, sitemap, robots, and OG image cleanly into out/.
Step 2: Replace Redis With a 2-Table D1 Schema
Replacing key-value Redis storage with relational SQLite actually made the live scoreboard simpler and faster to query. Here is the entire schema.sql:
CREATE TABLE IF NOT EXISTS quizzes ( id TEXT PRIMARY KEY, name TEXT NOT NULL, theme TEXT NOT NULL DEFAULT 'coral', answers_json TEXT NOT NULL, questions_json TEXT NOT NULL, created_at INTEGER NOT NULL ); CREATE TABLE IF NOT EXISTS scores ( id TEXT PRIMARY KEY, quiz_id TEXT NOT NULL, friend_name TEXT NOT NULL, score INTEGER NOT NULL, total INTEGER NOT NULL, pct INTEGER NOT NULL, stamp TEXT NOT NULL, emoji_grid TEXT NOT NULL, created_at INTEGER NOT NULL ); CREATE INDEX IF NOT EXISTS idx_scores_quiz_rank ON scores (quiz_id, score DESC, created_at ASC); With the composite index (quiz_id, score DESC, created_at ASC), fetching a ranked leaderboard is a single indexed SELECT with zero application-side sorting.
Step 3: Move API Routes to Cloudflare Pages functions/
Cloudflare Pages automatically compiles any TypeScript files under the repo's /functions directory into edge Workers alongside your static out/ bundle. I moved the 3 API endpoints out of src/app/api/ into:
-
functions/api/quizzes/index.ts->POST /api/quizzes(validates payload, writes to D1, returns a 7-char short ID) -
functions/api/quizzes/[id].ts->GET /api/quizzes/:id(returns quiz + ranked scoreboard) -
functions/api/quizzes/[id]/scores.ts->POST /api/quizzes/:id/scores(server-side grading so scores cannot be forged client-side)
I also kept a graceful fallback: if context.env.DB is missing during local static previews, POST /api/quizzes returns { mode: "stateless" } so the frontend silently falls back to a URL-encoded share link instead of breaking.
Step 4: The Tricky Part: Dynamic Open Graph Tags Without SSR
The biggest downside of output: "export" is losing dynamic generateMetadata().
When someone shares https://bffquizmaker.com/q/adypk55 in iMessage or WhatsApp, a generic page title kills click-through rate. It needs to render:
Can you beat Mia's Friendship Test?
8 questions, zero mercy. Guess Mia's real answers and see where you land on the live scoreboard.
The clean Cloudflare-native fix is pairing a single static shell page (src/app/q/page.tsx -> out/q.html) with a Pages Function at functions/q/[id].ts using Cloudflare's streaming HTMLRewriter:
export async function onRequestGet(context: PagesContext<{ id: string }>) { const id = context.params.id ?? ""; // Fetch the static /q shell built by Next.js const shellUrl = new URL("/q", context.request.url); const assetRes = await context.env.ASSETS.fetch(shellUrl); const headers = new Headers(assetRes.headers); // Keep user-generated quiz links out of Google's index headers.set("X-Robots-Tag", "noindex, nofollow"); headers.set("Cache-Control", "no-store"); const row = await context.env.DB.prepare( "SELECT name, answers_json FROM quizzes WHERE id = ?" ) .bind(id) .first<{ name: string; answers_json: string }>(); const response = new Response(assetRes.body, { status: assetRes.status, headers, }); if (!row) return response; const ogTitle = `Can you beat ${row.name}'s Friendship Test?`; const description = `Guess ${row.name}'s real answers and see where you land on the live scoreboard.`; return new HTMLRewriter() .on("title", { element(el) { el.setInnerContent(`${ogTitle} | BFF Quiz Maker`); } }) .on('meta[property="og:title"]', { element(el) { el.setAttribute("content", ogTitle); } }) .on('meta[property="og:description"]', { element(el) { el.setAttribute("content", description); } }) .on('meta[name="twitter:title"]', { element(el) { el.setAttribute("content", ogTitle); } }) .transform(response); } This gave us:
- Full static speed for all SEO landing pages.
- Personalized social preview cards for every
/q/:idlink at the edge. -
X-Robots-Tag: noindex, nofollowon every user-generated quiz URL so Google only indexes our core landing pages.
Two Deployment Gotchas That Saved Me Hours
- D1 bindings do not apply until you trigger a Retry Deployment: Cloudflare's initial Pages setup form has no D1 binding field. You deploy once, add
DB -> bff-quiz-dbunderSettings -> Bindings, and then must clickDeployments -> Retry deploymentbeforecontext.env.DBactually attaches to your Functions. - Verify everything on
*.pages.devbefore touching DNS: I ran a full end-to-end check onbff-quiz-maker.pages.dev(create quiz -> inspect raw HTML headers -> finish quiz as a friend -> reload scoreboard) before addingbffquizmaker.comunder Custom Domains. Because DNS was already on Cloudflare, activating the custom domain swapped the old Vercel CNAME automatically with zero downtime.
Result
- Monthly hosting & database cost: $0/mo (commercial-friendly)
- Full static export + edge Functions + D1: live at bffquizmaker.com
- Source code: github.com/zjolnir/bff-quiz-maker
If you are building content + lightweight interactive tools on Next.js and do not actually need full-page Node.js SSR on every route, output: "export" + Pages Functions + D1 + HTMLRewriter is a surprisingly clean stack to run indefinitely at zero fixed cost.