payloadcms / payloadcms/payload

UnpublishButton does not reflect beforeChange hook modifications in form state

Open
#16,755 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

area: ui stale status: needs-triage v3
Dominant language
TypeScript
Stars
44.8k
Forks
4.2k
Avg merge
2d 21h
Merged PRs (30d)
53

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in @payloadcms/ui/dist/elements/UnpublishButton/index.js around lines 112-116 and run the linked reproduction to observe the stale form value after unpublishing. Trace the successful response and form reset behavior, then verify that values changed by beforeChange are shown immediately without a reload.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
frontend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
67/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.