diff --git a/CHANGELOG.md b/CHANGELOG.md index 134dfe5..7721937 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 | diff --git a/CLAUDE.md b/CLAUDE.md index fc10142..945b89e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -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) ``` diff --git a/functions/AGENTS.md b/functions/AGENTS.md index 294afac..88a55fb 100644 --- a/functions/AGENTS.md +++ b/functions/AGENTS.md @@ -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//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=.` (`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//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=.` (`HttpOnly; Secure; SameSite=Lax`, 24 h), signed with `TURNSTILE_SECRET_KEY`. Exports the cookie helpers the middleware uses. | ## For AI Agents diff --git a/functions/_middleware.ts b/functions/_middleware.ts index 689c38c..cad1e8d 100644 --- a/functions/_middleware.ts +++ b/functions/_middleware.ts @@ -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) diff --git a/tests/functions/turnstile.test.ts b/tests/functions/turnstile.test.ts index bb329db..033a608 100644 --- a/tests/functions/turnstile.test.ts +++ b/tests/functions/turnstile.test.ts @@ -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("/"),