Skip to content

Bug: Autosave invalidates query cache and resets form fields to server state #295

Closed

Description

@Vallhalen

Description

When editing a content entry with many custom fields, autosave fires after 2 seconds of inactivity and resets all form fields to the previous server state, discarding any unsaved changes the user made.

Steps to Reproduce

  1. Create a collection with many custom fields (e.g. 80+ flat fields for a homepage)
  2. Open the content editor in admin panel
  3. Edit any field value
  4. Wait ~2 seconds for autosave to trigger
  5. Observe: The field reverts to its previous value

Expected Behavior

Autosave should save the current form state without resetting the form. User edits should persist after autosave completes.

Actual Behavior

After autosave completes, the form fields revert to the server state (the values before the edit). The user's changes are lost.

Manual save (clicking the Save button) works correctly — changes persist.

Root Cause (from source analysis)

In the admin bundle (@emdash-cms/admin/dist/index.js):

  1. Autosave mutation (autosaveMutation) calls updateContent() with skipRevision: true
  2. On success, it calls queryClient.invalidateQueries(["content", collection, id]) — this triggers a refetch of the entry from the server
  3. The refetch updates the item prop, which triggers a useEffect that does:
    setFormData(item.data);  // Resets form to server state
  4. If the autosave payload didn't include all fields (or if there's a race condition between the save completing and the refetch), the form gets reset to stale server data

The useEffect dependency on item?.updatedAt / itemDataString causes a form reset every time the query cache is refreshed.

Suggested Fix

Option A: Don't invalidate the query cache after autosave — just update lastAutosaveAt without refetching:

onSuccess: () => {
    setLastAutosaveAt(new Date());
    // Don't invalidate queries — the local form state IS the source of truth
}

Option B: After refetch, merge server data with local unsaved changes instead of replacing form state.

Environment

  • EmDash v0.1.0
  • Cloudflare Workers
  • Collection with 85+ custom fields
  • Chrome / Edge (latest)

Workaround

Clicking the manual Save button immediately after editing works correctly. Only the debounced autosave exhibits this issue.

Activity

  1. added a commit that references this issue on Apr 13, 2026
    10ebfe1
  2. added a commit that references this issue on Apr 13, 2026
    d5b6a5f
  3. added a commit that references this issue on Apr 27, 2026
    f35e897
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions