Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
## 2026-11-12 21:56

| Check | Status | Details |
|-------|--------|---------|
| Lint | ✓ PASS | 0 errors |
| Build | ✓ PASS | 27 pages built, 5 pages indexed (Pagefind) |
| Unit Tests | ✓ PASS | 73 tests passed |
| E2E Tests | ✓ PASS | 96 tests passed |
| Lighthouse | ✓ PASS | 99.7% performance (avg) |

### Lighthouse Performance (local build)

| Page | Scores (3 runs) |
|------|-----------------|
| / (index) | 100%, 100%, 100% |
| /about | 99%, 99%, 99% |
| /posts | 99%, 100%, 100% |
| /archives (soft 404, not linked) | 100%, 100%, 100% |
| /404 | 100%, 100%, 100% |

**Diagnostics:** Production check after #13: `/posts/1/` answered 401 instead of 301, because Pages Functions run before `public/_redirects`. Live site not measured.

Changes: The Turnstile middleware answers `/posts/1` and `/posts/1/` with a 301 to `/posts/` before the gate, restoring the redirect for visitors without a session. 2 new unit tests.

## 2026-11-12 21:40

| Check | Status | Details |
Expand Down
2 changes: 1 addition & 1 deletion CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ The URL slug is the file name: `src/content/posts/foo.md` → `/posts/foo/`. Fil
bun run clean # Start fresh like CI (removes dist/ and public/pagefind/)
bun run lint # 1. Linting (0 errors)
bun run build # 2. Build (must run before unit tests!)
bun run test:run # 3. Unit tests (71+ tests, requires dist/)
bun run test:run # 3. Unit tests (73+ tests, requires dist/)
bun run test:e2e # 4. E2E tests (96+ tests)
bun run lighthouse # 5. Lighthouse CI (85%+ performance)
```
Expand Down
8 changes: 4 additions & 4 deletions functions/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ Cloudflare Pages Functions that gate the whole blog behind Cloudflare Turnstile,

## Key Files

| File | Description |
| ------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `_middleware.ts` | Runs on every request. Without a valid `blog_session` cookie, page loads get a self-contained Turnstile page (401, `noindex`) and other requests get a bare 401. Public without a cookie: `/api/*`, `/rss.xml`, both sitemaps, `/robots.txt`, favicons, `/og.png`, `/astropaper-og.jpg`, `/posts/<slug>/index.png` and the pre-v7 OG PNG. `PREVIEW=true` passes everything. A missing secret fails closed (500). |
| `api/verify-turnstile.ts` | `POST /api/verify-turnstile` with `{ token }`: verifies via Cloudflare siteverify and sets `blog_session=<expiry>.<HMAC-SHA256>` (`HttpOnly; Secure; SameSite=Lax`, 24 h), signed with `TURNSTILE_SECRET_KEY`. Exports the cookie helpers the middleware uses. |
| File | Description |
| ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `_middleware.ts` | Runs on every request. `/posts/1` and `/posts/1/` get a 301 to `/posts/` first, because Functions run before `public/_redirects`. Without a valid `blog_session` cookie, page loads get a self-contained Turnstile page (401, `noindex`) and other requests get a bare 401. Public without a cookie: `/api/*`, `/rss.xml`, both sitemaps, `/robots.txt`, favicons, `/og.png`, `/astropaper-og.jpg`, `/posts/<slug>/index.png` and the pre-v7 OG PNG. `PREVIEW=true` passes everything. A missing secret fails closed (500). |
| `api/verify-turnstile.ts` | `POST /api/verify-turnstile` with `{ token }`: verifies via Cloudflare siteverify and sets `blog_session=<expiry>.<HMAC-SHA256>` (`HttpOnly; Secure; SameSite=Lax`, 24 h), signed with `TURNSTILE_SECRET_KEY`. Exports the cookie helpers the middleware uses. |

## For AI Agents

Expand Down
6 changes: 6 additions & 0 deletions functions/_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export async function onRequest(context: {
const { request, env, next } = context;
const { pathname } = new URL(request.url);

// Functions run before public/_redirects, so the gate would otherwise answer /posts/1/ with 401.
if (pathname === "/posts/1" || pathname === "/posts/1/")
return new Response(null, {
status: 301,
headers: { Location: "/posts/" },
});
if (env.PREVIEW === "true") return next();
if (isPublicPath(pathname)) return next();
if (!env.TURNSTILE_SECRET_KEY)
Expand Down
9 changes: 9 additions & 0 deletions tests/functions/turnstile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ describe("onRequest", () => {
).toBe(200);
});

it.each(["/posts/1", "/posts/1/"])(
"redirects %s to /posts/ before the gate",
async path => {
const res = await onRequest({ request: req(path), env, next: ok });
expect(res.status).toBe(301);
expect(res.headers.get("Location")).toBe("/posts/");
}
);

it("fails closed when the secret is missing", async () => {
const res = await onRequest({
request: req("/"),
Expand Down
Loading