Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

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:

1
import type { CollectionConfig } from 'payload'
2
3
export const Pages: CollectionConfig = {
4
slug: 'pages',
5
admin: {
6
preview: ({ slug }) => `http://localhost:3000/${slug}`,
7
},
8
fields: [
9
{
10
name: 'slug',
11
type: 'text',
12
}
13
],
14
}

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

doc

The data of the Document being edited. This includes changes that have not yet been saved.

options

An object with additional properties.

The options object contains the following properties:

Path

Description

locale

The current locale of the Document being edited.

req

The Payload Request object.

token

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:

1
preview: (doc, { req }) => `${req.protocol}//${req.host}/${doc.slug}`

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:

1
import type { CollectionConfig } from 'payload'
2
3
export const Pages: CollectionConfig = {
4
slug: 'pages',
5
admin: {
6
preview: ({ slug, collection }) => {
7
const encodedParams = new URLSearchParams({
8
slug,
9
collection,
10
path: `/${slug}`,
11
previewSecret: process.env.PREVIEW_SECRET || ''
12
})
13
14
return `/preview?${encodedParams.toString()}`
15
}
16
},
17
fields: [
18
{
19
name: 'slug',
20
type: 'text',
21
}
22
],
23
}

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

1
import type { CollectionSlug, PayloadRequest } from 'payload'
2
import { getPayload } from 'payload'
3
4
import { draftMode } from 'next/headers'
5
import { redirect } from 'next/navigation'
6
7
import configPromise from '@payload-config'
8
9
export async function GET(
10
req: {
11
cookies: {
12
get: (name: string) => {
13
value: string
14
}
15
}
16
} & Request,
17
): Promise<Response> {
18
const payload = await getPayload({ config: configPromise })
19
20
const { searchParams } = new URL(req.url)
21
22
const path = searchParams.get('path')
23
const collection = searchParams.get('collection') as CollectionSlug
24
const slug = searchParams.get('slug')
25
const previewSecret = searchParams.get('previewSecret')
26
27
if (previewSecret !== process.env.PREVIEW_SECRET) {
28
return new Response('You are not allowed to preview this page', { status: 403 })
29
}
30
31
if (!path || !collection || !slug) {
32
return new Response('Insufficient search params', { status: 404 })
33
}
34
35
if (!path.startsWith('/')) {
36
return new Response('This endpoint can only be used for relative previews', { status: 500 })
37
}
38
39
let user
40
41
try {
42
user = await payload.auth({
43
req: req as unknown as PayloadRequest,
44
headers: req.headers,
45
})
46
} catch (error) {
47
payload.logger.error({ err: error }, 'Error verifying token for live preview')
48
return new Response('You are not allowed to preview this page', { status: 403 })
49
}
50
51
const draft = await draftMode()
52
53
if (!user) {
54
draft.disable()
55
return new Response('You are not allowed to preview this page', { status: 403 })
56
}
57
58
// You can add additional checks here to see if the user is allowed to preview this page
59
60
draft.enable()
61
62
redirect(path)
63
}

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

1
export default async function Page({ params: paramsPromise }) {
2
const { slug = 'home' } = await paramsPromise
3
4
const { isEnabled: isDraftMode } = await draftMode()
5
6
const payload = await getPayload({ config })
7
8
const page = await payload.find({
9
collection: 'pages',
10
depth: 0,
11
draft: isDraftMode,
12
limit: 1,
13
overrideAccess: isDraftMode,
14
where: {
15
slug: {
16
equals: slug,
17
},
18
},
19
})?.then(({ docs }) => docs?.[0])
20
21
if (page === null) {
22
return notFound()
23
}
24
25
return (
26
<main>
27
<h1>{page?.title}</h1>
28
</main>
29
)
30
}
Next

Managing User Preferences