Skip to content

Commit

Permalink
templates: conditionally render the live preview listener component (p…
Browse files Browse the repository at this point in the history
…ayloadcms#9973)

Conditionally render the live preview listener component so that we
don't make unnecessary requests to the API without draft mode being
enabled.
  • Loading branch information
paulpopus authored Dec 13, 2024
1 parent 4dc5003 commit 050ff84
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion templates/website/src/app/(frontend)/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Args = {
}

export default async function Page({ params: paramsPromise }: Args) {
const { isEnabled: draft } = await draftMode()
const { slug = 'home' } = await paramsPromise
const url = '/' + slug

Expand All @@ -72,7 +73,7 @@ export default async function Page({ params: paramsPromise }: Args) {
{/* Allows redirects for valid pages too */}
<PayloadRedirects disableNotFound url={url} />

<LivePreviewListener />
{draft && <LivePreviewListener />}

<RenderHero {...hero} />
<RenderBlocks blocks={layout} />
Expand Down
3 changes: 2 additions & 1 deletion templates/website/src/app/(frontend)/posts/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Args = {
}

export default async function Post({ params: paramsPromise }: Args) {
const { isEnabled: draft } = await draftMode()
const { slug = '' } = await paramsPromise
const url = '/posts/' + slug
const post = await queryPostBySlug({ slug })
Expand All @@ -55,7 +56,7 @@ export default async function Post({ params: paramsPromise }: Args) {
{/* Allows redirects for valid pages too */}
<PayloadRedirects disableNotFound url={url} />

<LivePreviewListener />
{draft && <LivePreviewListener />}

<PostHero post={post} />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Args = {
}

export default async function Page({ params: paramsPromise }: Args) {
const { isEnabled: draft } = await draftMode()
const { slug = 'home' } = await paramsPromise
const url = '/' + slug

Expand All @@ -72,7 +73,7 @@ export default async function Page({ params: paramsPromise }: Args) {
{/* Allows redirects for valid pages too */}
<PayloadRedirects disableNotFound url={url} />

<LivePreviewListener />
{draft && <LivePreviewListener />}

<RenderHero {...hero} />
<RenderBlocks blocks={layout} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Args = {
}

export default async function Post({ params: paramsPromise }: Args) {
const { isEnabled: draft } = await draftMode()
const { slug = '' } = await paramsPromise
const url = '/posts/' + slug
const post = await queryPostBySlug({ slug })
Expand All @@ -55,7 +56,7 @@ export default async function Post({ params: paramsPromise }: Args) {
{/* Allows redirects for valid pages too */}
<PayloadRedirects disableNotFound url={url} />

<LivePreviewListener />
{draft && <LivePreviewListener />}

<PostHero post={post} />

Expand Down

0 comments on commit 050ff84

Please sign in to comment.