<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Phil Bishop</title>
    <description>The latest articles on DEV Community by Phil Bishop (@phil_bishop_87292d6835cda).</description>
    <link>https://dev.to/phil_bishop_87292d6835cda</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4265762%2F3b0cbd77-795d-47f8-8e70-e626ac11e276.png</url>
      <title>DEV Community: Phil Bishop</title>
      <link>https://dev.to/phil_bishop_87292d6835cda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/phil_bishop_87292d6835cda"/>
    <language>en</language>
    <item>
      <title>Blocking by country on Cloudflare Workers (HTTP 451) without blocking search crawlers</title>
      <dc:creator>Phil Bishop</dc:creator>
      <pubDate>Thu, 26 Nov 2026 03:33:50 +0000</pubDate>
      <link>https://dev.to/phil_bishop_87292d6835cda/blocking-by-country-on-cloudflare-workers-http-451-without-blocking-search-crawlers-5170</link>
      <guid>https://dev.to/phil_bishop_87292d6835cda/blocking-by-country-on-cloudflare-workers-http-451-without-blocking-search-crawlers-5170</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;Three things make this harder than a country allow-list.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Laws are regional.&lt;/strong&gt; In the US the rule is per state. A visitor from the US is not one case. They are fifty.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rules change.&lt;/strong&gt; Laws take effect on set dates. Hard-coding a list in your Worker means a deploy for every change.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search engines crawl from odd places.&lt;/strong&gt; 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.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The third one is the trap. A geo block that works for people can quietly delist you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading the location
&lt;/h2&gt;

&lt;p&gt;Cloudflare gives a Worker the visitor's location on &lt;code&gt;request.cf&lt;/code&gt;. I use &lt;code&gt;country&lt;/code&gt; and &lt;code&gt;regionCode&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;requestLocation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;cf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cf&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;cf&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="o"&gt;/^&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="no"&gt;Z&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;$&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
    &lt;span class="n"&gt;cf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"XX"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="c1"&gt;// unknown&lt;/span&gt;
    &lt;span class="n"&gt;cf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"T1"&lt;/span&gt;    &lt;span class="c1"&gt;// Tor&lt;/span&gt;
      &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;cf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="n"&gt;cf&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;regionCode&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"string"&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;/^&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="no"&gt;Z0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;$&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;regionCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;cf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;regionCode&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two details matter here.&lt;/p&gt;

&lt;p&gt;First, the value is validated, not trusted. Anything that isn't a two-letter code, or is &lt;code&gt;XX&lt;/code&gt; or &lt;code&gt;T1&lt;/code&gt;, becomes &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Second, &lt;code&gt;null&lt;/code&gt; is a real answer. It means "we don't know", and the next step has to handle it.&lt;/p&gt;

&lt;p&gt;The location is used to make the decision and is not stored against the visitor.&lt;/p&gt;

&lt;h2&gt;
  
  
  Rules live in a table
&lt;/h2&gt;

&lt;p&gt;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: &lt;code&gt;open&lt;/code&gt;, &lt;code&gt;age_check&lt;/code&gt; or &lt;code&gt;blocked&lt;/code&gt;. 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.&lt;/p&gt;

&lt;p&gt;The decision function fails closed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;resolveRule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;today&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"age_check"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// no location&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;own&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rules&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;regional&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;own&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;regional&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;regional&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;effective&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;match&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;today&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"age_check"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// region unknown or unlisted&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;whole&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;own&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;region&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;whole&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;effective&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;whole&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;today&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"age_check"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// country unlisted&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So each of these ends up restricted:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;no location,&lt;/li&gt;
&lt;li&gt;a country with no row,&lt;/li&gt;
&lt;li&gt;a country that has regional rows when the region is unknown or unlisted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A US visitor with no state is treated as restricted. Nothing opens the gate by default.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;If the rules can't be read at all, the outcome is &lt;code&gt;unavailable&lt;/code&gt;. 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What a blocked visitor gets
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;In the middleware it's roughly this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;unavailable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"/unavailable"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;privateHeaders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;451&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;privateHeaders&lt;/code&gt; adds &lt;code&gt;X-Robots-Tag: noindex, nofollow&lt;/code&gt; and &lt;code&gt;Cache-Control: private, no-store&lt;/code&gt;. A 451 is never cached for anyone else, and never indexed.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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".&lt;/p&gt;

&lt;h2&gt;
  
  
  The crawler problem
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;There are two bad answers.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Let anything calling itself Googlebot through.&lt;/strong&gt; The user agent is a string anyone can send. This is a hole in your block.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Block by location and accept the consequences.&lt;/strong&gt; You get the delisting I described above.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What the Worker does instead is verify the crawler, then serve it the page an open-region visitor gets.&lt;/p&gt;

&lt;h3&gt;
  
  
  Verification
&lt;/h3&gt;

&lt;p&gt;A request counts as a search crawler only if both are true:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;the user agent names the crawler, and&lt;/li&gt;
&lt;li&gt;the connecting address is inside that engine's published address list (Google and Bing each publish a JSON list of ranges).
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;verifiedCrawler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ua&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"user-agent"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;ip&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"cf-connecting-ip"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;bot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;crawlerName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ua&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// regex over user agents&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;bot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// ordinary visitor&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;nets&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;rangesFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;nets&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"unverifiable"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// can't read the list&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;inNets&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ip&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nets&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="n"&gt;bot&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// wrong address: ordinary visitor&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"unverifiable"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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]&lt;/p&gt;

&lt;h3&gt;
  
  
  What the crawler is served
&lt;/h3&gt;

&lt;p&gt;A verified crawler is served as an anonymous visitor in a fixed open place: California.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;CATALOGUE_LOCATION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"US"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;region&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"CA"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;crawler&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="no"&gt;CATALOGUE_LOCATION&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;requestLocation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;withLocation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;handle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;It does not get a special page. It gets the same page an allowed-region visitor gets.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why this isn't cloaking
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;li&gt;The Worker doesn't add content, keywords or links for crawlers.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;[CHECK: confirm with Phil he is happy for the post to say this is a recorded business decision without legal sign-off.]&lt;/p&gt;

&lt;h2&gt;
  
  
  When things break: 503, not 451
&lt;/h2&gt;

&lt;p&gt;Two failure cases come up.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A request names a crawler but its address list can't be read. The Worker answers &lt;code&gt;503&lt;/code&gt; with &lt;code&gt;Retry-After&lt;/code&gt;, not the 451 an ordinary visitor in that place would get. A 503 says "come back later". A 451 says "not here".&lt;/li&gt;
&lt;li&gt;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 &lt;code&gt;Disallow: /&lt;/code&gt; could get pages dropped.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;retryLater&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Temporarily unavailable"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;503&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"Retry-After"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"120"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One exception: &lt;code&gt;/robots.txt&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell you to check
&lt;/h2&gt;

&lt;p&gt;If you build something like this, check these before you ship.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Region, not just country.&lt;/strong&gt; &lt;code&gt;request.cf.country&lt;/code&gt; alone misses every US state rule.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unknown is a state.&lt;/strong&gt; Decide what &lt;code&gt;null&lt;/code&gt; means. For me it means restricted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rules as data.&lt;/strong&gt; Review dates and start dates in a table beat an array in your code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Crawler verification needs two signals.&lt;/strong&gt; User agent plus a published address list. Either alone is easy to fake or to break.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plan for the list fetch failing.&lt;/strong&gt; Bundle a copy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache per place.&lt;/strong&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Log what crawlers actually get.&lt;/strong&gt; I count crawler requests by whether they verified. That is how I found the problem.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Test after deploy.&lt;/strong&gt; The restricted-place check looks fine in staging right up until a crawler hits it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The data
&lt;/h2&gt;

&lt;p&gt;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 &lt;a href="https://pornsleuth.com/age-verification-laws" rel="noopener noreferrer"&gt;live tracker&lt;/a&gt; is checked daily. A CC BY 4.0 snapshot of the same table, as a CSV, is on &lt;a href="https://github.com/pornsleuth/age-verification-laws" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. It's general information, not legal advice. If you spot a mistake, tell me.&lt;/p&gt;

&lt;p&gt;If you've handled the crawler side differently, I'd like to hear how, particularly if you rely on Cloudflare's own bot signals.&lt;/p&gt;

</description>
      <category>cloudflare</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>opensource</category>
    </item>
    <item>
      <title>An open dataset of age-verification laws for adult sites (CSV, JSON, Cloudflare rules)</title>
      <dc:creator>Phil Bishop</dc:creator>
      <pubDate>Thu, 26 Nov 2026 02:03:53 +0000</pubDate>
      <link>https://dev.to/phil_bishop_87292d6835cda/an-open-dataset-of-age-verification-laws-for-adult-sites-csv-json-cloudflare-rules-57p5</link>
      <guid>https://dev.to/phil_bishop_87292d6835cda/an-open-dataset-of-age-verification-laws-for-adult-sites-csv-json-cloudflare-rules-57p5</guid>
      <description>&lt;p&gt;If you run a site with adult content, the legal map changes every few months. Twenty-seven US states and eight countries now require adult sites to check that visitors are 18 or over, and several more have laws passed but not yet in force.&lt;/p&gt;

&lt;p&gt;We keep that map as a table, checked daily, and publish it as open data under CC BY 4.0.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's in it
&lt;/h2&gt;

&lt;p&gt;One row per country or US state:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;status&lt;/strong&gt;: age check required, law passed but not in force, adult sites prohibited, or no age-check law&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;law&lt;/strong&gt;: the law's name or number&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;effective_from&lt;/strong&gt;: when it took effect&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;source&lt;/strong&gt;: an official or industry source for the row&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;verified&lt;/strong&gt;: &lt;code&gt;yes&lt;/code&gt; once checked against an official source&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The live table is the &lt;a href="https://pornsleuth.com/age-verification-laws" rel="noopener noreferrer"&gt;age verification laws tracker&lt;/a&gt;. A snapshot lives in the &lt;a href="https://github.com/pornsleuth/age-verification-laws" rel="noopener noreferrer"&gt;GitHub repository&lt;/a&gt;, with a CSV you can pull straight into a build.&lt;/p&gt;

&lt;h2&gt;
  
  
  Using it at the edge
&lt;/h2&gt;

&lt;p&gt;The repo's &lt;a href="https://github.com/pornsleuth/age-verification-laws/blob/main/EXAMPLES.md" rel="noopener noreferrer"&gt;examples file&lt;/a&gt; turns the CSV into Cloudflare WAF expressions. For US states, Cloudflare exposes the visitor's state as &lt;code&gt;ip.src.subdivision_1_iso_code&lt;/code&gt; (for example &lt;code&gt;US-TX&lt;/code&gt;), so a rule for every state with a law in force looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(ip.src.country eq "US" and ip.src.subdivision_1_iso_code in {"US-TX" "US-UT" ...})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a Worker, the same field is &lt;code&gt;request.cf.regionCode&lt;/code&gt;. The examples also include a JSON list of the countries where adult sites are prohibited outright, and a short script to regenerate everything when the CSV changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Caveats
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;This is general information, not legal advice.&lt;/li&gt;
&lt;li&gt;IP location is approximate; VPNs exist. Geo rules are a floor, not a compliance programme.&lt;/li&gt;
&lt;li&gt;Laws move. Use the live page for decisions and the CSV for automation, and refresh it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Spotted a mistake or a missing law? Open an issue on the repo. Corrections are what keep this useful.&lt;/p&gt;

</description>
      <category>opendata</category>
      <category>webdev</category>
      <category>security</category>
      <category>compliance</category>
    </item>
  </channel>
</rss>
