UNPKG

@prismicio/next

Version:

Helpers to integrate Prismic into Next.js apps

1 lines 2.72 kB
{"version":3,"file":"redirectToPreviewURL.cjs","sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import type { LinkResolverFunction, Client } from \"@prismicio/client\";\n\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\";\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\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, \"resolvePreviewURL\">;\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/pages/building-your-application/routing/api-routes\\>\n\t */\n\treq: NextApiRequestLike;\n\n\t/**\n\t * The `res` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\\>\n\t */\n\tres: NextApiResponseLike;\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction;\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed\n\t * document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the\n\t * `defaultURL` option must _not_ include it. Instead, provide the `basePath`\n\t * property using the `basePath` option.\n\t */\n\tdefaultURL?: string;\n\n\t/**\n\t * The `basePath` for the Next.js app as it is defined in `next.config.js`.\n\t * This option can be omitted if the app does not have a `basePath`.\n\t *\n\t * @remarks\n\t * The Router Handler or API route is unable to detect the app's `basePath`\n\t * automatically. It must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string;\n};\n\nexport async function redirectToPreviewURL(\n\tconfig: RedirectToPreviewURLConfig,\n): Promise<void> {\n\tconst {\n\t\tclient,\n\t\treq,\n\t\tres,\n\t\tlinkResolver,\n\t\tdefaultURL = \"/\",\n\t\tbasePath = \"\",\n\t} = config;\n\n\tconst previewToken = req.query.token?.toString();\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tlinkResolver,\n\t\tdefaultURL,\n\t\tpreviewToken,\n\t});\n\n\tres.redirect(basePath + previewURL);\n\n\treturn;\n}\n"],"names":[],"mappings":";;AAqDA,eAAsB,qBACrB,QAAkC;;AAE5B,QAAA,EACL,QACA,KACA,KACA,cACA,aAAa,KACb,WAAW,GAAA,IACR;AAEJ,QAAM,gBAAe,SAAI,MAAM,UAAV,mBAAiB;AAEhC,QAAA,aAAa,MAAM,OAAO,kBAAkB;AAAA,IACjD;AAAA,IACA;AAAA,IACA;AAAA,EAAA,CACA;AAEG,MAAA,SAAS,WAAW,UAAU;AAElC;AACD;;"}