Repository navigation
fix(admin): patch autosave result into the observed content query (#2362) - #2363
Conversation
|
All contributors have signed the CLA ✍️ ✅ |
There was a problem hiding this comment.
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— thepatchAutosaveQuerieshelper now usesqueryClient.setQueriesDatawith a["content", collection, id]prefix filter instead of an exact locale-scoped key, and the unusedlocaleparameter is removed from its signature.packages/admin/tests/publish-button-locale.test.tsx— a new behavioral regression test using the realContentEditorasserts that the Publish button appears after autosave. It correctly pins the PUT handler to the exact?locale=enURL so the mock's prefix fallback doesn't accidentally serve the GET handler's clean item.- API client and editor — confirmed
updateContentappends?locale=when a locale is provided,getDraftStatusderives the button state fromliveRevisionId/draftRevisionId, and onlyContentEditPageobserves theid-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); |
There was a problem hiding this comment.
[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:
| 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.
|
I have read the CLA Document and I hereby sign the CLA |
I looked into whether that state is reachable, and I don't think it is: a given Translations are separate rows with their own IDs (row-per-locale, migration
So Worth noting the prefix is also the more robust option in the one place where the target genuinely diverges from the route: Happy to narrow it to an exact |
…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]>
7f1ac98 to
6996d91
Compare
🦋 Changeset detectedLatest commit: 5c93b61 The changes in this PR will be included in the next version bump. This PR includes changesets to release 17 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 |
ascorbic
left a comment
There was a problem hiding this comment.
I agree: the error state is unreachable. Thanks for this!
@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-types
@emdash-cms/registry-client
@emdash-cms/registry-lexicons
@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: |
What does this PR do?
On a site without
i18nconfigured, 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
activeLocaleisundefinedwhen i18n is off:patchAutosaveQuerieswrote the saved item back withsetQueryDataunder an exact key built fromvariables.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
invalidateQueriescall 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()clearsdraft_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.tsxalready has a suite calledContentEditPage – autosave cache patchingrunning the affected configuration (i18n: undefined, itemlocale: "en"), and it passes on the broken code: it mocksContentEditorand 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 inpublish-button-locale.test.tsxalongside the #1557 cases. It fails onmainand 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
Checklist
pnpm typecheckpassespnpm lintpassespnpm testpasses (or targeted tests for my change) — full@emdash-cms/adminsuite: 115 files, 1358 testspnpm formathas been runmessages.pochanges includedAI-generated code disclosure
Screenshots / test output
Regression test on
main, fix reverted:With the fix applied:
Full admin suite:
🤖 Generated with Claude Code