Repository navigation
feat: set a cache policy - #206
Conversation
Set Cache-Control headers in next.config.js, so that the Docker image and Vercel send the same ones. Pages are revalidated on every request instead of Next.js's s-maxage of a year, which would let a shared cache serve pages that point at /_next/static/ files of an older image. Files of public/ are cached for a day, robots.txt and the sitemaps for an hour, and sw.js not at all. Hashed /_next/static/ files keep Next.js's immutable header. A test matches paths against the rules the way Next.js does and checks that every file of public/ gets a rule for files. The README documents the policy. Closes #204 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Reviewer's GuideConfigurationDefine cache-control rules in Next.js for pages, public assets, crawler files, and the service worker, while leaving Next.js-managed asset caching intact.
DocumentationDocument the cache policies, their deployment implications, and when to rename public assets to avoid stale cached files.
TestsTest the effective cache rule for representative paths and verify that every file in public/ receives its intended policy.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## dev #206 +/- ##
=======================================
Coverage 49.86% 49.86%
=======================================
Files 44 44
Lines 1105 1105
Branches 292 292
=======================================
Hits 551 551
Misses 479 479
Partials 75 75 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The check for public/ accepted any rule for files, so robots.txt, the sitemaps or sw.js could silently fall back to the rule for other files. Expect the specific rule for each of them instead. Also say why the test imports a Next.js internal, that the image TTL is a Next.js default, that the page rule covers any path without an extension, and link the sw.js row to the note on the old PWA. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stale-while-revalidate lets a cache serve an old file of public/ once more within a week after its day, and a direct request for /404 gets the page rule. Say so instead of promising a day and no cached 404s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@sourcery-ai review |
Linked issuesChecked at commit 0300bfc #204 feat: configure a cache policy · 1 of 1 met
|
# Conflicts: # next.config.js
Summary
Sets a cache policy for the site (part of #107), in the
headersofnext.config.js, so that the Docker image and Vercel send the sameCache-Controlheaders:Cache-Control/_next/static/(hashed JS, CSS, fonts)public, max-age=31536000, immutable(Next.js default, can't be overridden)/_next/imagepublic, max-age=0, must-revalidatepublic/public, max-age=86400, stale-while-revalidate=604800robots.txt,sitemap*.xmlpublic, max-age=3600sw.jsno-cache, no-store, must-revalidatePages are revalidated on every request because Next.js sends
s-maxage=31536000for prerendered pages. A shared cache could then serve a page for a year after a deploy, for example a reverse proxy in front of the Docker host, which Watchtower redeploys without purging anything. That page would point at/_next/static/files the new image no longer has. Withmax-age=0, must-revalidate, caches revalidate by ETag (a304), so deploys show at once.public/sw.jsis not changed: offline support (#205) is left to the maintainers. It only gets a header, so that browsers always fetch the clean-up worker fresh.next.config.js: the rules, with comments.src/test/cache-policy.test.ts: compiles the rules with Next.js's ownbuildCustomRoute, the functionnext builduses. It checks which header applies to pages, files, crawler files,sw.jsand/_next/, and that every file inpublic/gets the policy meant for it.README.md: new section "Caching" under "Deployment".Verification
sw.jsrule) makes 2 tests fail.npx prettier --check "src/**/*.{ts,tsx,json,css}": passes.README.mdhas a formatting difference in its link references that was there before; the new section is formatted.npx tsc --noEmit: passes.npm run lint: passes.npm run test:coverage: 10 files, 106 tests passed.npm run build: passes (all routes static, sitemap generated).node .next/standalone/server.jswithpublic/and.next/staticcopied), checked withcurl -D -. The adversarial reviewer repeated this independently and got the same results./,/products:public, max-age=0, must-revalidate.If-None-Matchwith the ETag gives304./productswithRSC: 1,/nodes/(redirect): same page header./nonexistent,/missing.png(404):private, no-cache, no-store, max-age=0, must-revalidate(unchanged, set by Next.js)./elixir.png,/favicon.ico,/flags/de.svg,/ELIXIR%20On-Cloud%20Logo%20Pack/On-Cloud_MONO.svg:public, max-age=86400, stale-while-revalidate=604800./robots.txt,/sitemap.xml,/sitemap-0.xml:public, max-age=3600./sw.js:no-cache, no-store, must-revalidate./_next/image?url=%2Felixir.png&w=128&q=75:public, max-age=86400, must-revalidate./_next/static/chunks/*.js:public, max-age=31536000, immutable.s-maxage=31536000for pages andpublic, max-age=0for all files ofpublic/.302). Vercel applies theheadersofnext.config.jsas documented.AuditandCI passed. npm has dropped GHSA-fc5q-39xc-vjpf, which GitHub withdrew on 2026-11-18; fix: make next-sitemap a dev dependency #208 (for fix: keep next-sitemap out of production audit #207) also keeps build-only tools out of the audit from now on.Review
adversarial-reviewerandquality-reviewerran onorigin/dev...HEAD. Neither found anything high. The fixes are ind89d25eand2168a32:public/check accepted any rule for files. It now expects the specific rule forrobots.txt, the sitemaps andsw.js.next builduses.stale-while-revalidatecan serve an oldpublic/file once more within a week. It now says that 404s for unknown paths are not cached, because a directGET /404gets the page rule. The image TTL is marked as the Next.js default, the page rule's comment says it covers any path without an extension, and thesw.jsrow links to the PWA note.These fixes only changed tests, comments and docs, not logic, so no second round was needed.
Declined review findings
curl). Declined for this PR: the real server was checked by hand and by the adversarial reviewer with the same results, and a server-level test job would be a CI change of its own. The unit test checks the rule order Next.js documents.GET /404getspublic, max-age=0, must-revalidate. Caches must revalidate it on every request, so it's harmless; the README now says so rather than adding a rule.Open questions
None.
Closes #204
🤖 Generated with Claude Code
Summary by Sourcery
Set explicit cache policies for site responses so deployments revalidate pages promptly while caching static assets appropriately.
New Features:
Enhancements:
Documentation:
Tests: