Preview Mode

Using preview mode with DrupalClient.


To implement preview mode:

  1. Configure preview mode for entity types on Drupal. See the guide here.
  2. Set a previewSecret on the DrupalClient.

lib/drupal.ts

export const drupal = new DrupalClient(
process.env.NEXT_PUBLIC_DRUPAL_BASE_URL,
{
previewSecret: process.env.DRUPAL_PREVIEW_SECRET,
}
)
  1. Implement /pages/api/preview.ts.

pages/api/preview.ts

import { drupal } from "lib/drupal"
export default async function handler(req, res) {
return await drupal.preview(req, res)
}
  1. Implement /pages/api/exit-preview.ts

pages/api/exit-preview.ts

export default function exit(_, res) {
res.clearPreviewData()
res.writeHead(307, { Location: "/" })
res.end()
}