Repository navigation
Refresh public pages when coordinators save content (W6-10) - #153
Merged
Merged
Conversation
Add POST /api/revalidate in Next.js, which checks a shared secret in
constant time, allows only the 7 public cache tags, and expires each tag
with revalidateTag(tag, { expire: 0 }).
Every admin write now schedules request_revalidation as a BackgroundTask
after its commit. It uses stdlib urllib with a 3s timeout, logs only the
tags and the status on failure, and does nothing when REVALIDATE_SECRET
is unset or empty. Resource writes also refresh fit-guide, since the
public fit guide shows each linked resource. Reorder maps each collection
to its tag, and report options get none.
A guard test fails when a new admin write isn't in the tag table.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Public pages are cached for hours (
cacheLife("hours")infrontend/src/lib/public-data.ts), so a coordinator's edit wouldn't show up until the cache expires. After each admin save, the backend now tells Next.js which cache tags to drop (TDD 4.6.8).Changes:
POST /api/revalidate(frontend/src/app/api/revalidate/route.ts):x-revalidate-secretheader againstREVALIDATE_SECRETin constant time. Both values are SHA-256 hashed first, sotimingSafeEqualalways compares equal lengths and never throws.tagslist, or any tag not on the allow-list.revalidateTag(tag, { expire: 0 })for each tag and returns 204.frontend/src/lib/cache-tags.tsholds the 7 allowed tags.getPublicDatanow takes aCacheTag, so the allow-list can't drift from the tags pages actually use.backend/app/services/revalidate.py:request_revalidation(tags)sends the POST with a 3s timeout. It never raises. On failure it logs only the tags and the status code (or the error type for network errors), never the secret.schedule_revalidation(background, *tags)is the one helper routes call. It adds the request as a FastAPIBackgroundTask, so it runs after the response is sent, which is after the commit. A failed or slow call can't fail or slow the save.REVALIDATE_SECRETis unset or an empty string, matching the frontend.backend/app/config.pyaddsrevalidate_url(dev defaulthttp://localhost:3000/api/revalidate) andrevalidate_secret(defaultNone).Every admin write schedules its tags right after
db.commit():hourssettingscontentfaqsfit-guideresources,fit-guideresourcesstatisticsfit-guide: the public/api/fit-guideincludes each linked resource's name, URL and phone, and hides unpublished resources. Deleting a resource also clears the link (ON DELETE SET NULL). Refreshing onlyresourceswould leave stale or broken resource links on the FAQ page.resourcesbecause the resources page fetches/api/resource-tagsunder that tag.settingson every save, even for non-public keys. It's simpler, the extra refresh costs almost nothing, and it can't miss a public page that reads a setting indirectly.Reorder: the
Collectiondataclass gets arevalidatefield:faqs→faqs,statistics→statistics,fit-guide→fit-guide,resource-tags→resourcesresources→resourcesonly, since the fit guide has its own orderreport-options→ noneTests:
tests/test_revalidate.py:/api/adminroute from the OpenAPI schema and fails unless each one is in the table or in an explicit not-public set (accounts). A future admin write can't skip revalidation without someone deciding its tags. I checked that the table catches mistakes by droppingfit-guidefrom the resource update: the test failed.tags=[...] status=Norerror=Type, never the secret, and don't raise.POST /api/admin/faqsstill returns 201 and the row is committed.conftest.pyswaps the sender for a recorder in every test. The dev container now setsREVALIDATE_SECRET, so tests would otherwise send real HTTP calls.route.test.ts: a missing secret, a wrong secret (same length and different length) and an unset or empty env var all give 401. An unknown tag, an empty list, a bad shape or bad JSON give 400. A valid call gives 204 and callsrevalidateTag(tag, { expire: 0 })once per tag.Env (Replace copied env files with code defaults and devcontainer env #38 convention):
REVALIDATE_SECRETgets a dev-only value in.devcontainer/docker-compose.yml. The backend and Next.js run in the same container, so one entry covers both..env.examplefiles list it, and the backend one also listsREVALIDATE_URL.Decisions:
revalidateTag(tag, { expire: 0 }), not"max". In Next 16, callingrevalidateTagwith one argument is deprecated."max"serves stale content while it revalidates, so the first visit after an edit would still show the old page, which is the problem this issue fixes. The bundled Next docs recommend{ expire: 0 }for webhook callers, which can't useupdateTag.urllib, nothttpx2.httpx2is only a dev dependency, and this is one small POST. Starlette runs sync background tasks in a thread pool, so the blocking call doesn't block the event loop.REVALIDATE_SECRETis optional on both sides (unset means off on the backend and 401 on the frontend), and the tests set their own values.Proof
/api/revalidateis served by Next.js, not rewritten to the backend: I ranmake dev-frontendwith only the dev secret set and no backend running:If the
/api/:path*rewrite had caught/api/revalidate, all four calls would have failed like the control. Next serves its own routes before applying rewrites, sonext.config.tsneeded no change. Next's log showedPOST /api/revalidate 204.I also called the backend's real
request_revalidation(["faqs", "fit-guide"])against that server: it got a 204. With a wrong secret, the backend logged onlyRevalidation failed: tags=['faqs'] status=401. I didn't run a full admin save from the portal, because my local dev database has no coordinator account.make checkpasses (lint, mypy, tsc, 679 backend and 279 frontend tests, build).Note for reviewers: rebuild the dev container (Dev Containers: Rebuild Container) to pick up
REVALIDATE_SECRET. Until then it's unset, so the backend skips revalidation and the route returns 401, and edits only appear after the cache expires. For production, set the sameREVALIDATE_SECRETon both App Platform components (TDD 4.11), and setREVALIDATE_URLon the backend to the public site's/api/revalidate.Closes #144