UNPKG

@prismicio/next

Version:

Helpers to integrate Prismic into Next.js apps

1 lines 2.41 kB
{"version":3,"file":"enableAutoPreviews.cjs","sources":["../../src/pages/enableAutoPreviews.ts"],"sourcesContent":["import type { PreviewData } from \"next\";\nimport type { Client } from \"@prismicio/client\";\n\nimport type { NextApiRequestLike } from \"./types\";\n\n/**\n * Configuration for `enableAutoPreviews`.\n *\n * @typeParam TPreviewData - Next.js preview data object.\n */\nexport type EnableAutoPreviewsConfig = {\n\t/** Prismic client with which automatic previews will be enabled. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"queryContentFromRef\" | \"enableAutoPreviewsFromReq\">;\n\n\t/**\n\t * The `previewData` object provided in the `getStaticProps()` or\n\t * `getServerSideProps()` context object.\n\t */\n\tpreviewData?: PreviewData;\n\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\treq?: NextApiRequestLike;\n};\n\n/**\n * Configures a Prismic client to automatically query draft content during a\n * preview session. It either takes in a Next.js `getStaticProps` context object\n * or a Next.js API endpoint request object.\n *\n * @param config - Configuration for the function.\n */\nexport function enableAutoPreviews(config: EnableAutoPreviewsConfig): void {\n\tif (\"previewData\" in config && config.previewData) {\n\t\t// Assume we are in `getStaticProps()` or\n\t\t// `getServerSideProps()` with active Preview Mode.\n\n\t\tif (isPrismicPreviewData(config.previewData)) {\n\t\t\tconfig.client.queryContentFromRef(config.previewData.ref);\n\t\t}\n\n\t\treturn;\n\t}\n\n\tif (\"req\" in config && config.req) {\n\t\t// Assume we are in an API Route.\n\n\t\tconfig.client.enableAutoPreviewsFromReq(config.req);\n\n\t\treturn;\n\t}\n}\n\nfunction isPrismicPreviewData(input: unknown): input is { ref: string } {\n\treturn typeof input === \"object\" && input !== null && \"ref\" in input;\n}\n"],"names":[],"mappings":";;AAsCM,SAAU,mBAAmB,QAAgC;AAC9D,MAAA,iBAAiB,UAAU,OAAO,aAAa;AAI9C,QAAA,qBAAqB,OAAO,WAAW,GAAG;AAC7C,aAAO,OAAO,oBAAoB,OAAO,YAAY,GAAG;AAAA,IAAA;AAGzD;AAAA,EAAA;AAGG,MAAA,SAAS,UAAU,OAAO,KAAK;AAG3B,WAAA,OAAO,0BAA0B,OAAO,GAAG;AAElD;AAAA,EAAA;AAEF;AAEA,SAAS,qBAAqB,OAAc;AAC3C,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,SAAS;AAChE;;"}