Skip to content
All writing

One Cloudflare Worker for every university subdomain

Quiz Slayer started as a quiz app for my own class. When other sections wanted it, every university needed its own subdomain. Here is how one Cloudflare Worker serves them all, and the route that stops it answering hosts it shouldn't.

Quiz Slayer began in March as a practice app for my own class: MCQs ten at a time, with the reason after every answer. By the November midterms about 60 classmates had answered 20,154 questions on it. Then the other sections asked for it, and so did people at other universities. (The human side of that story is on quizslayer.app.)

One class needs one app. Many universities need each university to feel like its own place, with its own sections, boards and CRs, without me deploying anything when a new one joins. So each university gets an address: superior.quizslayer.app first, and later <anything>.quizslayer.app.

Pages couldn't take a wildcard

My plan was Cloudflare Pages. Its custom-domain docs only cover single hostnames, and a wildcard there is undocumented. Workers Custom Domains don't take wildcards either. What does work is a Workers Route: *.quizslayer.app/* matches every first-level subdomain, but not the apex, and it needs a proxied wildcard * DNS record behind it.

So the student app is one Worker with static assets, qs-uni:

"assets": {
  "directory": "./dist",
  "not_found_handling": "single-page-application",
  "run_worker_first": ["/api/*"]
},
"routes": [{ "pattern": "*.quizslayer.app/*", "zone_name": "quizslayer.app" }]

That's step 1 in the drawing below. Only /api/* runs Worker code. Every other request is a static asset, so a new university adds files served, not code run.

The wildcard catches too much

A wildcard route answers every host under the zone, including the ones that belong to something else. Cloudflare picks the most specific route, so console.quizslayer.app/* goes to the console's own Worker without any extra work.

content.quizslayer.app is harder. It's the R2 bucket that holds the question banks, and an R2 custom domain isn't a Worker, so no Worker config can claim it. Without a fix, the wildcard would send it to qs-uni (step 2). The fix is a route with no Worker on it at all. Cloudflare's docs say a route like that "will act to negate any less specific patterns".

Which host reaches which part of Quiz Slayer

Four hosts and the Cloudflare route each one matches. Step 1: superior.quizslayer.app matches the wildcard route *.quizslayer.app/* and reaches the qs-uni Worker, which reads the hostname to find the university. console.quizslayer.app matches its own more specific route and reaches the console Worker. Step 2: content.quizslayer.app matches a route with no Worker, so it reaches the R2 bucket; without that route the wildcard would answer it. The apex quizslayer.app is a Custom Domain on the marketing Worker.

HOSTROUTESERVED BYsuperior.quizslayer.appconsole.quizslayer.appcontent.quizslayer.appquizslayer.app*.quizslayer.app/*content.quizslayer.app/*console.quizslayer.app/*apexqs-uni WorkerR2 bucketconsole Workerqs-marketingwildcard routeroute with no Workermore specific, winsCustom Domainreads the hostnamequestion banksstatic assetsAstro, static12Without the route at step 2, the wildcard would answer content.quizslayer.app too.
Which host reaches which part of Quiz Slayer

Four hosts and the Cloudflare route each one matches. Step 1: superior.quizslayer.app matches the wildcard route *.quizslayer.app/* and reaches the qs-uni Worker, which reads the hostname. Step 2: content.quizslayer.app matches a route with no Worker and reaches the R2 bucket; without that route the wildcard would answer it. console.quizslayer.app matches its own more specific route. The apex is a Custom Domain on the marketing Worker.

HOST · ROUTE · SERVED BYsuperior.quizslayer.app*.quizslayer.app/*qs-uni Workercontent.quizslayer.appcontent.quizslayer.app/*R2 bucketconsole.quizslayer.appconsole.quizslayer.app/*console Workerquizslayer.appapexqs-marketingwildcardreads the hostnameno Workerquestion banksmore specificCustom DomainAstro, static12Without step 2, qs-uni would answer this host.

The most specific route wins, so the one host that isn't a Worker needs a route with no Worker on it, or the wildcard takes it.

The hostname is the tenant

The Worker serves the same files to every subdomain. The app works out which university it is from location.hostname, before it asks the server anything:

export function subdomainFromHost(hostname: string): HostTenant {
  const host = hostname.trim().toLowerCase().replace(/\.$/, '')
  if (host === BASE_DOMAIN || !host.endsWith(`.${BASE_DOMAIN}`)) return { kind: 'none' }
  const label = host.slice(0, -(BASE_DOMAIN.length + 1))
  return isUniSubdomain(label) ? { kind: 'subdomain', subdomain: label } : { kind: 'invalid' }
}

isUniSubdomain refuses a reserved list: www, console, content, and names like mail and autodiscover. A university that owned one of those could receive traffic meant for the platform. The list's comment says "Extend, never shorten". On the server, Convex looks the label up by an index and returns one of three answers: this university, "you belong to another one, go there", or "No such university" for anything missing, draft or archived. It never takes more than three reads.

So adding a university is a row in the database. No build, no DNS change, no deploy.

The one thing that isn't automatic

Google sign-in. The auth library takes an exact list of origins it may send people back to, with no wildcard or pattern. So each new university adds one origin to the server's settings, and until it's there the server tells the app that sign-in isn't ready on that address. Preview URLs are switched off too: a preview couldn't sign in, and it would read production data.

What I'd tell myself in March

If your app might ever be per-tenant, keep the tenant out of the build and read it from the host. Then the hosting question shrinks to one route, plus one negating route for each host under the zone that isn't yours to serve.

The scoring that keeps the boards fair has its own post, and the case study has the rest. This is the kind of web engineering I do for clients too.