LivePreview before/after hooks #12971
Patrickroelofs
started this conversation in
Feature Requests & Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Lifecycle Hooks for
livePreview
(afterClose
&beforeOpen
)Current behavior
When using Payload's
livePreview
together with Next.js, I often usedraftMode
fromnext/headers
to enable draft previews.This is a simple and effective way to enable draft functionality in a Payload + Next.js project. However, one limitation is the lack of lifecycle hooks for when the preview is opened or closed.
Problem
Once draftMode is enabled, it stays enabled until manually cleared. There is currently no built-in way to detect when the preview has been closed, which makes it difficult to reset state (e.g., disabling draftMode).
Because draftMode().enable() sets a cookie, there’s a timing issue:
The live preview window may open before the cookie is fully set, which means it opens without draft mode enabled. This causes confusion as the draft content doesn't show immediately unless the user manually refreshes the preview window.
Suggested Solution
Add support for optional lifecycle hooks on the livePreview object:
this request was re-written with chatgpt, since I suck at writing clear descriptions
Beta Was this translation helpful? Give feedback.
All reactions