Age-verification laws changed how many sites decide who gets a page. About half of US states now have one. So do the UK, France, Italy, Germany, Australia and others. If your site is covered, "block by country" stops being enough.
I run an adult film catalogue called PornSleuth on Cloudflare Workers. This post is only about the plumbing. I'll cover how the Worker decides, what it returns, and how search crawlers fit in. Everything below is simplified from the real code.
I'm not a lawyer, and nothing here is legal advice. The rules the Worker applies are data I maintain, not code. That is deliberate, and I'll come back to it.
The problem
Three things make this harder than a country allow-list.
- Laws are regional. In the US the rule is per state. A visitor from the US is not one case. They are fifty.
- Rules change. Laws take effect on set dates. Hard-coding a list in your Worker means a deploy for every change.
- Search engines crawl from odd places. Early on, every Googlebot request I counted came from an address Cloudflare placed in a restricted region. Googlebot was being told the site was unavailable. Search Console accepted the sitemap, then reported an error fetching it.
The third one is the trap. A geo block that works for people can quietly delist you.
Reading the location
Cloudflare gives a Worker the visitor's location on request.cf. I use country and regionCode:
function requestLocation(request: Request) { const cf = (request as any).cf; const country = typeof cf?.country === "string" && /^[A-Z]{2}$/.test(cf.country) && cf.country !== "XX" && // unknown cf.country !== "T1" // Tor ? cf.country : null; const region = typeof cf?.regionCode === "string" && /^[A-Z0-9]{1,3}$/.test(cf.regionCode) ? cf.regionCode : null; return { country, region }; } Two details matter here.
First, the value is validated, not trusted. Anything that isn't a two-letter code, or is XX or T1, becomes null.
Second, null is a real answer. It means "we don't know", and the next step has to handle it.
The location is used to make the decision and is not stored against the visitor.
Rules live in a table
The Worker doesn't contain a list of states. It reads rules from a database table. Each row is a country, or a country plus a region, with one of three outcomes: open, age_check or blocked. A row can also carry an upcoming rule with a start date. Each row records its legal basis, a source link and a review date.
The decision function fails closed:
function resolveRule(rules, { country, region }, today) { if (!country) return "age_check"; // no location const own = rules.filter((r) => r.country === country); const regional = own.filter((r) => r.region !== ""); if (regional.length) { const match = region && regional.find((r) => r.region === region); return match ? effective(match, today) : "age_check"; // region unknown or unlisted } const whole = own.find((r) => r.region === ""); return whole ? effective(whole, today) : "age_check"; // country unlisted } So each of these ends up restricted:
- no location,
- a country with no row,
- a country that has regional rows when the region is unknown or unlisted.
A US visitor with no state is treated as restricted. Nothing opens the gate by default.
Start dates need care. A law's start date is local time somewhere between UTC-12 and UTC+14, but the Worker compares against the UTC date. So a tightening rule starts a day early and a loosening rule a day late. A rule is never early or late anywhere.
If the rules can't be read at all, the outcome is unavailable. The Worker keeps using the last rules it read for up to 15 minutes, so a known block doesn't lapse during a database blip. After that, nothing unlocks.
What a blocked visitor gets
Where the rule says a check is needed, or the place is blocked outright, catalogue pages answer with HTTP 451, "Unavailable For Legal Reasons". The body is a short page saying the site isn't available where you are.
In the middleware it's roughly this:
async function unavailable(next) { const page = await next("/unavailable"); return privateHeaders( new Response(page.body, { status: 451, headers: page.headers }) ); } privateHeaders adds X-Robots-Tag: noindex, nofollow and Cache-Control: private, no-store. A 451 is never cached for anyone else, and never indexed.
Some paths stay reachable from a blocked place: legal pages, sign-in, the operator's admin area, the sitemap and, helpfully, the public page that explains which laws apply. People who are blocked can still read why.
I chose 451 over 403 or 404 because it says what is happening. It's also a status code that clients and crawlers can distinguish from "gone".
The crawler problem
Now the trap. A crawler's request has a location too, and it comes from a data centre. If the rules put that place in a restricted region, the crawler gets a 451 for every page.
There are two bad answers.
- Let anything calling itself Googlebot through. The user agent is a string anyone can send. This is a hole in your block.
- Block by location and accept the consequences. You get the delisting I described above.
What the Worker does instead is verify the crawler, then serve it the page an open-region visitor gets.
Verification
A request counts as a search crawler only if both are true:
- the user agent names the crawler, and
- the connecting address is inside that engine's published address list (Google and Bing each publish a JSON list of ranges).
async function verifiedCrawler(request: Request) { const ua = request.headers.get("user-agent"); const ip = request.headers.get("cf-connecting-ip"); if (!ip) return null; const bot = crawlerName(ua); // regex over user agents if (!bot) return null; // ordinary visitor try { const nets = await rangesFor(bot); if (!nets) return "unverifiable"; // can't read the list return inNets(ip, nets) ? bot : null; // wrong address: ordinary visitor } catch { return "unverifiable"; } } A name without a matching address is an ordinary visitor, so it gets whatever its location earns. In a restricted place, that's a 451. A spoofed Googlebot gets nothing extra.
The address lists come from a fetch the Worker refreshes about once a day per isolate. I found out the hard way that this fetch can fail. At one point Google's server answered the Worker's request with a 429, and every Googlebot request hit the "can't verify" branch. The fix was a copy of both lists bundled with the code, used when the live fetch fails. The bundled copy is regenerated by a script and committed, and a stale copy means a brand new Google address is treated as an ordinary visitor.
I use the published lists, not Cloudflare's verified-bot signal. I haven't compared the two in production, so I can't say which is better. [CHECK]
What the crawler is served
A verified crawler is served as an anonymous visitor in a fixed open place: California.
const CATALOGUE_LOCATION = { country: "US", region: "CA" }; const location = crawler ? CATALOGUE_LOCATION : requestLocation(request); return withLocation(location, () => handle(request)); Everything downstream reads the location from that scope: the rules check, the database reads that decide which items are visible, and the page cache. The crawler goes through the same code path as a person in California.
It does not get a special page. It gets the same page an allowed-region visitor gets.
Why this isn't cloaking
Cloaking means showing a search engine content that users don't see. That is what these rules are meant to avoid, so I'll be specific about what is and isn't going on.
- The crawler sees the same content, projection and filters as any anonymous visitor in an open place. The page is the one I serve to a real person in California.
- The Worker doesn't add content, keywords or links for crawlers.
- The one difference is access. A person in a restricted place gets a 451. A crawler whose server happens to sit in the same place doesn't. The rules govern people's access, and a crawler is not a person.
That last point is a judgement call, not a technicality, and I'm not claiming it settles the legal question. I made the call and recorded it as a decision, with the trade-off written down: the state and country rules no longer apply to the search engines' crawlers. If you copy this pattern, make that decision on purpose, and write it down.
[CHECK: confirm with Phil he is happy for the post to say this is a recorded business decision without legal sign-off.]
When things break: 503, not 451
Two failure cases come up.
- A request names a crawler but its address list can't be read. The Worker answers
503withRetry-After, not the 451 an ordinary visitor in that place would get. A 503 says "come back later". A 451 says "not here". - The switches or location rules can't be read. The public site answers 503 as well, instead of falling back to a closed-site response whose 404s and
Disallow: /could get pages dropped.
const retryLater = () => new Response("Temporarily unavailable", { status: 503, headers: { "Retry-After": "120" }, }); One exception: /robots.txt skips all of this. It's the same file for everyone, and an error on it can make Google stop crawling the whole site. It never gets the retry 503.
What I'd tell you to check
If you build something like this, check these before you ship.
- Region, not just country.
request.cf.countryalone misses every US state rule. - Unknown is a state. Decide what
nullmeans. For me it means restricted. - Rules as data. Review dates and start dates in a table beat an array in your code.
- Crawler verification needs two signals. User agent plus a published address list. Either alone is easy to fake or to break.
- Plan for the list fetch failing. Bundle a copy.
- Cache per place. If you cache pages, the cache key has to include the location, or one visitor's answer becomes everyone's. A 451 should never be cacheable.
- Log what crawlers actually get. I count crawler requests by whether they verified. That is how I found the problem.
- Test after deploy. The restricted-place check looks fine in staging right up until a crawler hits it.
The data
I keep the table of laws I use public. It lists each place, the law, the start date, a source and whether I've confirmed it against an official source. The live tracker is checked daily. A CC BY 4.0 snapshot of the same table, as a CSV, is on GitHub. It's general information, not legal advice. If you spot a mistake, tell me.
If you've handled the crawler side differently, I'd like to hear how, particularly if you rely on Cloudflare's own bot signals.

