Skip to content

fix: retry creating an entry when an unrelated commit moved the branch head - #1653

Open
colingreig wants to merge 1 commit into
Thinkmill:mainfrom
colingreig:fix/create-retry-stale-data
Open

colingreig wants to merge 1 commit into
Thinkmill:mainfrom
colingreig:fix/create-retry-stale-data

Conversation

@colingreig

@colingreig colingreig commented Nov 4, 2026 •

Copy link
Copy Markdown

Summary

When saving hits STALE_DATA, useUpsertItem retries only if the entry's tree key is unchanged. For a create, currentLocalTreeKey is undefined while getTreeKey(...) always returns a string, so the comparison never matched and every create after an unrelated commit showed "This entry has been updated since it was opened".

With no local tree key, the remote key is now compared against the key of an absent entry:

-if (treeKey === args.currentLocalTreeKey) {
+const expectedTreeKey =
+  args.currentLocalTreeKey ?? getTreeKey(directoriesForTreeKey, new Map());
+if (treeKey === expectedTreeKey) {

A create retries when the entry (and its asset directories) still don't exist on the new head, and still shows the dialog if the same slug was created meanwhile. The retry uses the checked head as expectedHeadOid, so a further head move surfaces an error instead of overwriting.

CreateItemLocal and CreateItemCollab both pass currentLocalTreeKey: undefined into useUpsertItem, so both get the fix.

Tests

New packages/keystatic/src/app/updating.test.tsx (hook-level, mocked GitHub/urql layer):

  • create after an unrelated commit retries (fails before the fix)
  • create whose slug exists remotely shows the dialog
  • head moves again between the check and the retry: error, no overwrite (fails before the fix)
  • edit, entry unchanged: retries
  • edit, entry changed: dialog

Before the fix: 2 failed, 3 passed. After: 5 passed. Full keystatic vitest project: 58 files, 707 passed, 8 skipped. eslint, prettier and tsc clean. Includes a patch changeset for @keystatic/core.

Refs #1626

@colingreig
colingreig requested a review from emmatown as a code owner November 4, 2026 17:37
@changeset-bot

changeset-bot Bot commented Nov 4, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 344f062

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

This PR includes changesets to release 1 package
Name Type
@keystatic/core 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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant