Unpublishing a document with a newer draft doesn't revalidate it: afterChange previousDoc is the latest draft

Open
#19,531 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@hassanabrar202 is already working on this.

Since Nov 13, 2026.

  • #19561 by @hassanabrar202 — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
54/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
next.js, sqlite, typescript
Domain
api, backend

Research direction

Start with packages/payload/src/collections/operations/updateByID.ts, then follow getLatestCollectionVersion into utilities/update.ts to see how originalDoc becomes previousDoc in the afterChange hook. Use the linked reproduction’s repro.sh and website template to confirm the draft-save and unpublish behavior. Done when unpublishing with a pending draft makes the public page return 404 and the draft-save case does not incorrectly revalidate the published page.

Written by the indexing model from the issue text.

Description

area: core area: templates Bug Payload OSS
Describe the Bug

On a drafts-enabled collection, updateByID loads the pre-update document with getLatestCollectionVersion (the latest: true version row) and passes it to afterChange as previousDoc. For a published document with a newer draft, that is the draft, not the published document in the main collection.

The website template's revalidatePage / revalidatePost revalidate an unpublished path only when previousDoc?._status === 'published' && doc._status !== 'published'. With a newer draft pending:

  • saving the draft fires the revalidation (previous = published, doc = draft), although the published page did not change;
  • unpublishing fires nothing (previous = draft, doc = draft), so the page keeps being served.
Link to the code that reproduces this issue

https://github.com/jmtorres/payload-unpublish-previousdoc

Unmodified website template (sqlite) plus repro.sh, which sends the same requests the admin panel sends for Save Draft and Unpublish.

Reproduction Steps
  1. pnpm install, pnpm dev once to create the database, then stop it and run pnpm build && pnpm start.
  2. Publish a page and load it: 200.
  3. Edit it and save a draft (don't publish). Load the page again: 200.
  4. Click Unpublish.
  5. Load the page: still 200. The public REST API returns no document for that slug.

A page unpublished without a pending draft returns 404 at step 5. ./repro.sh runs both and prints:

published        /control-… 200   /case-… 200
after draft save /control-… 200   /case-… 200
after unpublish  /control-… 404   /case-… 200
expected         /control-… 404   /case-… 404

The page load in step 3 matters: the draft save revalidates the path, so without a visit in between the entry is still stale when the unpublish happens and the bug is hidden.

Expected behavior

The unpublished page returns 404.

Which area(s) are affected?

area: core, area: templates

Environment Info
payload: 3.93.0
next: 16.4.1
@payloadcms/db-sqlite: 3.93.0
Node: 24.22.2
pnpm: 12.16.1
macOS (Darwin 25.6.0)

The same code path is on main (4.0.0-beta.8): updateByID.ts → getLatestCollectionVersion → previousDoc: originalDoc in utilities/update.ts.

Suggested fix

Either give hooks the document the write replaces (for example a previousPublishedDoc, or previousDoc read from the main collection when the write is not a draft save), or have the template record the published → draft transition in beforeChange and read it in afterChange, as suggested in #4405. The docs for previousDoc ("The Document before changes were applied") should say which document it is.

Related: #4405

Dominant language
TypeScript
Stars
45.8k
Forks
4.4k
Avg merge
2d 16h
Merged PRs (30d)
251

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

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.

More from payloadcms/payload

All issues in payloadcms/payload

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.