Server-side Live Preview
Server-side Live Preview works by making a roundtrip to the server every time your document is saved, i.e. draft save, autosave, or publish. While using Live Preview, the Admin Panel emits a new window.postMessage
event which your front-end application can use to invoke this process. In Next.js, this means simply calling router.refresh()
which will hydrate the HTML using new data straight from the Local API.
If your front-end application is built with React, you can use the RefreshRouteOnChange
function that Payload provides. In the future, all other major frameworks like Vue and Svelte will be officially supported. If you are using any of these frameworks today, you can still integrate with Live Preview yourself using the underlying tooling that Payload provides. See building your own router refresh component for more information.
React
If your front-end application is built with server-side React like Next.js App Router, you can use the RefreshRouteOnSave
component that Payload provides.
First, install the @payloadcms/live-preview-react
package:
Then, render the RefreshRouteOnSave
component anywhere in your page.tsx
. Here's an example:
page.tsx
:
RefreshRouteOnSave.tsx
:
Building your own router refresh component
No matter what front-end framework you are using, you can build your own component using the same underlying tooling that Payload provides.
First, install the base @payloadcms/live-preview
package:
This package provides the following functions:
Path | Description |
---|---|
ready | Sends a window.postMessage event to the Admin Panel to indicate that the front-end is ready to receive messages. |
isDocumentEvent | Checks if a MessageEvent originates from the Admin Panel and is a document-level event, i.e. draft save, autosave, publish, etc. |
With these functions, you can build your own hook using your front-end framework of choice:
Here is an example of what the same RefreshRouteOnSave
React component from above looks like under the hood:
Example
For a working demonstration of this, check out the official Live Preview Example. There you will find a fully working example of how to implement Live Preview in your Next.js App Router application.
Troubleshooting
Updates do not appear as fast as client-side Live Preview
If you are noticing that updates feel less snappy than client-side Live Preview (i.e. the useLivePreview
hook), this is because of how the two differ in how they work—instead of emitting events against form state, server-side Live Preview refreshes the route after a new document is saved.
Use Autosave to mimic this effect server-side. Try decreasing the value of versions.autoSave.interval
to make the experience feel more responsive:
Iframe refuses to connect
If your front-end application has set a Content Security Policy (CSP) that blocks the Admin Panel from loading your front-end application, the iframe will not be able to load your site. To resolve this, you can whitelist the Admin Panel's domain in your CSP by setting the frame-ancestors
directive: