With these snippets you’ll be set up to dynamic enable preview mode for any content in your Next.js projects.
next
module with a preview.ts
file in my common
directory.
?slug=xx
, otherwise just print that the preview mode has been disabled. You can customise that default case, going to the homepage is probably a good idea.
enablePreview
is a function that returns a function. We pass in the method to confirm the slugs exist and a redirectSlugFormatter
, this will be your chance to format the slug. For example, if someone wants /article/one
. Your CMS may only return the slug of one
, you can then pass in a method that appends the /article
to the slug before the user is redirected.
The endpoint will need to be passed 2 query strings.
secret
- to confirm only people you want are requesting the preview. This is stored in the DATO_PREVIEW_SECRET
environment variable.slug
- the page slug that you want to previewpages/api/disable-preview.ts
- disables the previewpages/api/preview.ts
- enables the previewpages/api/disable-preview.ts
pages/api/preview.ts
pages/api/preview/article.ts
getStaticProps
etc.