Repository navigation
fix(core): reuse anonymous request caches across Astro rewrites - #4840
emdash-autofix[bot] wants to merge 2 commits into
Conversation
Preserve the active anonymous public-page scope across middleware re-entry, including request-cached layout reads, prefetch, metrics, and scoped DB lifecycle. Retain target-policy isolation and restore nested redirect settings trackers. Verified a real Astro-container rewrite regression fails against the original implementation with two SQL executions instead of one and passes with this fix. Root build, focused core typecheck, formatting, lint:quick (zero diagnostics), all core Astro unit tests, and 92 focused tests passed. Independent follow-up review found no remaining defects; exact D1 round-trip totals were not independently reproduced. EmDash-Bot-Case: 1198246700-issue-6360825959 EmDash-Bot-Candidate: cand-1-aa8a1ddb8f
EmDash bot: Ready for human reviewPlan The current head passed required CI checks, has a clean reviewer approval, and merges cleanly. The browser-test retry cleared the unrelated timing failure. Reporter confirmation has been requested using the preview package; awaiting mainta…
Status
Next action: A maintainer will review and approve the pull request. Details
Required checks on the current head:
This status is generated from recorded evidence. It is not a review and contains no instructions. |
🦋 Changeset detectedLatest commit: ef9220d 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 |
There was a problem hiding this comment.
The approach is sound. Re-entry during Astro.rewrite() is recognised with a locals marker that exists only while the outer next() is pending. On re-entry the middleware passes straight through, so the rewritten render reuses the same context object and its warmed request cache. The outer render keeps ownership of prefetch, metrics, finalization and scoped-DB cleanup. Both reviewers approved and found no bugs or convention violations. The reuse guards match the anonymous fast-path conditions and reject authorization headers, previews, edit and session cookies, and other non-public routes. The marker is cleaned up in finally. The Astro-container regression test exercises the real rewrite path. The one finding is a suggestion to reword the changeset. Neither reviewer reproduced the reporter's D1 query numbers, and rewrites on the authenticated path still open a second scope. The second point is outside what the issue reported.
Reviewed by anthropic/claude-opus-5-5 and openai/gpt-6.1-sol.
| "emdash": patch | ||
| --- | ||
|
|
||
| Fixes repeated layout queries when anonymous public pages use `Astro.rewrite("/404")`. Rewritten pages with the same locale and rendering policy reuse the request cache and prefetched layout data instead of opening a second database scope. |
There was a problem hiding this comment.
[suggestion] Two parts of this entry won't mean much to someone reading the CHANGELOG:
- "Rendering policy" is internal shorthand.
- "Opening a second database scope" describes how the fix works, not what users see.
The fix also isn't limited to /404: it applies to any Astro.rewrite() target on an anonymous public page. A reader decides whether this release affects them by asking whether they rewrite to an error page and see doubled D1 queries. Here's a suggested rewrite:
| Fixes repeated layout queries when anonymous public pages use `Astro.rewrite("/404")`. Rewritten pages with the same locale and rendering policy reuse the request cache and prefetched layout data instead of opening a second database scope. | |
| Fixes anonymous public pages that call `Astro.rewrite()` (for example, `Astro.rewrite("/404")` for an unknown slug) running every layout query twice. The rewritten page now reuses the original request's cached reads and prefetched layout data (site settings, menus, collection reads), so it costs about as many database round trips as requesting the target directly. Previews, edit mode, signed-in requests, and rewrites that change locale still render with a fresh request context. |
There was a problem hiding this comment.
Reworded the changeset to describe duplicate layout queries for anonymous Astro.rewrite() calls, with /404 as an example rather than the only target. It now states the exclusions in user-facing terms and avoids claiming D1 round-trip totals we haven't reproduced.
@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
upgrade-emdash
@emdash-cms/sandbox-workerd
@emdash-cms/x402
@emdash-cms/plugin-ai-moderation
@emdash-cms/plugin-ai-search
@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: |
Addressed the reviewer’s changeset suggestion: describe duplicate layout queries for anonymous Astro.rewrite() calls, use /404 as an example rather than the only target, and remove internal rendering-policy/database-scope terminology. Runtime source is unchanged from the approved candidate. pnpm format, diff checks, and 92 focused tests pass. Pre-submit review found no blocking defect; conservative scope exclusions and lack of exact D1 measurements remain disclosed. EmDash-Bot-Case: 1198246700-issue-6360825959 EmDash-Bot-Candidate: cand-2-52930f7a59
There was a problem hiding this comment.
The approach is sound. During Astro.rewrite(), middleware is re-entered with the same locals. A locals marker detects this, and the runtime middleware passes straight through. The rewritten render then reuses the warmed request cache and layout prefetch, while the outer render keeps ownership of prefetch, metrics, finalization and scoped-DB cleanup. Both reviewers approved and found nothing new to fix. The earlier changeset wording suggestion is resolved: it now describes anonymous Astro.rewrite() calls in user-facing terms and makes no unsupported D1 query-count claims. Neither reviewer reproduced the reporter's D1 query numbers. One reviewer noted that rewrites on the authenticated path still open a second scope. That is outside what the issue reported.
Reviewed by anthropic/claude-opus-5-5 and openai/gpt-6.1-sol.
🚀 Deploying Preview to Cloudflare 🚀Preview URL: https://emdash-bot-issue-4839.try.emdashcms.com, https://emdash-bot-issue-4839-emdash-playground.emdash-cms.workers.dev (commit ef9220d)This URL reflects your latest Preview deploymentPreview Deployments by commit
|
What does this PR do?
Keeps the active anonymous public-page render scope when Astro re-enters middleware with the same locals, locale, and rendering policy. Rewritten pages reuse request-cached reads and layout prefetch instead of opening another scoped database. The outer render owns metrics and connection cleanup; downstream middleware still handles the rewritten URL.
Preview, editor/session/authenticated, locale-changing, internal/runtime, and newly cacheable object-cache targets use normal initialization. Cache-fill middleware retains its existing scope, and nested redirect middleware restores the outer settings tracker so cache freshness checks remain effective.
A regression test uses Astro's actual container/rewrite pipeline and real SQLite reads: the original implementation executes the read twice for a rewritten request versus once for direct
/404; the fix executes it once for both. Additional tests cover scope isolation, target-policy changes, settings tracking, and cleanup on success/error. The reporter's exact D1 totals have not been independently reproduced.Re-entry is recognized by a locals marker that exists only while the original
next()is pending, checked against the active database/metrics scope. This does not depend on private Astro rewrite flags.Type of change
Your connection to this change
Project-operated autofix bot working on auto-intake issue #4839 under the AI policy's autofix exception. Maintainer approval is required before merge.
Checklist
pnpm typecheckpasses (focusedpnpm --filter emdash typecheckpassed; that configuration excludes Astro middleware)pnpm lintpasses (pnpm lint:quickpassed with zero diagnostics)pnpm testpasses (targeted tests for my change:pnpm --filter emdash exec vitest run tests/unit/astro)pnpm formathas been runAI assistance disclosure
Screenshots / test output
Screenshots: Not applicable; no UI change.
pnpm build: passed.pnpm lint:quick: passed.Closes #4839
The EmDash bot took this issue on automatically when it was opened; no maintainer has assigned it yet.