Repository navigation
fix(core): sync non-translatable fields when an entry is published - #3090
Conversation
Fails on main. On a collection with revisions a save stages a draft and the publish path never syncs, so the other locales keep their old values. On a collection without revisions the sync runs, but it rewrites the other locales without advancing their version, so a `_rev` read before the sync still validates and a stale save overwrites the synced value. The remaining cases pin what a fix has to preserve: a pending draft in another locale keeps a value it changed on its own, republishing or unpublishing that locale keeps the synced value, and a locale that already holds the value is not touched. A publication copies only the values it changed, so publishing a translation that never held a shared value, or one whose value fell behind, leaves the other locales' value alone. Media usage for the other locales has to follow a publish the way it already follows a save, a cleared image included.
Publishing is where a draft's values reach the content columns, so it is now where they reach the other locales too. The sync moves into `ContentRepository.syncNonTranslatableFields`, called by the publish handler on collections with revisions and by the update path on collections without them. At publication it copies only the values the publication changed, compared with the entry's row before it, so publishing a translation that never held a shared value, or one whose value fell behind, does not overwrite the other locales. For every other entry in the translation group it: - skips the entry when it already holds every value, so its `updated_at`, which feeds sitemap `<lastmod>` and `dateModified`, moves only when its live content does; - writes the changed columns and advances `version` and `updated_at`, so a `_rev` read before the sync is refused; - on collections with revisions, points `live_revision_id` at a new revision carrying the synced values, because republishing and unpublishing read that revision rather than the columns; - carries a value into a pending draft only where the draft still holds the entry's previous value, so an unpublished edit to the field survives and syncs once that locale is published. The write is a compare-and-swap on `version` and both revision pointers, retried against a fresh read the way draft staging is. Trashed entries are still synced, as before, and so is an entry another editor holds the lock on; the version bump turns that editor's next save into a conflict. After a publish the runtime refreshes media usage for the other locales, as it already did after a save on a collection without revisions. The published entry's data leaves out NULL columns, so that lookup counts an absent non-translatable field as cleared; otherwise an image the publication cleared would stay recorded as used by the other locales.
🦋 Changeset detectedLatest commit: 6596a10 The changes in this PR will be included in the next version bump. This PR includes changesets to release 18 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 changes 681 lines across 8 files. Large PRs are harder to review and more likely to be closed without review. 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-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 the right fix for the described bug. Moving non-translatable field sync from the save path to the publish path (for revision-enabled collections) matches the product model where a draft save should not propagate to other locales. The implementation is careful: it only copies values that actually changed, preserves per-locale updated_at for unchanged siblings, bumps version so stale _rev writes fail, creates new live revisions on revision collections so republish/unpublish stay consistent, and carries synced values into a draft only when the draft hadn’t diverged on that field. Media-usage refresh is wired into the publish path with the absentAsCleared option so clearing a non-translatable image on publish still refreshes siblings.
I read the full diff, the changed handler/repository/runtime files, the transaction.ts helper (which explains the documented D1 non-atomicity), the RevisionRepository, and the new/updated tests. SQL is parameterized and identifiers are validated. The tests are integration-level and exercise the exact regressions/fixed behaviors described. The changeset and docs paragraph are user-facing and accurate.
I have only one minor suggestion: the handleContentPublish docstring still opens with “Publication is one atomic content-row statement,” which is misleading now that the handler also runs the locale sync and slug redirect. Rephrasing the opening sentence keeps the docstring from contradicting the rest of the description. With that addressed or accepted, this looks ready to merge.
| @@ -1649,6 +1648,21 @@ export async function handleContentPublish( | |||
| expectedRevision, | |||
| ); | |||
There was a problem hiding this comment.
[suggestion] The docstring still says "Publication is one atomic content-row statement," but the handler now performs the publish, the non-translatable locale sync, and the slug redirect. The second sentence already notes the side writes, so the opening line contradicts the rest of the comment.
| ); | |
| /** | |
| * Publish content immediately. | |
| * | |
| * Promotes the staged draft to live and runs any non-translatable field sync | |
| * and slug-redirect side writes. On databases that support transactions, | |
| * the publication, sync, and redirect stay grouped atomically. | |
| */ |
There was a problem hiding this comment.
I'm leaving this sentence as it is. It comes from #2349, not this PR, and it describes the publication statement itself: that one content-row write is why a publish stays atomic on D1, where the locale sync and the slug redirect run after it without a transaction. The second sentence refers back to it. The suggested wording drops that, and a collection without revisions has no staged draft to promote.
Overlapping PRsThis PR modifies files that are also changed by other open PRs:
This may cause merge conflicts or duplicated work. A maintainer will coordinate. |
What does this PR do?
Syncs non-translatable fields to an entry's other locales when it is published, on collections with revisions, which is the default. A save there only stages a draft, and publishing promoted that draft without running the sync, so the other locales kept their old values.
The sync now runs at publication through
ContentRepository.syncNonTranslatableFields, which also replaces the rawUPDATEon the save path of collections without revisions:updated_at, which the sitemap's<lastmod>reads, moves only when its live content changes.versionandupdated_at, so a_revread before the sync is refused instead of overwriting the synced value.Saving a draft still leaves the other locales and their
updated_atalone (#2143), and hooks fire for the published entry only, as with the save-path sync. A locale that another editor holds the lock on (#2919) is still written, so an open translation never holds back publishing elsewhere, and the version bump turns that editor's next save into a conflict. On D1 the sync is not atomic with the publication, like the slug redirect written after it; the publication itself is still one content-row statement, so thehandleContentPublishdocstring keeps its opening sentence from #2349. The internationalization guide now says the copy happens at publication.Closes #2911
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: no admin UI change)AI-generated code disclosure
Screenshots / test output
Not applicable, no UI change.
non-translatable-sync.test.tscovers both kinds of collection on SQLite and PostgreSQL. Seven of its eleven cases fail onmain; the other four pin what the fix must leave alone. The cases for a matching locale, the new live revision, a pending draft and an unchanged or missing value each fail when that part is removed, run one mutation at a time. Both new cases inmedia-usage-runtime-refresh.test.tsfail without the runtime change. The fullpackages/coresuite passes.