Skip to content

fix(templates): answer content misses with a real 404 instead of a redirect - #3650

Merged
ascorbic merged 1 commit into
emdash-cms:mainfrom
swissky:fix/content-miss-real-404
Sep 30, 2026
Merged

ascorbic merged 1 commit into
emdash-cms:mainfrom
swissky:fix/content-miss-real-404

Conversation

@swissky

@swissky swissky commented Sep 29, 2026

Copy link
Copy Markdown
Member

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 returns 302, and only /404 returns 404, so search engines see a redirect to one shared error URL instead of a missing page. This switches them to Astro.rewrite("/404"), which is what Astro recommends for this case: the missing URL itself answers 404 with 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 the building-emdash-site skill, including its copies under templates/*/.agents/skills (edited in place; re-running sync-template-skills.sh would also pull unrelated skill changes into the templates)
  • demos/*, infra/* demos, fixtures/perf-site, the e2e and integration fixtures
  • docs code examples (and the one sentence in reference/api.mdx that said "redirects"), the registry-loader README, and the getEmDashEntry JSDoc example

apps/plugins-site is handled separately in #3643.

Astro.rewrite() runs the middleware again for /404 inside the original request, which surfaced two things in core, both fixed here:

  • Toolbar injected twice. For a logged-in editor, the inner pass injected the visual editing toolbar into the 404 page and the outer pass injected it again (two #emdash-toolbar elements); in toolbar: "client" mode every visitor got the bootstrap script twice. Injection now skips HTML that already contains the toolbar or bootstrap, while still marking toolbar responses private, no-store and opting them out of the route cache.
  • 404 in the route cache. A URL that matches no route has no route-cache handle, but a rewritten content miss runs inside the matched route's handle, and the 404 page's layout adds its cache hints. The redirect middleware (which already inspects the final status for the 404 log) now opts every 404 response out of the route cache, so a URL that missed before its entry was published doesn't keep serving a cached 404.

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 ?_edit gets the bootstrap from the inner pass (the rewrite drops the query) plus the full toolbar from the outer pass.

Closes #

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.
  • 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/...
  • I have included screenshots below if this PR changes the UI

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 emdash patch changeset covers the two middleware fixes.

AI-generated code disclosure

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

Screenshots / test output

Not applicable (no UI change). Verified on demos/simple (astro dev):

Request Before After
/posts/no-such-post, /pages/…, /category/…, /tag/… (anonymous) 302 → /404 404, site 404 page at the original URL
404 log missed path missed path, once per request, no /404 entry
/posts/no-such-post as editor — 404, one toolbar (was two with the rewrite before the core fix), private, no-store
existing post 200 200

Tests: new cases in request-context-toolbar-mode.test.ts (toolbar and bootstrap injected once through a nested rewrite pass) and middleware-redirect.test.ts (404 opts out of the route cache, 200 doesn't); each fails with its fix removed. Full packages/core suite: 8969 passed. pnpm typecheck, typecheck:demos, typecheck:templates: 0 errors; lint: 0 diagnostics.

@changeset-bot

changeset-bot Bot commented Sep 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 18046eb

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

@github-actions

Copy link
Copy Markdown
Contributor

Scope check

This 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.

@pkg-pr-new

pkg-pr-new Bot commented Sep 29, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

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

create-emdash

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

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

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

@emdash-cms/plugin-cli

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

@emdash-cms/plugin-test

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

@emdash-cms/plugin-types

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

@emdash-cms/registry-client

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

@emdash-cms/registry-lexicons

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

@emdash-cms/registry-loader

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

@emdash-cms/registry-moderation

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

@emdash-cms/registry-verification

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

@emdash-cms/sandbox-workerd

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: 18046eb

@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 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:

  1. Toolbar/bootstrap deduplication — injectBeforeBodyEnd now checks for an existing marker before injecting HTML again. The markers (id="emdash-toolbar" and <!-- EmDash Toolbar Bootstrap -->) match the strings emitted by renderToolbar() and renderToolbarBootstrap(), so nested rewrite passes do not duplicate injected markup.
  2. 404 route-cache opt-out — after next() returns a 404, the redirect middleware calls context.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.

@emdashbot emdashbot Bot added review/approved Approved; no new commits since and removed review/needs-review No maintainer or bot review yet labels Sep 29, 2026
@ascorbic
ascorbic merged commit 6940899 into emdash-cms:main Sep 30, 2026
44 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants