Skip to content

fix(core): keep the build out of Last-Modified when no cache hint sets one - #3528

Merged
khoinguyenpham04 merged 2 commits into
emdash-cms:mainfrom
danielmlr:fix/stale-last-modified
Sep 30, 2026
Merged

khoinguyenpham04 merged 2 commits into
emdash-cms:mainfrom
danielmlr:fix/stale-last-modified

Conversation

@danielmlr

@danielmlr danielmlr commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

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 no lastModified, such as pages with no hint or only settings and menu hints. #2374 made the build time their only Last-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 no Last-Modified again, 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

  • Bug fix
  • Feature (requires maintainer-approved Discussion)
  • Refactor (no behavior change)
  • Translation
  • Documentation
  • Performance improvement
  • Tests
  • Chore (dependencies, CI, tooling)

Checklist

  • I have read CONTRIBUTING.md
  • pnpm typecheck passes
  • pnpm lint passes
  • pnpm test passes (or targeted tests for my change)
  • pnpm format has been run
  • I have added/updated tests for my changes (if applicable)
  • User-visible strings in the admin UI are wrapped for translation (if applicable). Do not include messages.po changes except in translation PRs — a workflow extracts catalogs on merge to main. (n/a: no admin UI change)
  • I have added and reviewed the user-facing changeset (if this PR changes a published package)
  • New features link to an approved Discussion: https://github.com/emdash-cms/emdash/discussions/... (n/a: bug fix)
  • I have included screenshots below if this PR changes the UI (n/a: no UI change)

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 5.5

Screenshots / test output

Not applicable (no UI change).

packages/core/tests/unit/astro/middleware-cache-validator.test.ts adds 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 the maxAge both cases set first stands for a route rule, not a hint from the page. On main both fail with expected 2026-08-07T22:26:49.000Z to be undefined, the build time.

  • pnpm build, pnpm typecheck, pnpm lint, pnpm format; lint:json reports 0 diagnostics
  • vitest run tests/unit/astro tests/unit/cache-hints.test.ts in packages/core: 472 passed
  • pnpm query-counts, sqlite and d1: both snapshots match
  • Perf fixture with memoryCache() and a maxAge route rule: /search (no hint) sent the build time as Last-Modified on main and none with the fix; / (hinted) sent it in both

…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-bot

changeset-bot Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: a6c2831

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/plugin-test Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/admin Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
create-emdash Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
@emdash-cms/auth-atproto Patch
@emdash-cms/plugin-embeds Patch

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

@pkg-pr-new

pkg-pr-new Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

npm i https://pkg.pr.new/@emdash-cms/admin@3528

@emdash-cms/auth

npm i https://pkg.pr.new/@emdash-cms/auth@3528

@emdash-cms/auth-atproto

npm i https://pkg.pr.new/@emdash-cms/auth-atproto@3528

@emdash-cms/blocks

npm i https://pkg.pr.new/@emdash-cms/blocks@3528

@emdash-cms/cloudflare

npm i https://pkg.pr.new/@emdash-cms/cloudflare@3528

@emdash-cms/contentful-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/contentful-to-portable-text@3528

emdash

npm i https://pkg.pr.new/emdash@3528

create-emdash

npm i https://pkg.pr.new/create-emdash@3528

@emdash-cms/gutenberg-to-portable-text

npm i https://pkg.pr.new/@emdash-cms/gutenberg-to-portable-text@3528

@emdash-cms/plugin-cli

npm i https://pkg.pr.new/@emdash-cms/plugin-cli@3528

@emdash-cms/plugin-test

npm i https://pkg.pr.new/@emdash-cms/plugin-test@3528

@emdash-cms/plugin-types

npm i https://pkg.pr.new/@emdash-cms/plugin-types@3528

@emdash-cms/registry-client

npm i https://pkg.pr.new/@emdash-cms/registry-client@3528

@emdash-cms/registry-lexicons

npm i https://pkg.pr.new/@emdash-cms/registry-lexicons@3528

@emdash-cms/registry-loader

npm i https://pkg.pr.new/@emdash-cms/registry-loader@3528

@emdash-cms/registry-moderation

npm i https://pkg.pr.new/@emdash-cms/registry-moderation@3528

@emdash-cms/registry-verification

npm i https://pkg.pr.new/@emdash-cms/registry-verification@3528

@emdash-cms/sandbox-workerd

npm i https://pkg.pr.new/@emdash-cms/sandbox-workerd@3528

@emdash-cms/x402

npm i https://pkg.pr.new/@emdash-cms/x402@3528

@emdash-cms/plugin-ai-moderation

npm i https://pkg.pr.new/@emdash-cms/plugin-ai-moderation@3528

@emdash-cms/plugin-atproto

npm i https://pkg.pr.new/@emdash-cms/plugin-atproto@3528

@emdash-cms/plugin-audit-log

npm i https://pkg.pr.new/@emdash-cms/plugin-audit-log@3528

@emdash-cms/plugin-color

npm i https://pkg.pr.new/@emdash-cms/plugin-color@3528

@emdash-cms/plugin-embeds

npm i https://pkg.pr.new/@emdash-cms/plugin-embeds@3528

@emdash-cms/plugin-field-kit

npm i https://pkg.pr.new/@emdash-cms/plugin-field-kit@3528

@emdash-cms/plugin-forms

npm i https://pkg.pr.new/@emdash-cms/plugin-forms@3528

@emdash-cms/plugin-webhook-notifier

npm i https://pkg.pr.new/@emdash-cms/plugin-webhook-notifier@3528

commit: a6c2831

@danielmlr
danielmlr marked this pull request as ready for review September 27, 2026 22:20

@emdashbot emdashbot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread packages/core/src/astro/middleware.ts Resolved
@github-actions github-actions Bot added the review/awaiting-author Reviewed; waiting on the author to respond label Sep 27, 2026
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.

@khoinguyenpham04 khoinguyenpham04 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Sep 30, 2026
@khoinguyenpham04
khoinguyenpham04 merged commit 189c6b3 into emdash-cms:main Sep 30, 2026
51 checks passed
@emdashbot emdashbot Bot mentioned this pull request Sep 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

2 participants