Automation

How to Avoid Opening a Public Write Endpoint on a Server at Home

An arrow runs Reader → Edge+DB; on the MacBook side only the pull arrow is live and the inbound arrow is crossed out

I added a "Was this helpful?" button at the bottom of posts. When a reader clicks it, a counter goes up.

The feature itself is simple, but where to write it was the problem. This blog's admin server is a MacBook sitting at home.

Two reasons not to write directly to the MacBook

The simplest setup would have the button hit the admin server directly. Two things got in the way.

One. If the MacBook is off, the vote disappears. It's a laptop — closed more often than it's open. Any vote cast while it's off has nowhere to land, so it's just lost.

Two. It means opening a public write endpoint on a machine sitting at home. That's an address anyone can hit. A read endpoint would be one thing, but this is a write. The admin dashboard lives on that same machine, and I didn't like the idea of cutting a door next to it that anyone could write through.

I flipped the direction

I moved the receiving end to the edge.

sequenceDiagram
  participant Reader
  participant Edge as Edge Function + DB
  participant MacBook as MacBook Admin
  Reader->>Edge: Vote
  Edge-->>Reader: Current tally
  Note over MacBook: Doesn't matter if it's off
  MacBook->>Edge: Once a day, give me the tally
  Edge-->>MacBook: Per-post totals

The edge records the vote, and the MacBook pulls it once a day. Every call goes outbound from the MacBook. There's no inbound door.

Votes cast while the MacBook was off still come through intact on the next run. They're sitting in the edge DB the whole time, so nothing is lost.

How duplicates are prevented

Recording "already clicked" in the browser only blocks that browser. Opening an incognito window gets around it instantly.

It needs to be blocked on the server side too, which means something to distinguish who clicked. There's no login, so the only thing left is the request IP. But storing the raw IP means accumulating personal data.

So it gets stored as a hash instead.

const day = new Date().toISOString().slice(0, 10)
const digest = await crypto.subtle.digest('SHA-256',
  new TextEncoder().encode(`${ip}|${slug}|${day}`))

Three things go into the mix.

Ingredient Why it's included
Request IP Distinguishes people
Post slug So the same person can still vote on other posts
Date After a day passes, even the same person gets a different value

Mixing in the date is the key part. This value only means anything for that one day and becomes useless the next. The raw IP itself is never stored anywhere.

There's no reason to keep values that have already gone stale, so old ones get cleaned up every time a new vote comes in.

DELETE FROM voters WHERE at < unixepoch() - 172800 -- 48 hours, padded beyond a single day to allow for date-boundary slop

Rather than standing up a separate scheduler, I just tacked one line onto the existing write path.

The pull side is locked with a key

The endpoint that returns the full tally is for admin use only. It must never be public.

GET /api/feedback?slug=…        ← anyone, just that post's count
GET /api/feedback?all=1&key=…   ← only with the right key, everything

The key is set to the same value on both the edge side and the admin side. I got tripped up here once. Registering a secret on the edge doesn't attach to a deployment that's already running. It only takes effect after you register it and redeploy once more. I kept getting rejected even with the correct key and spent a while confused before figuring that out.

The surprising part: the site doesn't rebuild when the numbers change

It's a static site. Any content change means rebuilding and redeploying. So does that mean the site has to be rebuilt every time someone clicks?

No. The vote count was never added to the list of fields that trigger a rebuild. Instead, the post page asks for the number directly when it loads.

fetch(`/api/feedback?slug=${postSlug}`)
  .then(counts).then((c) => c && show(c))

The number baked in at build time is just a starting value; once the page opens, it gets overwritten with the current one. Readers always see the latest count.

So the MacBook's daily pull actually has nothing to do with the public page. It's there for the admin dashboard's stats and the weekly report.

In the end, this is how you handle small, frequently-changing numbers on a static site: keep them out of the build, and let the page ask for them itself.