@prismicio/next 2.3.4-pr.174.890bc09 → 2.3.4-pr.175.5f9614e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/dist/PrismicPreview.cjs +5 -19
  2. package/dist/PrismicPreview.cjs.map +1 -1
  3. package/dist/PrismicPreview.d.cts.map +1 -1
  4. package/dist/PrismicPreview.d.ts.map +1 -1
  5. package/dist/PrismicPreview.js +5 -17
  6. package/dist/PrismicPreview.js.map +1 -1
  7. package/dist/PrismicPreviewClient.cjs +11 -1
  8. package/dist/PrismicPreviewClient.cjs.map +1 -1
  9. package/dist/PrismicPreviewClient.js +9 -1
  10. package/dist/PrismicPreviewClient.js.map +1 -1
  11. package/dist/exitPreview.cjs +6 -0
  12. package/dist/exitPreview.cjs.map +1 -1
  13. package/dist/exitPreview.d.cts.map +1 -1
  14. package/dist/exitPreview.d.ts.map +1 -1
  15. package/dist/exitPreview.js +6 -0
  16. package/dist/exitPreview.js.map +1 -1
  17. package/dist/lib/toolbarV5.cjs +37 -0
  18. package/dist/lib/toolbarV5.cjs.map +1 -0
  19. package/dist/lib/toolbarV5.js +34 -0
  20. package/dist/lib/toolbarV5.js.map +1 -0
  21. package/dist/package.cjs +1 -1
  22. package/dist/package.js +1 -1
  23. package/dist/pages/PrismicPreview.cjs +2 -2
  24. package/dist/pages/PrismicPreview.cjs.map +1 -1
  25. package/dist/pages/PrismicPreview.d.cts.map +1 -1
  26. package/dist/pages/PrismicPreview.d.ts.map +1 -1
  27. package/dist/pages/PrismicPreview.js +3 -3
  28. package/dist/pages/PrismicPreview.js.map +1 -1
  29. package/dist/pages/appendCookie.cjs +12 -0
  30. package/dist/pages/appendCookie.cjs.map +1 -0
  31. package/dist/pages/appendCookie.js +12 -0
  32. package/dist/pages/appendCookie.js.map +1 -0
  33. package/dist/pages/exitPreview.cjs +3 -0
  34. package/dist/pages/exitPreview.cjs.map +1 -1
  35. package/dist/pages/exitPreview.d.cts.map +1 -1
  36. package/dist/pages/exitPreview.d.ts.map +1 -1
  37. package/dist/pages/exitPreview.js +3 -0
  38. package/dist/pages/exitPreview.js.map +1 -1
  39. package/dist/pages/redirectToPreviewURL.cjs +18 -1
  40. package/dist/pages/redirectToPreviewURL.cjs.map +1 -1
  41. package/dist/pages/redirectToPreviewURL.d.cts +1 -1
  42. package/dist/pages/redirectToPreviewURL.d.cts.map +1 -1
  43. package/dist/pages/redirectToPreviewURL.d.ts +1 -1
  44. package/dist/pages/redirectToPreviewURL.d.ts.map +1 -1
  45. package/dist/pages/redirectToPreviewURL.js +18 -1
  46. package/dist/pages/redirectToPreviewURL.js.map +1 -1
  47. package/dist/pages/types.d.cts +1 -0
  48. package/dist/pages/types.d.cts.map +1 -1
  49. package/dist/pages/types.d.ts +1 -0
  50. package/dist/pages/types.d.ts.map +1 -1
  51. package/dist/redirectToPreviewURL.cjs +13 -3
  52. package/dist/redirectToPreviewURL.cjs.map +1 -1
  53. package/dist/redirectToPreviewURL.d.cts +1 -1
  54. package/dist/redirectToPreviewURL.d.cts.map +1 -1
  55. package/dist/redirectToPreviewURL.d.ts +1 -1
  56. package/dist/redirectToPreviewURL.d.ts.map +1 -1
  57. package/dist/redirectToPreviewURL.js +13 -3
  58. package/dist/redirectToPreviewURL.js.map +1 -1
  59. package/package.json +8 -8
  60. package/src/PrismicPreview.tsx +0 -7
  61. package/src/PrismicPreviewClient.tsx +12 -1
  62. package/src/exitPreview.ts +3 -0
  63. package/src/lib/toolbarV5.ts +54 -0
  64. package/src/pages/PrismicPreview.tsx +7 -4
  65. package/src/pages/appendCookie.ts +16 -0
  66. package/src/pages/exitPreview.ts +3 -0
  67. package/src/pages/redirectToPreviewURL.ts +24 -3
  68. package/src/pages/types.ts +1 -0
  69. package/src/redirectToPreviewURL.ts +16 -10
@@ -1 +1 @@
1
- {"version":3,"file":"exitPreview.cjs","names":[],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\n/** Configuration for `exitPreview()`. */\nexport type ExitPreviewAPIRouteConfig = {\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\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/api-routes/introduction\\>\n\t */\n\tres: NextApiResponseLike\n}\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in an API\n * route.\n *\n * @example\n * \t;```typescript\n * \t// src/pages/api/exit-preview.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport default function handler(_req, res) {\n * \t\treturn exitPreview({ res })\n * \t}\n * \t```\n */\nexport function exitPreview(config: ExitPreviewAPIRouteConfig): void {\n\tconfig.res.clearPreviewData()\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\tconfig.res.setHeader(\"Cache-Control\", \"no-store\")\n\n\tconfig.res.json({ success: true })\n\n\treturn\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAkCA,SAAgB,YAAY,QAAyC;CACpE,OAAO,IAAI,iBAAiB;CAG5B,OAAO,IAAI,UAAU,iBAAiB,UAAU;CAEhD,OAAO,IAAI,KAAK,EAAE,SAAS,KAAK,CAAC;AAGlC"}
1
+ {"version":3,"file":"exitPreview.cjs","names":["previewSessionCookie"],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import { previewSessionCookie } from \"../lib/toolbarV5\"\nimport { appendCookie } from \"./appendCookie\"\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\n/** Configuration for `exitPreview()`. */\nexport type ExitPreviewAPIRouteConfig = {\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\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/api-routes/introduction\\>\n\t */\n\tres: NextApiResponseLike\n}\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in an API\n * route.\n *\n * @example\n * \t;```typescript\n * \t// src/pages/api/exit-preview.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport default function handler(_req, res) {\n * \t\treturn exitPreview({ res })\n * \t}\n * \t```\n */\nexport function exitPreview(config: ExitPreviewAPIRouteConfig): void {\n\tconfig.res.clearPreviewData()\n\tappendCookie(config.res, previewSessionCookie, \"\", new Date(0))\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\tconfig.res.setHeader(\"Cache-Control\", \"no-store\")\n\n\tconfig.res.json({ success: true })\n\n\treturn\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoCA,SAAgB,YAAY,QAAyC;CACpE,OAAO,IAAI,iBAAiB;CAC5B,qBAAA,aAAa,OAAO,KAAKA,kBAAAA,sBAAsB,oBAAI,IAAI,KAAK,CAAC,CAAC;CAG9D,OAAO,IAAI,UAAU,iBAAiB,UAAU;CAEhD,OAAO,IAAI,KAAK,EAAE,SAAS,KAAK,CAAC;AAGlC"}
@@ -1 +1 @@
1
- {"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;YAGY;;;;;;EAMX,MAAM;;;;;;EAON,KAAK;;;;;;;;;;;;;;;;;wBAkBU,YAAY,QAAQ"}
1
+ {"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;YAKY;;;;;;EAMX,MAAM;;;;;;EAON,KAAK;;;;;;;;;;;;;;;;;wBAkBU,YAAY,QAAQ"}
@@ -1 +1 @@
1
- {"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;YAGY;;;;;;EAMX,MAAM;;;;;;EAON,KAAK;;;;;;;;;;;;;;;;;wBAkBU,YAAY,QAAQ"}
1
+ {"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;YAKY;;;;;;EAMX,MAAM;;;;;;EAON,KAAK;;;;;;;;;;;;;;;;;wBAkBU,YAAY,QAAQ"}
@@ -1,3 +1,5 @@
1
+ import { previewSessionCookie } from "../lib/toolbarV5.js";
2
+ import { appendCookie } from "./appendCookie.js";
1
3
  //#region src/pages/exitPreview.ts
2
4
  /**
3
5
  * Ends a Prismic preview session within a Next.js app. This function should be used in an API
@@ -16,6 +18,7 @@
16
18
  */
17
19
  function exitPreview(config) {
18
20
  config.res.clearPreviewData();
21
+ appendCookie(config.res, previewSessionCookie, "", /* @__PURE__ */ new Date(0));
19
22
  config.res.setHeader("Cache-Control", "no-store");
20
23
  config.res.json({ success: true });
21
24
  }
@@ -1 +1 @@
1
- {"version":3,"file":"exitPreview.js","names":[],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\n/** Configuration for `exitPreview()`. */\nexport type ExitPreviewAPIRouteConfig = {\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\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/api-routes/introduction\\>\n\t */\n\tres: NextApiResponseLike\n}\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in an API\n * route.\n *\n * @example\n * \t;```typescript\n * \t// src/pages/api/exit-preview.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport default function handler(_req, res) {\n * \t\treturn exitPreview({ res })\n * \t}\n * \t```\n */\nexport function exitPreview(config: ExitPreviewAPIRouteConfig): void {\n\tconfig.res.clearPreviewData()\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\tconfig.res.setHeader(\"Cache-Control\", \"no-store\")\n\n\tconfig.res.json({ success: true })\n\n\treturn\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAkCA,SAAgB,YAAY,QAAyC;CACpE,OAAO,IAAI,iBAAiB;CAG5B,OAAO,IAAI,UAAU,iBAAiB,UAAU;CAEhD,OAAO,IAAI,KAAK,EAAE,SAAS,KAAK,CAAC;AAGlC"}
1
+ {"version":3,"file":"exitPreview.js","names":[],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import { previewSessionCookie } from \"../lib/toolbarV5\"\nimport { appendCookie } from \"./appendCookie\"\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\n/** Configuration for `exitPreview()`. */\nexport type ExitPreviewAPIRouteConfig = {\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\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/api-routes/introduction\\>\n\t */\n\tres: NextApiResponseLike\n}\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in an API\n * route.\n *\n * @example\n * \t;```typescript\n * \t// src/pages/api/exit-preview.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport default function handler(_req, res) {\n * \t\treturn exitPreview({ res })\n * \t}\n * \t```\n */\nexport function exitPreview(config: ExitPreviewAPIRouteConfig): void {\n\tconfig.res.clearPreviewData()\n\tappendCookie(config.res, previewSessionCookie, \"\", new Date(0))\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\tconfig.res.setHeader(\"Cache-Control\", \"no-store\")\n\n\tconfig.res.json({ success: true })\n\n\treturn\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoCA,SAAgB,YAAY,QAAyC;CACpE,OAAO,IAAI,iBAAiB;CAC5B,aAAa,OAAO,KAAK,sBAAsB,oBAAI,IAAI,KAAK,CAAC,CAAC;CAG9D,OAAO,IAAI,UAAU,iBAAiB,UAAU;CAEhD,OAAO,IAAI,KAAK,EAAE,SAAS,KAAK,CAAC;AAGlC"}
@@ -1,7 +1,24 @@
1
+ const require_toolbarV5 = require("../lib/toolbarV5.cjs");
2
+ const require_appendCookie = require("./appendCookie.cjs");
3
+ let _prismicio_client = require("@prismicio/client");
1
4
  //#region src/pages/redirectToPreviewURL.ts
2
5
  async function redirectToPreviewURL(config) {
3
6
  const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
4
- const previewToken = req.query.token?.toString();
7
+ let previewToken = req.query.token?.toString();
8
+ const session = req.query.session?.toString();
9
+ if (session) {
10
+ if (!previewToken) {
11
+ previewToken = await require_toolbarV5.getPreviewSessionRef(client, session);
12
+ res.setPreviewData({ ref: previewToken });
13
+ }
14
+ require_appendCookie.appendCookie(res, _prismicio_client.cookie.preview, previewToken);
15
+ require_appendCookie.appendCookie(res, require_toolbarV5.previewSessionCookie, session);
16
+ const path = require_toolbarV5.getSafePath(req.query.path?.toString());
17
+ if (path) {
18
+ res.redirect(path);
19
+ return;
20
+ }
21
+ }
5
22
  const previewURL = await client.resolvePreviewURL({
6
23
  linkResolver,
7
24
  defaultURL,
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.cjs","names":[],"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 document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` 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`. This option can be\n\t * 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` automatically. It\n\t * must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {\n\tconst { client, req, res, linkResolver, defaultURL = \"/\", basePath = \"\" } = 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"],"mappings":";AAmDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,MAAM,eAAe,IAAI,MAAM,OAAO,SAAS;CAE/C,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
1
+ {"version":3,"file":"redirectToPreviewURL.cjs","names":["getPreviewSessionRef","prismicCookie","previewSessionCookie","getSafePath"],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type LinkResolverFunction, type Client } from \"@prismicio/client\"\n\nimport { getPreviewSessionRef, getSafePath, previewSessionCookie } from \"../lib/toolbarV5\"\nimport { appendCookie } from \"./appendCookie\"\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\" | \"documentAPIEndpoint\">\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 document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` 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`. This option can be\n\t * 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` automatically. It\n\t * must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {\n\tconst { client, req, res, linkResolver, defaultURL = \"/\", basePath = \"\" } = config\n\n\tlet previewToken = req.query.token?.toString()\n\n\t// The toolbar's v5 flow follows the session in the preview cookies. Share\n\t// links only carry the session, so `setPreviewData()` has no ref for them.\n\tconst session = req.query.session?.toString()\n\tif (session) {\n\t\tif (!previewToken) {\n\t\t\tpreviewToken = await getPreviewSessionRef(client, session)\n\t\t\tres.setPreviewData({ ref: previewToken })\n\t\t}\n\t\tappendCookie(res, prismicCookie.preview, previewToken)\n\t\tappendCookie(res, previewSessionCookie, session)\n\n\t\tconst path = getSafePath(req.query.path?.toString())\n\t\tif (path) {\n\t\t\tres.redirect(path)\n\n\t\t\treturn\n\t\t}\n\t}\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"],"mappings":";;;;AAqDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,IAAI,eAAe,IAAI,MAAM,OAAO,SAAS;CAI7C,MAAM,UAAU,IAAI,MAAM,SAAS,SAAS;CAC5C,IAAI,SAAS;EACZ,IAAI,CAAC,cAAc;GAClB,eAAe,MAAMA,kBAAAA,qBAAqB,QAAQ,OAAO;GACzD,IAAI,eAAe,EAAE,KAAK,aAAa,CAAC;EACzC;EACA,qBAAA,aAAa,KAAKC,kBAAAA,OAAc,SAAS,YAAY;EACrD,qBAAA,aAAa,KAAKC,kBAAAA,sBAAsB,OAAO;EAE/C,MAAM,OAAOC,kBAAAA,YAAY,IAAI,MAAM,MAAM,SAAS,CAAC;EACnD,IAAI,MAAM;GACT,IAAI,SAAS,IAAI;GAEjB;EACD;CACD;CAEA,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
@@ -3,7 +3,7 @@ import { Client, LinkResolverFunction } from "@prismicio/client";
3
3
  //#region src/pages/redirectToPreviewURL.d.ts
4
4
  export type RedirectToPreviewURLConfig = {
5
5
  /** The Prismic client configured for the preview session's repository. */
6
- client: Pick<Client, "resolvePreviewURL">;
6
+ client: Pick<Client, "resolvePreviewURL" | "documentAPIEndpoint">;
7
7
  /**
8
8
  * The `req` object from a Next.js API route.
9
9
  *
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.d.cts","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"mappings":";;;YAIY;;EAKX,QAAQ,KAAK;;;;;;EAOb,KAAK;;;;;;EAOL,KAAK;;;;;;EAOL,eAAe;;;;;;;EAQf;;;;;;;;;EAUA;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
1
+ {"version":3,"file":"redirectToPreviewURL.d.cts","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"mappings":";;;YAMY;;EAKX,QAAQ,KAAK;;;;;;EAOb,KAAK;;;;;;EAOL,KAAK;;;;;;EAOL,eAAe;;;;;;;EAQf;;;;;;;;;EAUA;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
@@ -3,7 +3,7 @@ import { Client, LinkResolverFunction } from "@prismicio/client";
3
3
  //#region src/pages/redirectToPreviewURL.d.ts
4
4
  export type RedirectToPreviewURLConfig = {
5
5
  /** The Prismic client configured for the preview session's repository. */
6
- client: Pick<Client, "resolvePreviewURL">;
6
+ client: Pick<Client, "resolvePreviewURL" | "documentAPIEndpoint">;
7
7
  /**
8
8
  * The `req` object from a Next.js API route.
9
9
  *
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.d.ts","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"mappings":";;;YAIY;;EAKX,QAAQ,KAAK;;;;;;EAOb,KAAK;;;;;;EAOL,KAAK;;;;;;EAOL,eAAe;;;;;;;EAQf;;;;;;;;;EAUA;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
1
+ {"version":3,"file":"redirectToPreviewURL.d.ts","names":[],"sources":["../../src/pages/redirectToPreviewURL.ts"],"mappings":";;;YAMY;;EAKX,QAAQ,KAAK;;;;;;EAOb,KAAK;;;;;;EAOL,KAAK;;;;;;EAOL,eAAe;;;;;;;EAQf;;;;;;;;;EAUA;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
@@ -1,7 +1,24 @@
1
+ import { getPreviewSessionRef, getSafePath, previewSessionCookie } from "../lib/toolbarV5.js";
2
+ import { appendCookie } from "./appendCookie.js";
3
+ import { cookie } from "@prismicio/client";
1
4
  //#region src/pages/redirectToPreviewURL.ts
2
5
  async function redirectToPreviewURL(config) {
3
6
  const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
4
- const previewToken = req.query.token?.toString();
7
+ let previewToken = req.query.token?.toString();
8
+ const session = req.query.session?.toString();
9
+ if (session) {
10
+ if (!previewToken) {
11
+ previewToken = await getPreviewSessionRef(client, session);
12
+ res.setPreviewData({ ref: previewToken });
13
+ }
14
+ appendCookie(res, cookie.preview, previewToken);
15
+ appendCookie(res, previewSessionCookie, session);
16
+ const path = getSafePath(req.query.path?.toString());
17
+ if (path) {
18
+ res.redirect(path);
19
+ return;
20
+ }
21
+ }
5
22
  const previewURL = await client.resolvePreviewURL({
6
23
  linkResolver,
7
24
  defaultURL,
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.js","names":[],"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 document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` 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`. This option can be\n\t * 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` automatically. It\n\t * must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {\n\tconst { client, req, res, linkResolver, defaultURL = \"/\", basePath = \"\" } = 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"],"mappings":";AAmDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,MAAM,eAAe,IAAI,MAAM,OAAO,SAAS;CAE/C,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
1
+ {"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type LinkResolverFunction, type Client } from \"@prismicio/client\"\n\nimport { getPreviewSessionRef, getSafePath, previewSessionCookie } from \"../lib/toolbarV5\"\nimport { appendCookie } from \"./appendCookie\"\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\" | \"documentAPIEndpoint\">\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 document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` 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`. This option can be\n\t * 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` automatically. It\n\t * must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {\n\tconst { client, req, res, linkResolver, defaultURL = \"/\", basePath = \"\" } = config\n\n\tlet previewToken = req.query.token?.toString()\n\n\t// The toolbar's v5 flow follows the session in the preview cookies. Share\n\t// links only carry the session, so `setPreviewData()` has no ref for them.\n\tconst session = req.query.session?.toString()\n\tif (session) {\n\t\tif (!previewToken) {\n\t\t\tpreviewToken = await getPreviewSessionRef(client, session)\n\t\t\tres.setPreviewData({ ref: previewToken })\n\t\t}\n\t\tappendCookie(res, prismicCookie.preview, previewToken)\n\t\tappendCookie(res, previewSessionCookie, session)\n\n\t\tconst path = getSafePath(req.query.path?.toString())\n\t\tif (path) {\n\t\t\tres.redirect(path)\n\n\t\t\treturn\n\t\t}\n\t}\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"],"mappings":";;;;AAqDA,eAAsB,qBAAqB,QAAmD;CAC7F,MAAM,EAAE,QAAQ,KAAK,KAAK,cAAc,aAAa,KAAK,WAAW,OAAO;CAE5E,IAAI,eAAe,IAAI,MAAM,OAAO,SAAS;CAI7C,MAAM,UAAU,IAAI,MAAM,SAAS,SAAS;CAC5C,IAAI,SAAS;EACZ,IAAI,CAAC,cAAc;GAClB,eAAe,MAAM,qBAAqB,QAAQ,OAAO;GACzD,IAAI,eAAe,EAAE,KAAK,aAAa,CAAC;EACzC;EACA,aAAa,KAAKA,OAAc,SAAS,YAAY;EACrD,aAAa,KAAK,sBAAsB,OAAO;EAE/C,MAAM,OAAO,YAAY,IAAI,MAAM,MAAM,SAAS,CAAC;EACnD,IAAI,MAAM;GACT,IAAI,SAAS,IAAI;GAEjB;EACD;CACD;CAEA,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;CACD,CAAC;CAED,IAAI,SAAS,WAAW,UAAU;AAGnC"}
@@ -20,6 +20,7 @@ export type NextApiRequestLike = {
20
20
  export type NextApiResponseLike = {
21
21
  redirect(url: string): NextApiResponseLike;
22
22
  clearPreviewData(): NextApiResponseLike;
23
+ getHeader(name: string): number | string | string[] | undefined;
23
24
  setHeader(name: string, value: string | string[]): NextApiResponseLike;
24
25
  json(body: any): void;
25
26
  setPreviewData(data: object | string): NextApiResponseLike;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.cts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;YAKY,qBAAqB,eAChC,KAAK;;;;;;YAOM;EACX,OAAO,QAAQ;EACf,SAAS,QAAQ;;;;;;;YAQN;EACX,SAAS,cAAc;EACvB,oBAAoB;EACpB,UAAU,cAAc,2BAA2B;EAEnD,KAAK;EACL,eAAe,wBAAwB"}
1
+ {"version":3,"file":"types.d.cts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;YAKY,qBAAqB,eAChC,KAAK;;;;;;YAOM;EACX,OAAO,QAAQ;EACf,SAAS,QAAQ;;;;;;;YAQN;EACX,SAAS,cAAc;EACvB,oBAAoB;EACpB,UAAU;EACV,UAAU,cAAc,2BAA2B;EAEnD,KAAK;EACL,eAAe,wBAAwB"}
@@ -20,6 +20,7 @@ export type NextApiRequestLike = {
20
20
  export type NextApiResponseLike = {
21
21
  redirect(url: string): NextApiResponseLike;
22
22
  clearPreviewData(): NextApiResponseLike;
23
+ getHeader(name: string): number | string | string[] | undefined;
23
24
  setHeader(name: string, value: string | string[]): NextApiResponseLike;
24
25
  json(body: any): void;
25
26
  setPreviewData(data: object | string): NextApiResponseLike;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;YAKY,qBAAqB,eAChC,KAAK;;;;;;YAOM;EACX,OAAO,QAAQ;EACf,SAAS,QAAQ;;;;;;;YAQN;EACX,SAAS,cAAc;EACvB,oBAAoB;EACpB,UAAU,cAAc,2BAA2B;EAEnD,KAAK;EACL,eAAe,wBAAwB"}
1
+ {"version":3,"file":"types.d.ts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;YAKY,qBAAqB,eAChC,KAAK;;;;;;YAOM;EACX,OAAO,QAAQ;EACf,SAAS,QAAQ;;;;;;;YAQN;EACX,SAAS,cAAc;EACvB,oBAAoB;EACpB,UAAU;EACV,UAAU,cAAc,2BAA2B;EAEnD,KAAK;EACL,eAAe,wBAAwB"}
@@ -1,17 +1,27 @@
1
+ const require_toolbarV5 = require("./lib/toolbarV5.cjs");
1
2
  let _prismicio_client = require("@prismicio/client");
2
3
  //#region src/redirectToPreviewURL.ts
3
4
  async function redirectToPreviewURL(config) {
4
5
  const { client, request, linkResolver, defaultURL = "/" } = config;
5
6
  const { cookies, draftMode } = await import("next/headers.js");
6
7
  const { redirect } = await import("next/navigation");
7
- const documentID = request.nextUrl.searchParams.get("documentId") ?? void 0;
8
+ const { searchParams } = request.nextUrl;
9
+ const documentID = searchParams.get("documentId") ?? void 0;
10
+ let previewToken = searchParams.get("token") ?? void 0;
8
11
  const cookieJar = await cookies();
9
- const previewToken = request.nextUrl.searchParams.get("token") ?? void 0;
12
+ const session = searchParams.get("session");
13
+ if (session) {
14
+ previewToken ??= await require_toolbarV5.getPreviewSessionRef(client, session);
15
+ cookieJar.set(require_toolbarV5.previewSessionCookie, session, {
16
+ sameSite: "none",
17
+ secure: true
18
+ });
19
+ }
10
20
  if (previewToken) cookieJar.set(_prismicio_client.cookie.preview, previewToken, {
11
21
  sameSite: "none",
12
22
  secure: true
13
23
  });
14
- const previewURL = await client.resolvePreviewURL({
24
+ const previewURL = session && require_toolbarV5.getSafePath(searchParams.get("path")) || await client.resolvePreviewURL({
15
25
  documentID,
16
26
  previewToken,
17
27
  defaultURL,
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.cjs","names":["prismicCookie"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport type { NextRequestLike } 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 `request` object from a Next.js Route Handler.\n\t *\n\t * @see Next.js Route Handler docs: \\<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\\>\n\t */\n\trequest: NextRequestLike\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 document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<never> {\n\tconst { client, request, linkResolver, defaultURL = \"/\" } = config\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies, draftMode } = await import(\"next/headers\")\n\n\t// Dynamic so Node.js can load this module: the bare specifier (kept by\n\t// tsdown.config.ts for Next.js's server alias) is not resolvable by Node.js.\n\tconst { redirect } = await import(\"next/navigation\")\n\n\tconst documentID = request.nextUrl.searchParams.get(\"documentId\") ?? undefined\n\n\t// Set the initial preview cookie. Setting the cookie here is necessary\n\t// to support unpublished previews. Without setting it here, the page\n\t// will try to render without the preview cookie, leading to a\n\t// PrismicNotFound error.\n\tconst cookieJar = await cookies()\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tcookieJar.set(prismicCookie.preview, previewToken, { sameSite: \"none\", secure: true })\n\t}\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tdocumentID,\n\t\tpreviewToken,\n\t\tdefaultURL,\n\t\tlinkResolver,\n\t})\n\n\t;(await draftMode()).enable()\n\n\t// Next.js uses `SameSite=Lax` in development, which cross-site iframes drop.\n\tcookieJar.set({\n\t\t...cookieJar.get(\"__prerender_bypass\")!,\n\t\thttpOnly: true,\n\t\tsameSite: \"none\",\n\t\tsecure: true,\n\t})\n\n\treturn redirect(previewURL)\n}\n"],"mappings":";;AAkCA,eAAsB,qBAAqB,QAAoD;CAC9F,MAAM,EAAE,QAAQ,SAAS,cAAc,aAAa,QAAQ;CAK5D,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;CAI5C,MAAM,EAAE,aAAa,MAAM,OAAO;CAElC,MAAM,aAAa,QAAQ,QAAQ,aAAa,IAAI,YAAY,KAAK,KAAA;CAMrE,MAAM,YAAY,MAAM,QAAQ;CAChC,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,OAAO,KAAK,KAAA;CAClE,IAAI,cACH,UAAU,IAAIA,kBAAAA,OAAc,SAAS,cAAc;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAGtF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;CACD,CAAC;CAEA,CAAC,MAAM,UAAU,EAAA,CAAG,OAAO;CAG5B,UAAU,IAAI;EACb,GAAG,UAAU,IAAI,oBAAoB;EACrC,UAAU;EACV,UAAU;EACV,QAAQ;CACT,CAAC;CAED,OAAO,SAAS,UAAU;AAC3B"}
1
+ {"version":3,"file":"redirectToPreviewURL.cjs","names":["getPreviewSessionRef","previewSessionCookie","prismicCookie","getSafePath"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport { getPreviewSessionRef, getSafePath, previewSessionCookie } from \"./lib/toolbarV5\"\nimport type { NextRequestLike } 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\" | \"documentAPIEndpoint\">\n\n\t/**\n\t * The `request` object from a Next.js Route Handler.\n\t *\n\t * @see Next.js Route Handler docs: \\<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\\>\n\t */\n\trequest: NextRequestLike\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 document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<never> {\n\tconst { client, request, linkResolver, defaultURL = \"/\" } = config\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies, draftMode } = await import(\"next/headers\")\n\n\t// Dynamic so Node.js can load this module: the bare specifier (kept by\n\t// tsdown.config.ts for Next.js's server alias) is not resolvable by Node.js.\n\tconst { redirect } = await import(\"next/navigation\")\n\n\tconst { searchParams } = request.nextUrl\n\tconst documentID = searchParams.get(\"documentId\") ?? undefined\n\tlet previewToken = searchParams.get(\"token\") ?? undefined\n\tconst cookieJar = await cookies()\n\n\t// The toolbar's v5 flow follows the session. Share links only carry the session.\n\tconst session = searchParams.get(\"session\")\n\tif (session) {\n\t\tpreviewToken ??= await getPreviewSessionRef(client, session)\n\t\tcookieJar.set(previewSessionCookie, session, { sameSite: \"none\", secure: true })\n\t}\n\n\t// Set the initial preview cookie. Setting the cookie here is necessary\n\t// to support unpublished previews. Without setting it here, the page\n\t// will try to render without the preview cookie, leading to a\n\t// PrismicNotFound error.\n\tif (previewToken) {\n\t\tcookieJar.set(prismicCookie.preview, previewToken, { sameSite: \"none\", secure: true })\n\t}\n\n\tconst previewURL =\n\t\t(session && getSafePath(searchParams.get(\"path\"))) ||\n\t\t(await client.resolvePreviewURL({ documentID, previewToken, defaultURL, linkResolver }))\n\n\t;(await draftMode()).enable()\n\n\t// Next.js uses `SameSite=Lax` in development, which cross-site iframes drop.\n\tcookieJar.set({\n\t\t...cookieJar.get(\"__prerender_bypass\")!,\n\t\thttpOnly: true,\n\t\tsameSite: \"none\",\n\t\tsecure: true,\n\t})\n\n\treturn redirect(previewURL)\n}\n"],"mappings":";;;AAmCA,eAAsB,qBAAqB,QAAoD;CAC9F,MAAM,EAAE,QAAQ,SAAS,cAAc,aAAa,QAAQ;CAK5D,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;CAI5C,MAAM,EAAE,aAAa,MAAM,OAAO;CAElC,MAAM,EAAE,iBAAiB,QAAQ;CACjC,MAAM,aAAa,aAAa,IAAI,YAAY,KAAK,KAAA;CACrD,IAAI,eAAe,aAAa,IAAI,OAAO,KAAK,KAAA;CAChD,MAAM,YAAY,MAAM,QAAQ;CAGhC,MAAM,UAAU,aAAa,IAAI,SAAS;CAC1C,IAAI,SAAS;EACZ,iBAAiB,MAAMA,kBAAAA,qBAAqB,QAAQ,OAAO;EAC3D,UAAU,IAAIC,kBAAAA,sBAAsB,SAAS;GAAE,UAAU;GAAQ,QAAQ;EAAK,CAAC;CAChF;CAMA,IAAI,cACH,UAAU,IAAIC,kBAAAA,OAAc,SAAS,cAAc;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAGtF,MAAM,aACJ,WAAWC,kBAAAA,YAAY,aAAa,IAAI,MAAM,CAAC,KAC/C,MAAM,OAAO,kBAAkB;EAAE;EAAY;EAAc;EAAY;CAAa,CAAC;CAEtF,CAAC,MAAM,UAAU,EAAA,CAAG,OAAO;CAG5B,UAAU,IAAI;EACb,GAAG,UAAU,IAAI,oBAAoB;EACrC,UAAU;EACV,UAAU;EACV,QAAQ;CACT,CAAC;CAED,OAAO,SAAS,UAAU;AAC3B"}
@@ -3,7 +3,7 @@ import { Client, LinkResolverFunction } from "@prismicio/client";
3
3
  //#region src/redirectToPreviewURL.d.ts
4
4
  export type RedirectToPreviewURLConfig = {
5
5
  /** The Prismic client configured for the preview session's repository. */
6
- client: Pick<Client, "resolvePreviewURL">;
6
+ client: Pick<Client, "resolvePreviewURL" | "documentAPIEndpoint">;
7
7
  /**
8
8
  * The `request` object from a Next.js Route Handler.
9
9
  *
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.d.cts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;YAIY;;EAKX,QAAQ,KAAK;;;;;;EAOb,SAAS;;;;;;EAOT,eAAe;;;;;;;EAQf;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
1
+ {"version":3,"file":"redirectToPreviewURL.d.cts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;YAKY;;EAKX,QAAQ,KAAK;;;;;;EAOb,SAAS;;;;;;EAOT,eAAe;;;;;;;EAQf;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
@@ -3,7 +3,7 @@ import { Client, LinkResolverFunction } from "@prismicio/client";
3
3
  //#region src/redirectToPreviewURL.d.ts
4
4
  export type RedirectToPreviewURLConfig = {
5
5
  /** The Prismic client configured for the preview session's repository. */
6
- client: Pick<Client, "resolvePreviewURL">;
6
+ client: Pick<Client, "resolvePreviewURL" | "documentAPIEndpoint">;
7
7
  /**
8
8
  * The `request` object from a Next.js Route Handler.
9
9
  *
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.d.ts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;YAIY;;EAKX,QAAQ,KAAK;;;;;;EAOb,SAAS;;;;;;EAOT,eAAe;;;;;;;EAQf;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
1
+ {"version":3,"file":"redirectToPreviewURL.d.ts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;YAKY;;EAKX,QAAQ,KAAK;;;;;;EAOb,SAAS;;;;;;EAOT,eAAe;;;;;;;EAQf;;wBAGqB,qBAAqB,QAAQ,6BAA6B"}
@@ -1,17 +1,27 @@
1
+ import { getPreviewSessionRef, getSafePath, previewSessionCookie } from "./lib/toolbarV5.js";
1
2
  import { cookie } from "@prismicio/client";
2
3
  //#region src/redirectToPreviewURL.ts
3
4
  async function redirectToPreviewURL(config) {
4
5
  const { client, request, linkResolver, defaultURL = "/" } = config;
5
6
  const { cookies, draftMode } = await import("next/headers.js");
6
7
  const { redirect } = await import("next/navigation");
7
- const documentID = request.nextUrl.searchParams.get("documentId") ?? void 0;
8
+ const { searchParams } = request.nextUrl;
9
+ const documentID = searchParams.get("documentId") ?? void 0;
10
+ let previewToken = searchParams.get("token") ?? void 0;
8
11
  const cookieJar = await cookies();
9
- const previewToken = request.nextUrl.searchParams.get("token") ?? void 0;
12
+ const session = searchParams.get("session");
13
+ if (session) {
14
+ previewToken ??= await getPreviewSessionRef(client, session);
15
+ cookieJar.set(previewSessionCookie, session, {
16
+ sameSite: "none",
17
+ secure: true
18
+ });
19
+ }
10
20
  if (previewToken) cookieJar.set(cookie.preview, previewToken, {
11
21
  sameSite: "none",
12
22
  secure: true
13
23
  });
14
- const previewURL = await client.resolvePreviewURL({
24
+ const previewURL = session && getSafePath(searchParams.get("path")) || await client.resolvePreviewURL({
15
25
  documentID,
16
26
  previewToken,
17
27
  defaultURL,
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport type { NextRequestLike } 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 `request` object from a Next.js Route Handler.\n\t *\n\t * @see Next.js Route Handler docs: \\<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\\>\n\t */\n\trequest: NextRequestLike\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 document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<never> {\n\tconst { client, request, linkResolver, defaultURL = \"/\" } = config\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies, draftMode } = await import(\"next/headers\")\n\n\t// Dynamic so Node.js can load this module: the bare specifier (kept by\n\t// tsdown.config.ts for Next.js's server alias) is not resolvable by Node.js.\n\tconst { redirect } = await import(\"next/navigation\")\n\n\tconst documentID = request.nextUrl.searchParams.get(\"documentId\") ?? undefined\n\n\t// Set the initial preview cookie. Setting the cookie here is necessary\n\t// to support unpublished previews. Without setting it here, the page\n\t// will try to render without the preview cookie, leading to a\n\t// PrismicNotFound error.\n\tconst cookieJar = await cookies()\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tcookieJar.set(prismicCookie.preview, previewToken, { sameSite: \"none\", secure: true })\n\t}\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tdocumentID,\n\t\tpreviewToken,\n\t\tdefaultURL,\n\t\tlinkResolver,\n\t})\n\n\t;(await draftMode()).enable()\n\n\t// Next.js uses `SameSite=Lax` in development, which cross-site iframes drop.\n\tcookieJar.set({\n\t\t...cookieJar.get(\"__prerender_bypass\")!,\n\t\thttpOnly: true,\n\t\tsameSite: \"none\",\n\t\tsecure: true,\n\t})\n\n\treturn redirect(previewURL)\n}\n"],"mappings":";;AAkCA,eAAsB,qBAAqB,QAAoD;CAC9F,MAAM,EAAE,QAAQ,SAAS,cAAc,aAAa,QAAQ;CAK5D,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;CAI5C,MAAM,EAAE,aAAa,MAAM,OAAO;CAElC,MAAM,aAAa,QAAQ,QAAQ,aAAa,IAAI,YAAY,KAAK,KAAA;CAMrE,MAAM,YAAY,MAAM,QAAQ;CAChC,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,OAAO,KAAK,KAAA;CAClE,IAAI,cACH,UAAU,IAAIA,OAAc,SAAS,cAAc;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAGtF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;CACD,CAAC;CAEA,CAAC,MAAM,UAAU,EAAA,CAAG,OAAO;CAG5B,UAAU,IAAI;EACb,GAAG,UAAU,IAAI,oBAAoB;EACrC,UAAU;EACV,UAAU;EACV,QAAQ;CACT,CAAC;CAED,OAAO,SAAS,UAAU;AAC3B"}
1
+ {"version":3,"file":"redirectToPreviewURL.js","names":["prismicCookie"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport { getPreviewSessionRef, getSafePath, previewSessionCookie } from \"./lib/toolbarV5\"\nimport type { NextRequestLike } 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\" | \"documentAPIEndpoint\">\n\n\t/**\n\t * The `request` object from a Next.js Route Handler.\n\t *\n\t * @see Next.js Route Handler docs: \\<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\\>\n\t */\n\trequest: NextRequestLike\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 document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the `defaultURL` option must\n\t * _not_ include it. Instead, provide the `basePath` property using the `basePath` option.\n\t */\n\tdefaultURL?: string\n}\n\nexport async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<never> {\n\tconst { client, request, linkResolver, defaultURL = \"/\" } = config\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies, draftMode } = await import(\"next/headers\")\n\n\t// Dynamic so Node.js can load this module: the bare specifier (kept by\n\t// tsdown.config.ts for Next.js's server alias) is not resolvable by Node.js.\n\tconst { redirect } = await import(\"next/navigation\")\n\n\tconst { searchParams } = request.nextUrl\n\tconst documentID = searchParams.get(\"documentId\") ?? undefined\n\tlet previewToken = searchParams.get(\"token\") ?? undefined\n\tconst cookieJar = await cookies()\n\n\t// The toolbar's v5 flow follows the session. Share links only carry the session.\n\tconst session = searchParams.get(\"session\")\n\tif (session) {\n\t\tpreviewToken ??= await getPreviewSessionRef(client, session)\n\t\tcookieJar.set(previewSessionCookie, session, { sameSite: \"none\", secure: true })\n\t}\n\n\t// Set the initial preview cookie. Setting the cookie here is necessary\n\t// to support unpublished previews. Without setting it here, the page\n\t// will try to render without the preview cookie, leading to a\n\t// PrismicNotFound error.\n\tif (previewToken) {\n\t\tcookieJar.set(prismicCookie.preview, previewToken, { sameSite: \"none\", secure: true })\n\t}\n\n\tconst previewURL =\n\t\t(session && getSafePath(searchParams.get(\"path\"))) ||\n\t\t(await client.resolvePreviewURL({ documentID, previewToken, defaultURL, linkResolver }))\n\n\t;(await draftMode()).enable()\n\n\t// Next.js uses `SameSite=Lax` in development, which cross-site iframes drop.\n\tcookieJar.set({\n\t\t...cookieJar.get(\"__prerender_bypass\")!,\n\t\thttpOnly: true,\n\t\tsameSite: \"none\",\n\t\tsecure: true,\n\t})\n\n\treturn redirect(previewURL)\n}\n"],"mappings":";;;AAmCA,eAAsB,qBAAqB,QAAoD;CAC9F,MAAM,EAAE,QAAQ,SAAS,cAAc,aAAa,QAAQ;CAK5D,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;CAI5C,MAAM,EAAE,aAAa,MAAM,OAAO;CAElC,MAAM,EAAE,iBAAiB,QAAQ;CACjC,MAAM,aAAa,aAAa,IAAI,YAAY,KAAK,KAAA;CACrD,IAAI,eAAe,aAAa,IAAI,OAAO,KAAK,KAAA;CAChD,MAAM,YAAY,MAAM,QAAQ;CAGhC,MAAM,UAAU,aAAa,IAAI,SAAS;CAC1C,IAAI,SAAS;EACZ,iBAAiB,MAAM,qBAAqB,QAAQ,OAAO;EAC3D,UAAU,IAAI,sBAAsB,SAAS;GAAE,UAAU;GAAQ,QAAQ;EAAK,CAAC;CAChF;CAMA,IAAI,cACH,UAAU,IAAIA,OAAc,SAAS,cAAc;EAAE,UAAU;EAAQ,QAAQ;CAAK,CAAC;CAGtF,MAAM,aACJ,WAAW,YAAY,aAAa,IAAI,MAAM,CAAC,KAC/C,MAAM,OAAO,kBAAkB;EAAE;EAAY;EAAc;EAAY;CAAa,CAAC;CAEtF,CAAC,MAAM,UAAU,EAAA,CAAG,OAAO;CAG5B,UAAU,IAAI;EACb,GAAG,UAAU,IAAI,oBAAoB;EACrC,UAAU;EACV,UAAU;EACV,QAAQ;CACT,CAAC;CAED,OAAO,SAAS,UAAU;AAC3B"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/next",
3
- "version": "2.3.4-pr.174.890bc09",
3
+ "version": "2.3.4-pr.175.5f9614e",
4
4
  "description": "Helpers to integrate Prismic into Next.js apps",
5
5
  "keywords": [
6
6
  "next",
@@ -60,16 +60,16 @@
60
60
  "lz-string": "^1.5.0"
61
61
  },
62
62
  "devDependencies": {
63
- "@playwright/test": "^1.64.0",
63
+ "@playwright/test": "^1.64.1",
64
64
  "@prismicio/client": "^7.22.3",
65
65
  "@types/node": "^26.6.5",
66
66
  "@types/react": "^19.3.0",
67
- "next": "16.4.0",
68
- "oxfmt": "^0.72.0",
69
- "oxlint": "^1.87.0",
70
- "playwright": "^1.64.0",
71
- "react": "^19.3.0",
72
- "react-dom": "^19.3.0",
67
+ "next": "16.4.2",
68
+ "oxfmt": "^0.73.0",
69
+ "oxlint": "^1.88.0",
70
+ "playwright": "^1.64.1",
71
+ "react": "^19.3.1",
72
+ "react-dom": "^19.3.1",
73
73
  "tsdown": "^0.23.1",
74
74
  "typescript": "^7.0.2"
75
75
  },
@@ -1,12 +1,7 @@
1
- import { getToolbarSrc } from "@prismicio/client"
2
- import Script from "next/script"
3
1
  import type { FC, ReactNode } from "react"
4
2
 
5
- import { resolveDefaultExport } from "./lib/resolveDefaultExport"
6
3
  import { PrismicPreviewClient } from "./PrismicPreviewClient"
7
4
 
8
- const ResolvedScript = resolveDefaultExport(Script)
9
-
10
5
  /** Props for `<PrismicPreview>`. */
11
6
  export type PrismicPreviewProps = {
12
7
  /**
@@ -47,7 +42,6 @@ export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
47
42
  // in a Server Component which is not supported in the pages/ directory.
48
43
  const { draftMode } = await import("next/headers")
49
44
 
50
- const toolbarSrc = getToolbarSrc(repositoryName)
51
45
  const isDraftMode = (await draftMode()).isEnabled
52
46
 
53
47
  return (
@@ -58,7 +52,6 @@ export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
58
52
  isDraftMode={isDraftMode}
59
53
  {...otherProps}
60
54
  />
61
- <ResolvedScript src={toolbarSrc} strategy="lazyOnload" />
62
55
  </>
63
56
  )
64
57
  }
@@ -2,9 +2,15 @@
2
2
 
3
3
  import { cookie as prismicCookie } from "@prismicio/client"
4
4
  import { useRouter } from "next/navigation"
5
+ import Script from "next/script"
5
6
  import type { FC } from "react"
6
7
  import { useEffect } from "react"
7
8
 
9
+ import { resolveDefaultExport } from "./lib/resolveDefaultExport"
10
+ import { getToolbarScriptProps } from "./lib/toolbarV5"
11
+
12
+ const ResolvedScript = resolveDefaultExport(Script)
13
+
8
14
  type PrismicPreviewClientProps = {
9
15
  repositoryName: string
10
16
  isDraftMode: boolean
@@ -110,7 +116,12 @@ export const PrismicPreviewClient: FC<PrismicPreviewClientProps> = (props) => {
110
116
  return () => controller.abort()
111
117
  }, [repositoryName, isDraftMode, updatePreviewURL, exitPreviewURL, refresh])
112
118
 
113
- return null
119
+ return (
120
+ <ResolvedScript
121
+ {...getToolbarScriptProps(repositoryName, updatePreviewURL)}
122
+ strategy="lazyOnload"
123
+ />
124
+ )
114
125
  }
115
126
 
116
127
  /**
@@ -1,5 +1,7 @@
1
1
  import { cookie as prismicCookie } from "@prismicio/client"
2
2
 
3
+ import { previewSessionCookie } from "./lib/toolbarV5"
4
+
3
5
  /**
4
6
  * Ends a Prismic preview session within a Next.js app. This function should be used in a Router
5
7
  * Handler.
@@ -24,6 +26,7 @@ export async function exitPreview(): Promise<Response> {
24
26
  const cookieJar = await cookies()
25
27
  cookieJar.delete({ name: "__prerender_bypass", sameSite: "none", secure: true })
26
28
  cookieJar.delete({ name: prismicCookie.preview, sameSite: "none", secure: true })
29
+ cookieJar.delete({ name: previewSessionCookie, sameSite: "none", secure: true })
27
30
 
28
31
  // `Cache-Control` header is used to prevent CDN-level caching.
29
32
  return new Response(JSON.stringify({ success: true }), {
@@ -0,0 +1,54 @@
1
+ import { getToolbarSrc, PreviewTokenExpiredError, type Client } from "@prismicio/client"
2
+
3
+ /** The cookie storing the preview session ID. The toolbar's v5 flow follows this session. */
4
+ export const previewSessionCookie = "io.prismic.previewSession"
5
+
6
+ /**
7
+ * Props for the toolbar's `<script>`, opting into its v5 flow.
8
+ *
9
+ * @param previewURL - The app's preview route, as passed to `fetch()`.
10
+ */
11
+ export function getToolbarScriptProps(
12
+ repositoryName: string,
13
+ previewURL: string,
14
+ ): { src: string; "data-preview-url"?: string } {
15
+ return {
16
+ // TODO: Use `getToolbarSrc()` once `@prismicio/client` adds `v=5`.
17
+ src: `${getToolbarSrc(repositoryName)}&v=5`,
18
+ // The toolbar builds share links with this URL. `next/script` adds `lazyOnload` scripts in
19
+ // the browser, where the URL is known.
20
+ "data-preview-url":
21
+ typeof window === "undefined" ? undefined : new URL(previewURL, document.baseURI).href,
22
+ }
23
+ }
24
+
25
+ /** Fetches the current ref of a preview session, for share links that only carry the session. */
26
+ export async function getPreviewSessionRef(
27
+ client: Pick<Client, "documentAPIEndpoint">,
28
+ session: string,
29
+ ): Promise<string> {
30
+ const url = new URL(`/previews/token/${encodeURIComponent(session)}`, client.documentAPIEndpoint)
31
+ // The endpoint is on the repository host, not on its CDN.
32
+ url.hostname = url.hostname.replace(".cdn.", ".")
33
+ const res = await fetch(url, { cache: "no-store" })
34
+ const json = await res.json()
35
+ if (!json.ref) {
36
+ // Like the Content API's response to an expired preview token.
37
+ throw new PreviewTokenExpiredError("This preview session has expired", url.href, json)
38
+ }
39
+
40
+ return json.ref
41
+ }
42
+
43
+ /** Returns `path` if it is a path on the same origin, like `/blog?page=2`. */
44
+ export function getSafePath(path: string | undefined | null): string | undefined {
45
+ if (!path?.startsWith("/") || path.startsWith("//") || path.startsWith("/\\")) {
46
+ return
47
+ }
48
+
49
+ const origin = "https://placeholder.invalid"
50
+ const url = new URL(path, origin)
51
+ if (url.origin === origin) {
52
+ return url.href.slice(origin.length)
53
+ }
54
+ }
@@ -1,9 +1,11 @@
1
- import { getToolbarSrc, cookie as prismicCookie } from "@prismicio/client"
1
+ import { cookie as prismicCookie } from "@prismicio/client"
2
2
  import { useRouter } from "next/router"
3
3
  import Script from "next/script"
4
4
  import type { FC } from "react"
5
5
  import { type ReactNode, useEffect } from "react"
6
6
 
7
+ import { getToolbarScriptProps } from "../lib/toolbarV5"
8
+
7
9
  /** Props for `<PrismicPreview>`. */
8
10
  export type PrismicPreviewProps = {
9
11
  /**
@@ -46,8 +48,6 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
46
48
 
47
49
  const router = useRouter()
48
50
 
49
- const toolbarSrc = getToolbarSrc(repositoryName)
50
-
51
51
  useEffect(() => {
52
52
  const controller = new AbortController()
53
53
 
@@ -127,7 +127,10 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
127
127
  return (
128
128
  <>
129
129
  {children}
130
- <Script src={toolbarSrc} strategy="lazyOnload" />
130
+ <Script
131
+ {...getToolbarScriptProps(repositoryName, router.basePath + updatePreviewURL)}
132
+ strategy="lazyOnload"
133
+ />
131
134
  </>
132
135
  )
133
136
  }
@@ -0,0 +1,16 @@
1
+ import type { NextApiResponseLike } from "./types"
2
+
3
+ /** Adds a cookie like the App Router's preview cookie, keeping Next.js's preview data cookies. */
4
+ export function appendCookie(
5
+ res: NextApiResponseLike,
6
+ name: string,
7
+ value: string,
8
+ expires?: Date,
9
+ ): void {
10
+ const previous = res.getHeader("Set-Cookie") ?? []
11
+ let cookie = `${name}=${encodeURIComponent(value)}; Path=/; SameSite=None; Secure`
12
+ if (expires) {
13
+ cookie += `; Expires=${expires.toUTCString()}`
14
+ }
15
+ res.setHeader("Set-Cookie", [...[previous].flat().map(String), cookie])
16
+ }
@@ -1,3 +1,5 @@
1
+ import { previewSessionCookie } from "../lib/toolbarV5"
2
+ import { appendCookie } from "./appendCookie"
1
3
  import type { NextApiRequestLike, NextApiResponseLike } from "./types"
2
4
 
3
5
  /** Configuration for `exitPreview()`. */
@@ -34,6 +36,7 @@ export type ExitPreviewAPIRouteConfig = {
34
36
  */
35
37
  export function exitPreview(config: ExitPreviewAPIRouteConfig): void {
36
38
  config.res.clearPreviewData()
39
+ appendCookie(config.res, previewSessionCookie, "", new Date(0))
37
40
 
38
41
  // `Cache-Control` header is used to prevent CDN-level caching.
39
42
  config.res.setHeader("Cache-Control", "no-store")
@@ -1,5 +1,7 @@
1
- import type { LinkResolverFunction, Client } from "@prismicio/client"
1
+ import { cookie as prismicCookie, type LinkResolverFunction, type Client } from "@prismicio/client"
2
2
 
3
+ import { getPreviewSessionRef, getSafePath, previewSessionCookie } from "../lib/toolbarV5"
4
+ import { appendCookie } from "./appendCookie"
3
5
  import type { NextApiRequestLike, NextApiResponseLike } from "./types"
4
6
 
5
7
  export type RedirectToPreviewURLConfig = {
@@ -7,7 +9,7 @@ export type RedirectToPreviewURLConfig = {
7
9
  // `Pick` is used to use the smallest possible subset of
8
10
  // `prismic.Client`. Doing this reduces the surface area for breaking
9
11
  // type changes.
10
- client: Pick<Client, "resolvePreviewURL">
12
+ client: Pick<Client, "resolvePreviewURL" | "documentAPIEndpoint">
11
13
 
12
14
  /**
13
15
  * The `req` object from a Next.js API route.
@@ -52,7 +54,26 @@ export type RedirectToPreviewURLConfig = {
52
54
  export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void> {
53
55
  const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config
54
56
 
55
- const previewToken = req.query.token?.toString()
57
+ let previewToken = req.query.token?.toString()
58
+
59
+ // The toolbar's v5 flow follows the session in the preview cookies. Share
60
+ // links only carry the session, so `setPreviewData()` has no ref for them.
61
+ const session = req.query.session?.toString()
62
+ if (session) {
63
+ if (!previewToken) {
64
+ previewToken = await getPreviewSessionRef(client, session)
65
+ res.setPreviewData({ ref: previewToken })
66
+ }
67
+ appendCookie(res, prismicCookie.preview, previewToken)
68
+ appendCookie(res, previewSessionCookie, session)
69
+
70
+ const path = getSafePath(req.query.path?.toString())
71
+ if (path) {
72
+ res.redirect(path)
73
+
74
+ return
75
+ }
76
+ }
56
77
 
57
78
  const previewURL = await client.resolvePreviewURL({
58
79
  linkResolver,