Next.js Discord

Discord Forum

Use draft mode for static pages?

Unanswered
Masai Lion posted this in #help-forum
Open in Discord
Masai LionOP
I am making a site where I implemented payload cms' draft functionality and I want to implement a preview page for content that will be statically generated by ISR so I implemented a draft route

export async function GET(request: Request) {
  // Parse query string parameters
  const { searchParams } = new URL(request.url)
  const secret = searchParams.get('secret')
  const path = searchParams.get('path')

  if (secret !== process.env.DRAFT_MODE_SECRET || !path)
    return new Response('Invalid params', { status: 401 })

  const draft = await draftMode()

  draft.enable()

  redirect(path)
}


and in my page I do

  const { isEnabled: draft } = await draftMode()

  const project: Project | null = await payload.findByID({
    collection: 'project',
    id,
    locale,
    depth: 2,
    draft,
    overrideAccess: draft,
    disableErrors: true, // Return null instead of throwing an error if the project doesn't exist
  })

  if (!project) notFound()

  // Render page


But since draftMode() is a headers function it forces the page to use dynamic rendering. Most users accessing the site will not need this so I'd like to implement static rendering and only use dynamic rendering when an admin access the page. Any way to statically serve the generated page when no draft cookie is found and dynamically render it otherwise?

0 Replies