Skip to main content

Type Alias: PreviewHandlerOptions

PreviewHandlerOptions = object

Defined in: packages/next/src/handlers/previewHandler.ts:16

The options supported by previewHandler

Properties​

getRedirectPath()?​

optional getRedirectPath: (defaultRedirectPath, post, postTypeDef) => string

Defined in: packages/next/src/handlers/previewHandler.ts:66

If passed will override the default redirect path

Important: You should not need to override this but if you do, you must append -preview=true to the end of the redirecte path.

Parameters​

defaultRedirectPath​

string

the default redirect path

post​

any

PostEntity

postTypeDef​

CustomPostType

The object describing a post type

Returns​

string

the new redirect path


onRedirect()?​

optional onRedirect: (req, res, previewData, defaultRedirect?, redirectpath?) => void

Defined in: packages/next/src/handlers/previewHandler.ts:47

If passed will override the behavior of redirecting to the previewed post. We recommend implementing getRedirectPath instead. If you absolutely need to implement a custom redirect handler, we also suggest you implement getRedirectPath so that the preview cookie only applies to the specific path.

If set you should handle the redirect yourself by calling res.redirect.

Important: You should not need to override this but if you do, you must append -preview=true to the end of the redirected url.

This can be used to customize the preview url.

// pages/api/preview.js
import { previewHandler } from '@headstartwp/next';

export default async function handler(req, res) {
return previewHandler(req, res, {
onRedirect(req, res, previewData) => {
res.redirect(`/my-custom-preview-route/${previewData.id}-preview=true`);
}
});
}

Parameters​

req​

NextApiRequest

The NextApiRequest object

res​

NextApiResponse

The NextApiResponse object

previewData​

PreviewData

The previewData object

defaultRedirect?​

PreviewHandlerOptions["onRedirect"]

The default redirect function

redirectpath?​

string

Returns​

void


preparePreviewData()?​

optional preparePreviewData: (req, res, post, previewData) => PreviewData

Defined in: packages/next/src/handlers/previewHandler.ts:79

If passed, this function will be called when the preview data is fetched and allows for additional preview data to be set.

The preview data is then passed to res.setPreviewData. Be mindful of limitations of this function. You should not pass full post objects, just ids and other data that can be used to fetch the post.

Parameters​

req​

NextApiRequest

res​

NextApiResponse

post​

PostEntity

previewData​

PreviewData

Returns​

PreviewData