<?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: ItsWempo</title>
    <description>The latest articles on DEV Community by ItsWempo (@itswempo).</description>
    <link>https://dev.to/itswempo</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%2F4258240%2F046f1f51-3be2-4036-8af7-96958708fe95.png</url>
      <title>DEV Community: ItsWempo</title>
      <link>https://dev.to/itswempo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/itswempo"/>
    <language>en</language>
    <item>
      <title>I built a forum with no framework on Cloudflare Workers</title>
      <dc:creator>ItsWempo</dc:creator>
      <pubDate>Sun, 22 Nov 2026 04:05:16 +0000</pubDate>
      <link>https://dev.to/itswempo/i-built-a-forum-with-no-framework-on-cloudflare-workers-o8d</link>
      <guid>https://dev.to/itswempo/i-built-a-forum-with-no-framework-on-cloudflare-workers-o8d</guid>
      <description>&lt;p&gt;I run a small forum for the &lt;a href="https://polytoria.com" rel="noopener noreferrer"&gt;Polytoria&lt;/a&gt; game&lt;br&gt;
community: &lt;a href="https://wempos-forum.com" rel="noopener noreferrer"&gt;wempos-forum.com&lt;/a&gt;. It has boards,&lt;br&gt;
threads, polls, a chat lobby, DMs, profiles, two-factor sign-in and 41&lt;br&gt;
interface languages. The whole thing is one Cloudflare Worker, one D1&lt;br&gt;
database, and plain JavaScript modules in the browser. No React, no&lt;br&gt;
bundler, no build step for the front end.&lt;/p&gt;

&lt;p&gt;This post is about the decisions that made that work, the parts that bit&lt;br&gt;
me, and some code you can steal. The project is free software&lt;br&gt;
(AGPL-3.0-or-later), so all of it is in&lt;br&gt;
&lt;a href="https://github.com/Wempo-s-Silly-Projects/wempos-forum" rel="noopener noreferrer"&gt;the repo&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  The shape of it
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;browser ──► Worker (worker.mjs)
              ├── /api/*        JSON routes, one handler module per area
              ├── /mcp          a tool server for AI agents
              ├── discovery     /openapi.json, sitemap, markdown versions
              └── everything else → static files in public/, with a
                  per-page &amp;lt;head&amp;gt; injected on the way out
            D1 (SQLite)  ◄── every route, through bound statements
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The router is a list of handlers. Each one answers the paths it owns and&lt;br&gt;
returns &lt;code&gt;null&lt;/code&gt; for everything else, so the next handler gets a turn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="no"&gt;API_HANDLERS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;handleAuthRoutes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handlePostRoutes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handleProfileRoutes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="cm"&gt;/* …30 more… */&lt;/span&gt; &lt;span class="n"&gt;handleBrandingRoutes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;handleAnalyticsRoutes&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;Adding a feature means adding one module and one entry. There's no&lt;br&gt;
framework to learn, and every route is a plain function you can call&lt;br&gt;
from a test.&lt;/p&gt;
&lt;h2&gt;
  
  
  No front-end build step
&lt;/h2&gt;

&lt;p&gt;The browser loads &lt;code&gt;public/js/*.js&lt;/code&gt; exactly as written, as ES modules.&lt;br&gt;
That sounded like a constraint at first. In practice it means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;What you read in the repo is what runs.&lt;/strong&gt; No source maps to decode
when a member reports a bug.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The client router is also the server's source of truth.&lt;/strong&gt; The Worker
imports the same &lt;code&gt;public/js/router.js&lt;/code&gt; the browser uses, so a URL the
client can't route gets a real &lt;code&gt;404&lt;/code&gt; from the server too. One list of
page routes, no drift.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A strict Content-Security-Policy is easy.&lt;/strong&gt; No inline scripts, no
&lt;code&gt;eval&lt;/code&gt;, &lt;code&gt;script-src 'self'&lt;/code&gt;. There's nothing to whitelist because
there's nothing generated.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The cost is that I write a bit more DOM code by hand. The rule that keeps&lt;br&gt;
it safe: user text goes in with &lt;code&gt;textContent&lt;/code&gt;, never &lt;code&gt;innerHTML&lt;/code&gt;, and a&lt;br&gt;
test fails the build if a new HTML sink shows up.&lt;/p&gt;
&lt;h2&gt;
  
  
  One visibility rule, everywhere
&lt;/h2&gt;

&lt;p&gt;Forums leak in boring ways. A post gets hidden by a moderator, but it&lt;br&gt;
still shows in search. A member blocks someone, but that person's replies&lt;br&gt;
still appear in a notification. A banned account's posts vanish from the&lt;br&gt;
feed but not from the sitemap.&lt;/p&gt;

&lt;p&gt;So there's exactly one module that decides "can this viewer see this&lt;br&gt;
post?", covering hidden posts, active bans and block pairs in both&lt;br&gt;
directions. Every route that returns post text or member names goes&lt;br&gt;
through it. Then there's a test that seeds one of each forbidden thing&lt;br&gt;
(a hidden post, a banned author, a blocked pair, a deleted post, a&lt;br&gt;
staff-only record) and sweeps &lt;strong&gt;every&lt;/strong&gt; route, the agent tool server and&lt;br&gt;
the page heads, asserting none of that text ever comes back.&lt;/p&gt;

&lt;p&gt;That sweep has paid for itself. In the last release it caught a subtle&lt;br&gt;
one: the structured data on a thread page counted replies from someone the&lt;br&gt;
viewer had blocked, while the feed (correctly) didn't. Same SQL, one&lt;br&gt;
missing filter. Without the sweep, nobody would have noticed for months.&lt;/p&gt;
&lt;h2&gt;
  
  
  Link previews that respect hidden posts
&lt;/h2&gt;

&lt;p&gt;When someone pastes a thread link into Discord, it should show the&lt;br&gt;
thread's title and first lines. When the thread is hidden, it must show&lt;br&gt;
&lt;em&gt;nothing&lt;/em&gt; about it, and it must not even reveal whether the thread&lt;br&gt;
exists.&lt;/p&gt;

&lt;p&gt;The Worker rewrites the HTML shell's &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; per request. The important&lt;br&gt;
part is the order: the visibility check runs first, as an ordinary&lt;br&gt;
member, even for staff, so a moderator's session can't leak a hidden post&lt;br&gt;
into a cached preview:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FORUM_DB&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;prepare&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;POST_ROW_SQL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;first&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="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;row&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;canViewPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;viewer&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="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;// → generic head&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hidden, banned, blocked, deleted and missing all fall back to the same&lt;br&gt;
generic head with &lt;code&gt;noindex&lt;/code&gt;, and a test asserts the bytes are identical.&lt;/p&gt;

&lt;p&gt;The structured data for search engines goes in a&lt;br&gt;
&lt;code&gt;&amp;lt;script type="application/ld+json"&amp;gt;&lt;/code&gt; block. That's a trap, because a&lt;br&gt;
post body containing &lt;code&gt;&amp;lt;/script&amp;gt;&lt;/code&gt; would close the tag early. The fix is to&lt;br&gt;
escape after serializing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;jsonLdBlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&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;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&amp;lt;/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\\u003c"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&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;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\\u003e"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&amp;amp;/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\\u0026"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;u2028&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\\u2028"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;\&lt;span class="n"&gt;u2029&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"\\u2029"&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="sb"&gt;`&amp;lt;script type="application/ld+json"&amp;gt;${text}&amp;lt;/script&amp;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;It's still valid JSON (&lt;code&gt;\u003c&lt;/code&gt; is just &lt;code&gt;&amp;lt;&lt;/code&gt; to a JSON parser), but an&lt;br&gt;
HTML parser never sees a &lt;code&gt;&amp;lt;&lt;/code&gt;. There's a test that posts&lt;br&gt;
&lt;code&gt;&amp;lt;/script&amp;gt;&amp;lt;script&amp;gt;alert(1)&amp;lt;/script&amp;gt;&lt;/code&gt; and checks every block still parses&lt;br&gt;
and nothing gets injected.&lt;/p&gt;
&lt;h2&gt;
  
  
  Analytics with nothing to leak
&lt;/h2&gt;

&lt;p&gt;I wanted to know which pages people use, without tracking people. The&lt;br&gt;
answer was to store &lt;strong&gt;counts, not visits&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;page_view_counts&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;day&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;views&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;route&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;route&lt;/code&gt; is the router's page &lt;em&gt;name&lt;/em&gt;: &lt;code&gt;home&lt;/code&gt;, &lt;code&gt;post&lt;/code&gt;, &lt;code&gt;profile&lt;/code&gt;. It's&lt;br&gt;
never the path, so no post id, username or search query ever lands in a&lt;br&gt;
row. There's no IP, user agent, cookie or per-visit row. The decision to&lt;br&gt;
count is one small function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;pageViewPlan&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="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&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="nf"&gt;analyticsEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FORUM_DB&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="k"&gt;if&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="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="s2"&gt;"GET"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;isPrefetch&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="kc"&gt;null&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="nf"&gt;optedOut&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="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;looksLikeBot&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="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// DNT / Sec-GPC&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;response&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="mi"&gt;200&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="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="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&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;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;)&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="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"text/html"&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="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;route&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;routeForPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pathname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;search&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&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;route&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;route&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="s2"&gt;"not-found"&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;day&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;utcDay&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="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;referrer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;referrerHost&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="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;env&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;A few details I'm happy with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Do Not Track and Global Privacy Control are honored&lt;/strong&gt;, not just read.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Referrers are kept as a host only&lt;/strong&gt; (&lt;code&gt;reddit.com&lt;/code&gt;, never the full
URL), and only when it's someone else's site. The number of distinct
hosts per day is capped, so a flood of junk referrers can't bloat the
table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The write never slows the page.&lt;/strong&gt; It runs on &lt;code&gt;ctx.waitUntil&lt;/code&gt; after the
response is built, and any error becomes a log line, not a broken page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Old data deletes itself.&lt;/strong&gt; Rows older than 90 days are removed in small
bounded batches whenever staff open the Visits page.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The staff view is a plain table of daily totals, top page types and top&lt;br&gt;
referring sites. That's enough to see whether a Reddit post worked, and&lt;br&gt;
it holds nothing worth stealing.&lt;/p&gt;
&lt;h2&gt;
  
  
  D1 lessons (the expensive ones)
&lt;/h2&gt;

&lt;p&gt;D1 is SQLite, and mostly it's lovely. A few things cost me real time:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Foreign keys are enforced by the migration runner.&lt;/strong&gt; The classic
SQLite "rename the table, create a new one, copy, drop the old one"
rebuild can fail on the &lt;code&gt;DROP&lt;/code&gt;, or worse, silently cascade-delete rows
in tables that reference it. For schema changes I now add a new column,
backfill it, and switch reads over, instead of rebuilding tables.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Every migration is CREATE-only or column-additive, and released ones
are never edited.&lt;/strong&gt; Self-hosters can apply them in order from any older
version.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check migrations by name, not by count.&lt;/strong&gt; Before and after applying a
migration to production, I check that the expected migration &lt;em&gt;name&lt;/em&gt; is
in the ledger exactly once, and that a reference table's row count didn't
change. "Exit code 0" is not proof.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Bound parameters only, with real limits.&lt;/strong&gt; Every query is
&lt;code&gt;prepare(...).bind(...)&lt;/code&gt;, every &lt;code&gt;LIMIT&lt;/code&gt; is a bound number, and list
sizes stay under D1's bound-parameter cap.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Knowing what's actually deployed
&lt;/h2&gt;

&lt;p&gt;Every deploy stamps the commit hash into the HTML and into&lt;br&gt;
&lt;code&gt;/api/version&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wrangler deploy --keep-vars --var COMMIT_SHA:&lt;span class="nv"&gt;$(&lt;/span&gt;git rev-parse --short HEAD&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The browser compares its own build against &lt;code&gt;/api/version&lt;/code&gt; and shows a&lt;br&gt;
"new version available" banner when they differ, and the stamp doubles as&lt;br&gt;
a cache-buster for scripts. It also caught a nasty problem: a git push can&lt;br&gt;
trigger a hosted build that deploys &lt;em&gt;without&lt;/em&gt; the stamp. So the rule is&lt;br&gt;
that the stamped deploy always runs last, from a checkout at the pushed&lt;br&gt;
commit, and I probe &lt;code&gt;/api/version&lt;/code&gt; until it reports that commit.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing without a browser
&lt;/h2&gt;

&lt;p&gt;The suite (about 1,200 tests) runs with Node's built-in test runner. Routes&lt;br&gt;
run against an in-memory SQLite that behaves like D1, and front-end&lt;br&gt;
modules run against a small fake DOM. No headless browser, so the whole&lt;br&gt;
suite takes seconds and runs on every push. Fuzz tests (fast-check)&lt;br&gt;
throw thousands of malformed inputs at sign-in, search and posting.&lt;/p&gt;

&lt;p&gt;New features land red-first: the test commit goes in failing, then the&lt;br&gt;
code that makes it pass. When a change touches visibility, I break the&lt;br&gt;
fix on purpose to confirm the test notices. If removing the check doesn't&lt;br&gt;
fail a test, the test is wrong.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I'd tell you if you're building one
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pick the boring, inspectable stack.&lt;/strong&gt; One Worker, one database,
plain modules. Debugging is reading.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write the visibility rule once and sweep every route against it.&lt;/strong&gt;
Leaks hide in the routes you forgot existed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count, don't track.&lt;/strong&gt; You can learn what you need from aggregates.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Make "what's deployed?" a URL you can curl.&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The forum is at &lt;a href="https://wempos-forum.com" rel="noopener noreferrer"&gt;wempos-forum.com&lt;/a&gt;. If you play&lt;br&gt;
Polytoria or build games, come say hi. The code, self-hosting guide and&lt;br&gt;
security docs are on&lt;br&gt;
&lt;a href="https://github.com/Wempo-s-Silly-Projects/wempos-forum" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. I'd love&lt;br&gt;
feedback on any of the above.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>privacy</category>
    </item>
  </channel>
</rss>
