Skip to content

UnpublishButton does not reflect beforeChange hook modifications in form state #16755

@busbyk

Description

@busbyk

Describe the Bug

When a beforeChange hook modifies field values during an unpublish operation (e.g., setting publishedAt to null when transitioning from published to draft), the database is updated correctly but the admin UI form still shows the old value. A manual page reload shows the correct value.

Root cause: In @payloadcms/ui/dist/elements/UnpublishButton/index.js (lines 112-116), after a successful unpublish, the form is reset with dataFromProps (stale data from initial page load) and only _status is overridden:

if (res.status === 200) {
  void resetForm({
    ...(dataFromProps || {}),
    _status: 'draft'
  });

The server response is never read (res.json() is not called on success), so any beforeChange hook modifications are invisible to the user until they reload. The fix in PR #13416 (v3.51.0) addressed this for autosave but the Unpublish button has its own form reset logic that still uses stale data.

Link to the code that reproduces this issue

https://github.com/busbyk/unpublish-hook-stale-form-reproduction

Reproduction Steps

  1. Clone the repo and run pnpm install && pnpm dev
  2. Log in to admin panel at http://localhost:3000/admin
  3. Create a new Page with a title and click Publish -- note the publishedAt date auto-set in sidebar
  4. Open the document controls dropdown and click Unpublish, confirm
  5. Observe: Status changes to "Draft" but publishedAt still shows the old date
  6. Verify via API: fetch('/api/pages/<id>?depth=0').then(r => r.json()).then(d => console.log(d.publishedAt)) returns null
  7. Reload the page: publishedAt field is now correctly empty

Which area(s) are affected?

area: ui

Environment Info

Binaries:
  Node: 24.13.0
  npm: 11.6.2
  pnpm: 10.28.0
Relevant Packages:
  payload: 3.85.0
  next: 16.2.6
  @payloadcms/db-sqlite: 3.85.0
  @payloadcms/ui/shared: 3.85.0
  @payloadcms/richtext-lexical: 3.85.0
  react: 19.2.6
  react-dom: 19.2.6
Operating System:
  Platform: darwin
  Arch: arm64

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: uiRelated to the admin panel.status: needs-triagePossible bug which hasn't been reproduced yetv3

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions