Sealing notes until their day: a no-login Advent calendar on Cloudflare Workers and D1

I built Advent With Friends, a small site where you make a 24-door Advent calendar, send one link to...

秀秀 (Open-Source)5 min read0 kali dilihat
Sealing notes until their day: a no-login Advent calendar on Cloudflare Workers and D1

I built Advent With Friends, a small site where you make a 24-door Advent calendar, send one link to your friends, and they leave short notes behind the doors. You open one door a day until December 24.

It has no accounts, and that one decision shaped most of the engineering. Here are the five problems that took the most thought, with the code that solved them. The stack is Next.js on Cloudflare Workers (through OpenNext) with a D1 database.

1. A note must not reach the browser before its day

The whole point of the calendar is that the owner cannot peek. Hiding the text with CSS, or sending it and trusting the client not to show it, would make every note one "view source" away.

So the server decides, for every request, whether a note's text is part of the response at all:

const today = localDate(unlockNow, calendar.time_zone);
// ...for each door:
const unlocked = doorDate(number) <= today;
if (own || (owner && unlocked)) note.body = row.body;

Only two readers ever get the text: the person who wrote the note, and the owner once the door's date has arrived. The same function feeds both the JSON API and the server-rendered page, so there is one rule to test. The test suite checks the rendered HTML as well as the API response for a locked note's text.

2. "Its day" depends on where the owner lives

A door should open at the owner's midnight. The browser reports its time zone once, when the calendar is created, and the server validates it with Intl and stores it. After that the client's clock is never consulted:

export function localDate(now: Date, timeZone: string) {
  const parts = new Intl.DateTimeFormat("en", {
    timeZone, calendar: "gregory", numberingSystem: "latn",
    year: "numeric", month: "2-digit", day: "2-digit",
  }).formatToParts(now);
  // ...assemble YYYY-MM-DD
}

Comparing YYYY-MM-DD strings keeps the unlock check free of date arithmetic. An invalid time zone falls back to UTC.

3. Two friends tap the same door

Each door holds one note, and a group chat can easily send two people to the same empty door within a second of each other. A "check, then insert" in application code would let both through.

The database enforces it with a partial unique index, which also lets a door be refilled after a note is deleted:

CREATE UNIQUE INDEX notes_one_active_per_door
  ON notes(calendar_code, door) WHERE deleted_at IS NULL;

The insert carries the per-guest limit (three doors per calendar) in the same statement, so the quota cannot be raced either:

INSERT INTO notes (...)
SELECT ... FROM calendars
WHERE code = ? AND deleted_at IS NULL
  AND (SELECT count(*) FROM notes
       WHERE calendar_code = ? AND author_hash = ? AND deleted_at IS NULL) < ?
ON CONFLICT (calendar_code, door) WHERE deleted_at IS NULL DO NOTHING
RETURNING id

If nothing comes back, a second query in the same batch tells the loser whether the door was taken or their quota ran out, and they get a clear message.

4. Owning something without an account

When you create a calendar the server generates a 256-bit random token, stores only its SHA-256 hash, and sets the token in an httpOnly cookie. That browser is now the owner.

Cookies get cleared and phones get replaced, so the owner also gets a private link with the token in the URL fragment: /c/CODE#k=…. Fragments are not sent to the server, so the token stays out of access logs. The page reads it, exchanges it for the cookie, and then has to get it out of the address bar, because the address bar is exactly what people copy when they share a page.

My first attempt used history.replaceState. A real-browser test caught that the fragment was still there afterwards: the App Router keeps its own copy of the URL and put it back on the next soft refresh. The fix was to leave the keyed URL with a full navigation:

await calendarApi(`${apiPath}/claim`, "POST", { token });
if (hasFragment) window.location.replace(withoutKey);

That bug would have had owners pasting their admin link into group chats. Unit tests did not see it; driving Chrome through the actual flow did.

5. Rate limits and school Wi-Fi

Abuse limits are per IP per hour, stored as a salted hash. My first numbers were 10 calendars and 20 notes per hour. Then I pictured a class of thirty students on one school network, all behind the same address, trying this at lunch. The limits are now 60 and 300. If your product spreads through group chats, the people who find it together are often on the same network.

What I would do the same way again

  • Put the access rule in one server function and test its output, including the rendered HTML.
  • Let the database enforce "only one" rules with a unique index.
  • Test the flows in a real browser before shipping. Two of the four bugs I fixed before launch only appeared there.

The site is free and has no sign-up: tryadventwithfriends.com. If you are making a calendar late, doors whose date has already passed open as soon as a friend fills them.


Sumber artikel open-source oleh 秀秀 di DEV Community — Baca artikel asli. Topik: #webdev #nextjs #cloudflare #showdev

Sukai & bagikan artikel ini

Komentar