Repository navigation
fix(core): keep the build out of Last-Modified when no cache hint sets one - #3528
Conversation
…s one The build dimension from emdash-cms#2374 was folded into the route cache validator of every cached on-demand page, including pages whose `Astro.cache.set()` hints carry no `lastModified`: pages that pass no hint, and pages that pass only tag hints such as site settings, menus, widget areas, taxonomy terms or an empty collection query. For those the build time became the only `Last-Modified`, and it does not move when content is published: a returning visitor's conditional request is answered with 304 until the next deploy. Before emdash-cms#2374 such pages had no validator, so an expired browser copy was refetched. The middleware now folds the build in after the route has run, and only when the route set a `lastModified`. Pages with one keep revalidating against both content and build; pages without one go back to having no validator. `Astro.cache.set(false)` clears `lastModified`, so an opted-out route is never re-enabled by the late set(). The check reads Astro's per-request cache options in memory and adds no database round trip. Closes emdash-cms#2883
🦋 Changeset detectedLatest commit: a6c2831 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
@emdash-cms/admin
@emdash-cms/auth
@emdash-cms/auth-atproto
@emdash-cms/blocks
@emdash-cms/cloudflare
@emdash-cms/contentful-to-portable-text
emdash
create-emdash
@emdash-cms/gutenberg-to-portable-text
@emdash-cms/plugin-cli
@emdash-cms/plugin-test
@emdash-cms/plugin-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@emdash-cms/registry-loader
@emdash-cms/registry-moderation
@emdash-cms/registry-verification
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-atproto
@emdash-cms/plugin-audit-log
@emdash-cms/plugin-color
@emdash-cms/plugin-embeds
@emdash-cms/plugin-field-kit
@emdash-cms/plugin-forms
@emdash-cms/plugin-webhook-notifier
commit: |
There was a problem hiding this comment.
This PR takes the right approach to the stale Last-Modified problem. Moving applyBuildValidator to run after next() and guarding it on context.cache.options.lastModified correctly keeps the build dimension for pages that have a content validator, while pages whose hints carry no movable lastModified (no hint, tag-only hints, empty query results) no longer get an immobile build-time validator that would 304 forever after a publish. It matches the stated goal, adds no queries on logged-out routes, and the changeset is user-facing and accurate.
I read the diff, the full middleware, the updated unit test, the changeset, and checked against AGENTS.md conventions (localization, SQL safety, API shape, authorization, locale filtering, index discipline, logged-out query counts, changeset discipline, and comment discipline). No logic bugs, regressions, security issues, or broken contracts were found. The PR author’s reported test, typecheck, lint, and query-count results are consistent with the static review.
I have two minor suggestions for clarity that do not block merge: one test name contradicts its setup, and one comment in applyBuildValidator is ambiguous about which set() is skipped when a route opts out.
The applyBuildValidator doc comment said an opted-out route "never reaches set()", which reads as if the route could not call Astro.cache.set(). The route does call set(false); what the lastModified guard skips is the build set() after next(), which would re-enable caching. The no-hint test's title now names the route rule that its maxAge stands for. Astro applies route rules when it creates the request's cache, before the route runs, and the page itself passes no hint.
What does this PR do?
Fixes returning visitors seeing a stale page after a content publish on cached pages whose
Astro.cache.set()hints carry nolastModified, such as pages with no hint or only settings and menu hints. #2374 made the build time their onlyLast-Modified, which a publish does not move, so conditional requests got 304 until the next deploy.The middleware now adds the build after the route has run, and only if the route set a
lastModified. Pages without one send noLast-Modifiedagain, so an expired browser copy is refetched. Pages with one still revalidate against content and build,Astro.cache.set(false)still opts out, and no query is added.Unpublishing from a hinted listing still does not advance its
lastModified, as before #2374.Closes #2883
Type of change
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change)pnpm formathas been runmessages.pochanges except in translation PRs — a workflow extracts catalogs on merge tomain. (n/a: no admin UI change)AI-generated code disclosure
Screenshots / test output
Not applicable (no UI change).
packages/core/tests/unit/astro/middleware-cache-validator.test.tsadds two cases, a cached page with only a route rule and one with only tag hints. Astro applies route rules before the route runs, so themaxAgeboth cases set first stands for a route rule, not a hint from the page. Onmainboth fail withexpected 2026-08-07T22:26:49.000Z to be undefined, the build time.pnpm build,pnpm typecheck,pnpm lint,pnpm format;lint:jsonreports0diagnosticsvitest run tests/unit/astro tests/unit/cache-hints.test.tsinpackages/core: 472 passedpnpm query-counts, sqlite and d1: both snapshots matchmemoryCache()and amaxAgeroute rule:/search(no hint) sent the build time asLast-Modifiedonmainand none with the fix;/(hinted) sent it in both