Unpublishing a document with a newer draft doesn't revalidate it: afterChange previousDoc is the latest draft
Maintainers usually reply within 1 day
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
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
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
pnpm install,pnpm devonce to create the database, then stop it and runpnpm build && pnpm start.- Publish a page and load it: 200.
- Edit it and save a draft (don't publish). Load the page again: 200.
- Click Unpublish.
- 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
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from payloadcms/payload
-
db-d1-sqlite: upsert is aliased to updateOne, so payload.kv.set never inserts new keysPossibly taken @pcbeingused333 claimed this today. OpenBug db: d1-sqlite Payload OSS
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
payloadcms/payload#19766 ·
Maintainers usually reply within 1 day
-
Document bar (.doc-controls__content) overflows a 375 px viewport, clips "Created" and fails axe scrollable-region-focusablePossibly taken A pull request linked to this issue is open or already merged. Openarea: ui Bug
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
payloadcms/payload#19690 ·
Maintainers usually reply within 1 day
-
[v4] Blocks thumbnails croped 3:2Possibly taken @samiarif02 claimed this 2 days ago. Openarea: ui Bug Payload OSS status: needs-triage v3
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
payloadcms/payload#19609 ·
Maintainers usually reply within 1 day
-
Scheduled publish jobs are not deleted when their document is deleted (string id in the job, raw id in the cleanup)Possibly taken @volodymyrshk claimed this 10 days ago. Openarea: core Bug db: sqlite
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
payloadcms/payload#19448 ·
Maintainers usually reply within 1 day
-
plugin-multi-tenant: `isGlobal` collections redirect tenant-scoped users to `/admin` since 3.93.0 (GlobalViewRedirect prop-name mismatch)Possibly taken @JarrodMFlesch claimed this 10 days ago. OpenBug plugin: multi-tenant
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
payloadcms/payload#19420 · 1 assignee ·
Maintainers usually reply within 1 day
All issues in payloadcms/payload
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
milind-soni/OpenMausBot#6730 ·
Maintainers usually reply within 1 day
-
`@angular/[email protected]` depends on vulnerable Vite 7.3.7 - GHSA-p4f5-76q4-ffq7 (Critical RCE)Openarea: @angular/build
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
angular/angular-cli#34764 ·
Maintainers usually reply within 1 day
-
status/need-triage
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
google-gemini/gemini-cli#30384 · 1 comment ·
Maintainers usually reply within 1 day
-
P2
Difficulty 1/5 Under an hour Newbie friendliness 84/100
Maintainers usually reply within 1 day
-
area:image documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day