Preview
Preview is a feature that allows you to generate a direct link to your front-end application. When enabled, a "preview" button will appear on the Edit View within the Admin Panel with an href pointing to the URL you provide. This will provide your editors with a quick way of navigating to the front-end application where that Document's data is represented. Otherwise, they'd have to determine that URL themselves which is not always straightforward especially in complex apps.
The Preview feature can also be used to achieve something known as "Draft Preview". With Draft Preview, you can navigate to your front-end application and enter "draft mode", where your queries are modified to fetch draft content instead of published content. This is useful for seeing how your content will look before being published. More details.
To add Preview, pass a function to the admin.preview
property in any Collection Config or Global Config:
Options
The preview
function resolves to a string that points to your front-end application with additional URL parameters. This can be an absolute URL or a relative path, and can run async if needed.
The following arguments are provided to the preview
function:
Path | Description |
---|---|
| The data of the Document being edited. This includes changes that have not yet been saved. |
| An object with additional properties. |
The options
object contains the following properties:
Path | Description |
---|---|
| The current locale of the Document being edited. |
| The Payload Request object. |
| The JWT token of the currently authenticated in user. |
If your application requires a fully qualified URL, such as within deploying to Vercel Preview Deployments, you can use the req
property to build this URL:
Draft Preview
The Preview feature can be used to achieve "Draft Preview". After clicking the preview button from the Admin Panel, you can enter into "draft mode" within your front-end application. This will allow you to adjust your page queries to include the draft: true
param. When this param is present on the request, Payload will send back a draft document as opposed to a published one based on the document's _status
field.
To enter draft mode, the URL provided to the preview
function can point to a custom endpoint in your front-end application that sets a cookie or session variable to indicate that draft mode is enabled. This is framework specific, so the mechanisms here very from framework to framework although the underlying concept is the same.
Next.js
If you're using Next.js, you can do the following code to enter Draft Mode.
Step 1: Format the Preview URL
First, format your admin.preview
function to point to a custom endpoint that you'll open on your front-end. This URL should include a few key query search params:
Step 2: Create the Preview Route
Then, create an API route that verifies the preview secret, authenticates the user, and enters draft mode:
/app/preview/route.ts
Step 3: Query Draft Content
Finally, in your front-end application, you can detect draft mode and adjust your queries to include drafts:
/app/[slug]/page.tsx