Repository navigation
fix(templates): answer content misses with a real 404 instead of a redirect - #3650
Conversation
🦋 Changeset detectedLatest commit: 18046eb 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 |
Scope checkThis PR touches 98 files. PRs with a broad scope are harder to review. Please confirm the scope hasn't drifted beyond the intended change. If this scope is intentional, no action needed. A maintainer will review it. If not, please consider splitting this into smaller PRs. See CONTRIBUTING.md for contribution guidelines. |
@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 is a focused, well-scoped bug fix. The approach is sound: replacing Astro.redirect("/404") with Astro.rewrite("/404") across templates, demos, docs, and fixtures means the original URL returns a real 404 status with the site 404 page rendered inline, which is what Astro recommends for this case and what search engines expect. The two core middleware fixes that Astro.rewrite surfaced are correctly targeted:
- Toolbar/bootstrap deduplication —
injectBeforeBodyEndnow checks for an existing marker before injecting HTML again. The markers (id="emdash-toolbar"and<!-- EmDash Toolbar Bootstrap -->) match the strings emitted byrenderToolbar()andrenderToolbarBootstrap(), so nested rewrite passes do not duplicate injected markup. - 404 route-cache opt-out — after
next()returns a 404, the redirect middleware callscontext.cache?.set(false), preventing a cached 404 from outliving the missing entry being published later.
I checked the core middleware, the toolbar/bootstrap renderers, the redirect middleware tests, the toolbar-mode tests, the changeset, and the documentation diffs. The tests are real regression tests that fail without the fixes. The changeset is present and covers the emdash package behavior. Documentation prose and examples were updated consistently. No remaining Astro.redirect("/404") calls exist outside of apps/plugins-site (deliberately handled in #3643) and the backward-compatibility test comment.
No logged-out hot-path queries are added. No AGENTS.md conventions are violated. The known narrow case in toolbar: "client" + ?_edit on a missing URL is acknowledged and acceptable as a follow-up edge.
Clean change.
What does this PR do?
Every template, demo and docs example answered a missing entry with
Astro.redirect("/404"). That is a soft 404: the missing URL returns302, and only/404returns404, so search engines see a redirect to one shared error URL instead of a missing page. This switches them toAstro.rewrite("/404"), which is what Astro recommends for this case: the missing URL itself answers404with the site's 404 page, in one request instead of two.Call sites changed (same one-line replacement everywhere):
templates/*(starter, blog, marketing, portfolio, Node and Cloudflare variants) and thebuilding-emdash-siteskill, including its copies undertemplates/*/.agents/skills(edited in place; re-runningsync-template-skills.shwould also pull unrelated skill changes into the templates)demos/*,infra/*demos,fixtures/perf-site, the e2e and integration fixturesreference/api.mdxthat said "redirects"), theregistry-loaderREADME, and thegetEmDashEntryJSDoc exampleapps/plugins-siteis handled separately in #3643.Astro.rewrite()runs the middleware again for/404inside the original request, which surfaced two things in core, both fixed here:#emdash-toolbarelements); intoolbar: "client"mode every visitor got the bootstrap script twice. Injection now skips HTML that already contains the toolbar or bootstrap, while still marking toolbar responsesprivate, no-storeand opting them out of the route cache.The redirect middleware keeps supporting the old
Astro.redirect("/404")flow for existing sites (still logged under the missed path); only its comment changed. The rewrite flow is logged once, under the missed path, by the existing 404 branch.Known narrow case, left as is: in
toolbar: "client"mode, an editor opening a missing URL with?_editgets the bootstrap from the inner pass (the rewrite drops the query) plus the full toolbar from the outer pass.Closes #
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 items: no admin UI strings; bug fix, no Discussion; no visible UI change (the 404 pages render as before, only the status and URL change). Templates, demos and docs need no changeset; the
emdashpatch changeset covers the two middleware fixes.AI-generated code disclosure
Screenshots / test output
Not applicable (no UI change). Verified on
demos/simple(astro dev):/posts/no-such-post,/pages/…,/category/…,/tag/…(anonymous)302→/404404, site 404 page at the original URL/404entry/posts/no-such-postas editor404, one toolbar (was two with the rewrite before the core fix),private, no-store200200Tests: new cases in
request-context-toolbar-mode.test.ts(toolbar and bootstrap injected once through a nested rewrite pass) andmiddleware-redirect.test.ts(404 opts out of the route cache, 200 doesn't); each fails with its fix removed. Fullpackages/coresuite: 8969 passed.pnpm typecheck,typecheck:demos,typecheck:templates: 0 errors; lint: 0 diagnostics.