Skip to content

fix(admin): patch autosave result into the observed content query (#2362) - #2363

Merged
ascorbic merged 2 commits into
emdash-cms:mainfrom
danielmlr:fix/autosave-publish-button-locale-key
Aug 8, 2026
Merged

ascorbic merged 2 commits into
emdash-cms:mainfrom
danielmlr:fix/autosave-publish-button-locale-key

Conversation

@danielmlr

Copy link
Copy Markdown
Member

What does this PR do?

On a site without i18n configured, an autosave persisted the edit but never surfaced the "Publish" button — the editor kept showing the entry as fully published until a reload.

The editor reads the item under a locale-scoped key, and activeLocale is undefined when i18n is off:

queryKey: ["content", collection, id, { locale: activeLocale }]

patchAutosaveQueries wrote the saved item back with setQueryData under an exact key built from variables.targetLocale (rawItem?.locale ?? activeLocale), which is the DB default "en" even on a single-locale site. Exact matching meant the patch landed on [..., { locale: "en" }] — a cache entry nothing observes. The editor kept the revision pointers it loaded with, getDraftStatus() returned "published" instead of "published_with_changes", and no publish button rendered.

This is the same mismatch #1557 described. #1568 fixed it for every invalidateQueries call but not here, because this path deliberately patches the cache instead of invalidating — the autosave must not refetch server state into the form while the user is still typing (#295 / #302). So the fix keeps the patch and only widens its match to the (collection, id) prefix, exactly as the mutation invalidations already do. ["content", collection, id, …] has a single reader, so nothing else is touched.

Server-side behaviour was correct throughout — publish() clears draft_revision_id, the next autosave creates a new draft revision, and the entry genuinely is "published with changes". Only the client cache was wrong, which is why a reload always fixed it.

On the test placement. router.test.tsx already has a suite called ContentEditPage – autosave cache patching running the affected configuration (i18n: undefined, item locale: "en"), and it passes on the broken code: it mocks ContentEditor and asserts title and slug, which both come from the ["revision", draftRevisionId] half of the patch — that key has no locale and always landed. The regression therefore needs the real editor, so the new test goes in publish-button-locale.test.tsx alongside the #1557 cases. It fails on main and passes with this change.

One detail worth flagging for reviewers: the new test registers the PUT handler as /_emdash/api/content/posts/post_1?locale=en. Without the query string, createMockFetch's prefix fallback serves the GET handler's response to the PUT, and the test passes for the wrong reason.

Closes #2362

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) — full @emdash-cms/admin suite: 115 files, 1358 tests
  • 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) — n/a, no strings changed; no messages.po changes included
  • I have added a changeset (if this PR changes a published package)
  • New features link to an approved Discussion — n/a, bug fix

AI-generated code disclosure

  • This PR includes AI-generated code — model/tool: Claude Opus 5 (Claude Code)

Screenshots / test output

Regression test on main, fix reverted:

 ❯ tests/publish-button-locale.test.tsx:388:4
Caused by: Error: Matcher did not succeed in time.

 Test Files  1 failed (1)
      Tests  1 failed | 2 passed (3)

With the fix applied:

 Test Files  1 passed (1)
      Tests  3 passed (3)

Full admin suite:

 Test Files  115 passed (115)
      Tests  1358 passed (1358)

🤖 Generated with Claude Code

@github-actions github-actions Bot added the review/needs-review No maintainer or bot review yet label Aug 7, 2026
@github-actions

github-actions Bot commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@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 fixes a real, well-diagnosed bug: on non-i18n sites the autosave patch was landing on a React Query key scoped by the DB default locale ("en") while the editor reads a key with locale: undefined, so the editor never saw the updated revision pointers and suppressed the Publish button.

The approach is sound and matches the existing prefix-based invalidation strategy used elsewhere in ContentEditPage (#1568). I checked:

  • router.tsx — the patchAutosaveQueries helper now uses queryClient.setQueriesData with a ["content", collection, id] prefix filter instead of an exact locale-scoped key, and the unused locale parameter is removed from its signature.
  • packages/admin/tests/publish-button-locale.test.tsx — a new behavioral regression test using the real ContentEditor asserts that the Publish button appears after autosave. It correctly pins the PUT handler to the exact ?locale=en URL so the mock's prefix fallback doesn't accidentally serve the GET handler's clean item.
  • API client and editor — confirmed updateContent appends ?locale= when a locale is provided, getDraftStatus derives the button state from liveRevisionId/draftRevisionId, and only ContentEditPage observes the id-scoped content key.
  • Changeset — present and user-facing.

The only thing that gives me pause is that the prefix patch updates every cached locale variant for the same (collection, id), not just the currently observed one. In i18n mode, if a user switches locales and then autosaves, a previously viewed locale's cache entry could briefly hold the current locale's item until its next refetch. This is a minor cache-coherency concern, not a blocker, because ContentEditPage renders one locale at a time.

Headline: fix is correct, test is good, one low-risk suggestion about narrowing the patch scope.

// editor reads `{ locale: activeLocale }`, undefined when i18n is off, while the
// saved item carries the DB default "en". An exact key would write to an entry
// nobody observes, leaving the editor on stale revision pointers.
queryClient.setQueriesData<ContentItem>({ queryKey: ["content", collection, id] }, savedItem);

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.

[suggestion] The prefix match fixes the i18n-off mismatch, but it also updates every cached locale variant for this (collection, id) — including inactive entries for other locales. In i18n mode, switching locales and then autosaving could briefly overwrite a previously viewed locale's cache with the current locale's item until that query next refetches.

If you want to avoid that, pass activeLocale (the locale the editor actually observes) and patch the exact key instead of the prefix:

Suggested change
queryClient.setQueriesData<ContentItem>({ queryKey: ["content", collection, id] }, savedItem);
queryClient.setQueryData<ContentItem>(
["content", collection, id, { locale: activeLocale }],
savedItem,
);

That still fixes the reported bug because activeLocale is undefined when i18n is off, matching the editor's reader key exactly, while staying scoped to the current locale on i18n sites.

@danielmlr

Copy link
Copy Markdown
Member Author

I have read the CLA Document and I hereby sign the CLA

github-actions Bot added a commit that referenced this pull request Aug 7, 2026
@danielmlr

Copy link
Copy Markdown
Member Author

The only thing that gives me pause is that the prefix patch updates every cached locale variant for the same (collection, id) […] if a user switches locales and then autosaves, a previously viewed locale's cache entry could briefly hold the current locale's item until its next refetch.

I looked into whether that state is reachable, and I don't think it is: a given id never has more than one locale variant in the cache.

Translations are separate rows with their own IDs (row-per-locale, migration 019_i18n.ts), and every navigation into the editor pairs an ID with its own locale rather than re-reading the same ID under a different one:

  • ContentSettingsPanel.tsx (opening an existing translation) — params: { collection, id: tr.id }, search: { locale: tr.locale }
  • router.tsx, createTranslationMutation.onSuccess — params: { collection, id: result.id }, search: { locale: result.locale }
  • LocaleSwitcher is mounted on the list route, where switching navigates to /content/$collection and the list then yields each row's own ID

So ["content", collection, id, { locale }] only ever gets populated with that row's own locale, and the prefix filter matches exactly the one entry the editor observes. In non-i18n mode it's the single { locale: undefined } entry, which is the case the PR fixes.

Worth noting the prefix is also the more robust option in the one place where the target genuinely diverges from the route: autosaveMutation carries variables.targetId, which can be a previously edited entry whose in-flight autosave resolves after the user has already navigated to another one (ContentEditPage has explicit handling for this — recordAutosaveCompletion / autosaveCompletionSequenceRef). Patching under the current activeLocale would miss that entry's key; the prefix hits it.

Happy to narrow it to an exact { locale: activeLocale } key if a maintainer prefers the tighter scope — it fixes the reported bug just as well, at the cost of the stale-target case above.

…dash-cms#2362)

The editor reads the item under a locale-scoped key, and `activeLocale` is
undefined when i18n is not configured. `patchAutosaveQueries` wrote the saved
item back with an exact key built from `rawItem.locale` — the DB default "en" —
so the patch landed on a cache entry no component observes. The editor kept the
revision pointers it loaded with, `getDraftStatus()` stayed "published", and the
publish button never appeared after an autosave until a reload.

Match by (collection, id) prefix instead, as the mutation invalidations already
do since emdash-cms#1568.

The existing autosave suite in router.test.tsx runs the affected configuration
but mocks ContentEditor and asserts title and slug, both of which come from the
revision half of the patch that always worked. The regression test therefore
goes in publish-button-locale.test.tsx, which renders the real editor.

Co-Authored-By: Claude Opus 5 <[email protected]>
@danielmlr
danielmlr force-pushed the fix/autosave-publish-button-locale-key branch from 7f1ac98 to 6996d91 Compare August 7, 2026 17:09
@changeset-bot

changeset-bot Bot commented Aug 7, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5c93b61

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

This PR includes changesets to release 17 packages
Name Type
@emdash-cms/admin Patch
emdash Patch
@emdash-cms/cloudflare Patch
@emdash-cms/sandbox-workerd Patch
@emdash-cms/plugin-mcp-smoke Patch
@emdash-cms/fixture-perf-site Patch
@emdash-cms/perf-demo-site Patch
@emdash-cms/cache-demo-site Patch
@emdash-cms/do-demo-site Patch
@emdash-cms/do-solo-demo-site Patch
@emdash-cms/auth Patch
@emdash-cms/blocks Patch
@emdash-cms/gutenberg-to-portable-text Patch
@emdash-cms/x402 Patch
create-emdash 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 github-actions Bot added review/needs-rereview New commits since the last review and removed review/needs-review No maintainer or bot review yet labels Aug 7, 2026

@ascorbic ascorbic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I agree: the error state is unreachable. Thanks for this!

@ascorbic
ascorbic enabled auto-merge (squash) August 8, 2026 10:25
@github-actions github-actions Bot added review/approved Approved; no new commits since and removed review/needs-rereview New commits since the last review labels Aug 8, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 8, 2026

Copy link
Copy Markdown

Open in StackBlitz

@emdash-cms/admin

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

@emdash-cms/auth

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

@emdash-cms/auth-atproto

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

@emdash-cms/blocks

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

@emdash-cms/cloudflare

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

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

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

emdash

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

create-emdash

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

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

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

@emdash-cms/plugin-cli

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

@emdash-cms/plugin-types

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

@emdash-cms/registry-client

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

@emdash-cms/registry-lexicons

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

@emdash-cms/registry-verification

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

@emdash-cms/sandbox-workerd

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

@emdash-cms/x402

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

@emdash-cms/plugin-ai-moderation

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

@emdash-cms/plugin-atproto

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

@emdash-cms/plugin-audit-log

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

@emdash-cms/plugin-color

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

@emdash-cms/plugin-embeds

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

@emdash-cms/plugin-field-kit

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

@emdash-cms/plugin-forms

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

@emdash-cms/plugin-webhook-notifier

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

commit: 5c93b61

@ascorbic
ascorbic merged commit 6215e93 into emdash-cms:main Aug 8, 2026
68 of 69 checks passed
@emdashbot emdashbot Bot mentioned this pull request Aug 8, 2026
@danielmlr
danielmlr deleted the fix/autosave-publish-button-locale-key branch August 10, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Autosave never surfaces the publish button on sites without i18n (locale-scoped setQueryData key)

2 participants