@prismicio/next 2.3.1-pr.148.a101ed4 → 2.3.1-pr.148.d5dbc1a

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.
@@ -1,4 +1,5 @@
1
1
  require("./_virtual/_rolldown/runtime.cjs");
2
+ const require_previewCookieAttributes = require("./lib/previewCookieAttributes.cjs");
2
3
  let _prismicio_client = require("@prismicio/client");
3
4
  //#region src/exitPreview.ts
4
5
  /**
@@ -17,12 +18,18 @@ let _prismicio_client = require("@prismicio/client");
17
18
  * ```
18
19
  */
19
20
  async function exitPreview() {
20
- const { cookies, draftMode } = await import("next/headers.js");
21
+ const { cookies, draftMode, headers } = await import("next/headers.js");
21
22
  (await draftMode()).disable();
22
- (await cookies()).delete({
23
+ const cookieJar = await cookies();
24
+ const cookieAttributes = require_previewCookieAttributes.previewCookieAttributes((await headers()).get("host"));
25
+ cookieJar.delete({
26
+ name: "__prerender_bypass",
27
+ httpOnly: true,
28
+ ...cookieAttributes
29
+ });
30
+ cookieJar.delete({
23
31
  name: _prismicio_client.cookie.preview,
24
- sameSite: "none",
25
- secure: true
32
+ ...cookieAttributes
26
33
  });
27
34
  return new Response(JSON.stringify({ success: true }), { headers: { "Cache-Control": "no-store" } });
28
35
  }
@@ -1 +1 @@
1
- {"version":3,"file":"exitPreview.cjs","names":["prismicCookie"],"sources":["../src/exitPreview.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in a Router\n * Handler.\n *\n * @example\n * \t;```typescript\n * \t// src/app/api/exit-preview/route.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport async function GET() {\n * \t\treturn await exitPreview()\n * \t}\n * \t```\n */\nexport async function exitPreview(): Promise<Response> {\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;(await draftMode()).disable()\n\n\t// `redirectToPreviewURL` writes the preview cookie, so `exitPreview`\n\t// clears it to close the preview-cookie loop. The attributes must match\n\t// the write: a cross-site iframe can only clear a `SameSite=None` cookie.\n\t;(await cookies()).delete({ name: prismicCookie.preview, sameSite: \"none\", secure: true })\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\treturn new Response(JSON.stringify({ success: true }), {\n\t\theaders: {\n\t\t\t\"Cache-Control\": \"no-store\",\n\t\t},\n\t})\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiBA,eAAsB,cAAiC;CAItD,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;AAE3C,EAAC,MAAM,WAAW,EAAE,SAAS;AAK7B,EAAC,MAAM,SAAS,EAAE,OAAO;EAAE,MAAMA,kBAAAA,OAAc;EAAS,UAAU;EAAQ,QAAQ;EAAM,CAAC;AAG1F,QAAO,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,MAAM,CAAC,EAAE,EACtD,SAAS,EACR,iBAAiB,YACjB,EACD,CAAC"}
1
+ {"version":3,"file":"exitPreview.cjs","names":["previewCookieAttributes","prismicCookie"],"sources":["../src/exitPreview.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\nimport { previewCookieAttributes } from \"./lib/previewCookieAttributes\"\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in a Router\n * Handler.\n *\n * @example\n * \t;```typescript\n * \t// src/app/api/exit-preview/route.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport async function GET() {\n * \t\treturn await exitPreview()\n * \t}\n * \t```\n */\nexport async function exitPreview(): Promise<Response> {\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, headers } = await import(\"next/headers\")\n\n\t;(await draftMode()).disable()\n\n\t// Clear the cookies `redirectToPreviewURL` wrote with the attributes it\n\t// used: a cross-site iframe can only clear `SameSite=None` cookies, which\n\t// `draftMode().disable()` does not use in development.\n\tconst cookieJar = await cookies()\n\tconst cookieAttributes = previewCookieAttributes((await headers()).get(\"host\"))\n\tcookieJar.delete({ name: \"__prerender_bypass\", httpOnly: true, ...cookieAttributes })\n\tcookieJar.delete({ name: prismicCookie.preview, ...cookieAttributes })\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\treturn new Response(JSON.stringify({ success: true }), {\n\t\theaders: {\n\t\t\t\"Cache-Control\": \"no-store\",\n\t\t},\n\t})\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,eAAsB,cAAiC;CAItD,MAAM,EAAE,SAAS,WAAW,YAAY,MAAM,OAAO;AAEpD,EAAC,MAAM,WAAW,EAAE,SAAS;CAK9B,MAAM,YAAY,MAAM,SAAS;CACjC,MAAM,mBAAmBA,gCAAAA,yBAAyB,MAAM,SAAS,EAAE,IAAI,OAAO,CAAC;AAC/E,WAAU,OAAO;EAAE,MAAM;EAAsB,UAAU;EAAM,GAAG;EAAkB,CAAC;AACrF,WAAU,OAAO;EAAE,MAAMC,kBAAAA,OAAc;EAAS,GAAG;EAAkB,CAAC;AAGtE,QAAO,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,MAAM,CAAC,EAAE,EACtD,SAAS,EACR,iBAAiB,YACjB,EACD,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;AAiBA;;;;;;;;;;;;;;iBAAsB,WAAA,CAAA,GAAe,OAAA,CAAQ,QAAA"}
1
+ {"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;AAmBA;;;;;;;;;;;;;;iBAAsB,WAAA,CAAA,GAAe,OAAA,CAAQ,QAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;AAiBA;;;;;;;;;;;;;;iBAAsB,WAAA,CAAA,GAAe,OAAA,CAAQ,QAAA"}
1
+ {"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;AAmBA;;;;;;;;;;;;;;iBAAsB,WAAA,CAAA,GAAe,OAAA,CAAQ,QAAA"}
@@ -1,3 +1,4 @@
1
+ import { previewCookieAttributes } from "./lib/previewCookieAttributes.js";
1
2
  import { cookie } from "@prismicio/client";
2
3
  //#region src/exitPreview.ts
3
4
  /**
@@ -16,12 +17,18 @@ import { cookie } from "@prismicio/client";
16
17
  * ```
17
18
  */
18
19
  async function exitPreview() {
19
- const { cookies, draftMode } = await import("next/headers.js");
20
+ const { cookies, draftMode, headers } = await import("next/headers.js");
20
21
  (await draftMode()).disable();
21
- (await cookies()).delete({
22
+ const cookieJar = await cookies();
23
+ const cookieAttributes = previewCookieAttributes((await headers()).get("host"));
24
+ cookieJar.delete({
25
+ name: "__prerender_bypass",
26
+ httpOnly: true,
27
+ ...cookieAttributes
28
+ });
29
+ cookieJar.delete({
22
30
  name: cookie.preview,
23
- sameSite: "none",
24
- secure: true
31
+ ...cookieAttributes
25
32
  });
26
33
  return new Response(JSON.stringify({ success: true }), { headers: { "Cache-Control": "no-store" } });
27
34
  }
@@ -1 +1 @@
1
- {"version":3,"file":"exitPreview.js","names":["prismicCookie"],"sources":["../src/exitPreview.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in a Router\n * Handler.\n *\n * @example\n * \t;```typescript\n * \t// src/app/api/exit-preview/route.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport async function GET() {\n * \t\treturn await exitPreview()\n * \t}\n * \t```\n */\nexport async function exitPreview(): Promise<Response> {\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;(await draftMode()).disable()\n\n\t// `redirectToPreviewURL` writes the preview cookie, so `exitPreview`\n\t// clears it to close the preview-cookie loop. The attributes must match\n\t// the write: a cross-site iframe can only clear a `SameSite=None` cookie.\n\t;(await cookies()).delete({ name: prismicCookie.preview, sameSite: \"none\", secure: true })\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\treturn new Response(JSON.stringify({ success: true }), {\n\t\theaders: {\n\t\t\t\"Cache-Control\": \"no-store\",\n\t\t},\n\t})\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAiBA,eAAsB,cAAiC;CAItD,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;AAE3C,EAAC,MAAM,WAAW,EAAE,SAAS;AAK7B,EAAC,MAAM,SAAS,EAAE,OAAO;EAAE,MAAMA,OAAc;EAAS,UAAU;EAAQ,QAAQ;EAAM,CAAC;AAG1F,QAAO,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,MAAM,CAAC,EAAE,EACtD,SAAS,EACR,iBAAiB,YACjB,EACD,CAAC"}
1
+ {"version":3,"file":"exitPreview.js","names":["prismicCookie"],"sources":["../src/exitPreview.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\nimport { previewCookieAttributes } from \"./lib/previewCookieAttributes\"\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be used in a Router\n * Handler.\n *\n * @example\n * \t;```typescript\n * \t// src/app/api/exit-preview/route.js\n *\n * \timport { exitPreview } from \"@prismicio/next\"\n *\n * \texport async function GET() {\n * \t\treturn await exitPreview()\n * \t}\n * \t```\n */\nexport async function exitPreview(): Promise<Response> {\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, headers } = await import(\"next/headers\")\n\n\t;(await draftMode()).disable()\n\n\t// Clear the cookies `redirectToPreviewURL` wrote with the attributes it\n\t// used: a cross-site iframe can only clear `SameSite=None` cookies, which\n\t// `draftMode().disable()` does not use in development.\n\tconst cookieJar = await cookies()\n\tconst cookieAttributes = previewCookieAttributes((await headers()).get(\"host\"))\n\tcookieJar.delete({ name: \"__prerender_bypass\", httpOnly: true, ...cookieAttributes })\n\tcookieJar.delete({ name: prismicCookie.preview, ...cookieAttributes })\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\treturn new Response(JSON.stringify({ success: true }), {\n\t\theaders: {\n\t\t\t\"Cache-Control\": \"no-store\",\n\t\t},\n\t})\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmBA,eAAsB,cAAiC;CAItD,MAAM,EAAE,SAAS,WAAW,YAAY,MAAM,OAAO;AAEpD,EAAC,MAAM,WAAW,EAAE,SAAS;CAK9B,MAAM,YAAY,MAAM,SAAS;CACjC,MAAM,mBAAmB,yBAAyB,MAAM,SAAS,EAAE,IAAI,OAAO,CAAC;AAC/E,WAAU,OAAO;EAAE,MAAM;EAAsB,UAAU;EAAM,GAAG;EAAkB,CAAC;AACrF,WAAU,OAAO;EAAE,MAAMA,OAAc;EAAS,GAAG;EAAkB,CAAC;AAGtE,QAAO,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,MAAM,CAAC,EAAE,EACtD,SAAS,EACR,iBAAiB,YACjB,EACD,CAAC"}
@@ -0,0 +1,20 @@
1
+ //#region src/lib/previewCookieAttributes.ts
2
+ /**
3
+ * Attributes for the preview cookie and Next.js's Draft Mode cookie.
4
+ *
5
+ * Browsers only send `SameSite=Lax` cookies on same-site requests, so the cookies need
6
+ * `SameSite=None; Secure` to work when the site is embedded in a cross-site iframe, like the
7
+ * Prismic editor. Browsers reject `Secure` cookies over plain HTTP unless the host is `localhost`,
8
+ * so `next dev` on any other host keeps Next.js's `Lax` default.
9
+ */
10
+ function previewCookieAttributes(host) {
11
+ const secure = process.env.NODE_ENV !== "development" || host?.split(":")[0] === "localhost";
12
+ return {
13
+ sameSite: secure ? "none" : "lax",
14
+ secure
15
+ };
16
+ }
17
+ //#endregion
18
+ exports.previewCookieAttributes = previewCookieAttributes;
19
+
20
+ //# sourceMappingURL=previewCookieAttributes.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"previewCookieAttributes.cjs","names":[],"sources":["../../src/lib/previewCookieAttributes.ts"],"sourcesContent":["/**\n * Attributes for the preview cookie and Next.js's Draft Mode cookie.\n *\n * Browsers only send `SameSite=Lax` cookies on same-site requests, so the cookies need\n * `SameSite=None; Secure` to work when the site is embedded in a cross-site iframe, like the\n * Prismic editor. Browsers reject `Secure` cookies over plain HTTP unless the host is `localhost`,\n * so `next dev` on any other host keeps Next.js's `Lax` default.\n */\nexport function previewCookieAttributes(host: string | null | undefined): {\n\tsameSite: \"none\" | \"lax\"\n\tsecure: boolean\n} {\n\tconst secure = process.env.NODE_ENV !== \"development\" || host?.split(\":\")[0] === \"localhost\"\n\n\treturn { sameSite: secure ? \"none\" : \"lax\", secure }\n}\n"],"mappings":";;;;;;;;;AAQA,SAAgB,wBAAwB,MAGtC;CACD,MAAM,SAAS,QAAQ,IAAI,aAAa,iBAAiB,MAAM,MAAM,IAAI,CAAC,OAAO;AAEjF,QAAO;EAAE,UAAU,SAAS,SAAS;EAAO;EAAQ"}
@@ -0,0 +1,20 @@
1
+ //#region src/lib/previewCookieAttributes.ts
2
+ /**
3
+ * Attributes for the preview cookie and Next.js's Draft Mode cookie.
4
+ *
5
+ * Browsers only send `SameSite=Lax` cookies on same-site requests, so the cookies need
6
+ * `SameSite=None; Secure` to work when the site is embedded in a cross-site iframe, like the
7
+ * Prismic editor. Browsers reject `Secure` cookies over plain HTTP unless the host is `localhost`,
8
+ * so `next dev` on any other host keeps Next.js's `Lax` default.
9
+ */
10
+ function previewCookieAttributes(host) {
11
+ const secure = process.env.NODE_ENV !== "development" || host?.split(":")[0] === "localhost";
12
+ return {
13
+ sameSite: secure ? "none" : "lax",
14
+ secure
15
+ };
16
+ }
17
+ //#endregion
18
+ export { previewCookieAttributes };
19
+
20
+ //# sourceMappingURL=previewCookieAttributes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"previewCookieAttributes.js","names":[],"sources":["../../src/lib/previewCookieAttributes.ts"],"sourcesContent":["/**\n * Attributes for the preview cookie and Next.js's Draft Mode cookie.\n *\n * Browsers only send `SameSite=Lax` cookies on same-site requests, so the cookies need\n * `SameSite=None; Secure` to work when the site is embedded in a cross-site iframe, like the\n * Prismic editor. Browsers reject `Secure` cookies over plain HTTP unless the host is `localhost`,\n * so `next dev` on any other host keeps Next.js's `Lax` default.\n */\nexport function previewCookieAttributes(host: string | null | undefined): {\n\tsameSite: \"none\" | \"lax\"\n\tsecure: boolean\n} {\n\tconst secure = process.env.NODE_ENV !== \"development\" || host?.split(\":\")[0] === \"localhost\"\n\n\treturn { sameSite: secure ? \"none\" : \"lax\", secure }\n}\n"],"mappings":";;;;;;;;;AAQA,SAAgB,wBAAwB,MAGtC;CACD,MAAM,SAAS,QAAQ,IAAI,aAAa,iBAAiB,MAAM,MAAM,IAAI,CAAC,OAAO;AAEjF,QAAO;EAAE,UAAU,SAAS,SAAS;EAAO;EAAQ"}
package/dist/package.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "2.3.1-pr.148.a101ed4";
2
+ var version = "2.3.1-pr.148.d5dbc1a";
3
3
  //#endregion
4
4
  Object.defineProperty(exports, "version", {
5
5
  enumerable: true,
package/dist/package.js CHANGED
@@ -1,5 +1,5 @@
1
1
  //#region package.json
2
- var version = "2.3.1-pr.148.a101ed4";
2
+ var version = "2.3.1-pr.148.d5dbc1a";
3
3
  //#endregion
4
4
  export { version };
5
5
 
@@ -1,4 +1,5 @@
1
1
  require("./_virtual/_rolldown/runtime.cjs");
2
+ const require_previewCookieAttributes = require("./lib/previewCookieAttributes.cjs");
2
3
  let _prismicio_client = require("@prismicio/client");
3
4
  //#region src/redirectToPreviewURL.ts
4
5
  async function redirectToPreviewURL(config) {
@@ -6,11 +7,10 @@ async function redirectToPreviewURL(config) {
6
7
  const { cookies, draftMode } = await import("next/headers.js");
7
8
  const { redirect } = await import("next/navigation");
8
9
  const documentID = request.nextUrl.searchParams.get("documentId") ?? void 0;
10
+ const cookieJar = await cookies();
11
+ const cookieAttributes = require_previewCookieAttributes.previewCookieAttributes(request.headers.get("host"));
9
12
  const previewToken = request.nextUrl.searchParams.get("token") ?? void 0;
10
- if (previewToken) (await cookies()).set(_prismicio_client.cookie.preview, previewToken, {
11
- sameSite: "none",
12
- secure: true
13
- });
13
+ if (previewToken) cookieJar.set(_prismicio_client.cookie.preview, previewToken, cookieAttributes);
14
14
  const previewURL = await client.resolvePreviewURL({
15
15
  documentID,
16
16
  previewToken,
@@ -18,6 +18,11 @@ async function redirectToPreviewURL(config) {
18
18
  linkResolver
19
19
  });
20
20
  (await draftMode()).enable();
21
+ const draftCookie = cookieJar.get("__prerender_bypass");
22
+ if (draftCookie) cookieJar.set({
23
+ ...draftCookie,
24
+ ...cookieAttributes
25
+ });
21
26
  return redirect(previewURL);
22
27
  }
23
28
  //#endregion
@@ -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\t//\n\t// `SameSite=None` lets browsers send the cookie when the site is embedded\n\t// in a cross-site iframe, like the Prismic editor.\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tconst cookieJar = await cookies()\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\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,aAAa,IAAI,KAAA;CASrE,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,QAAQ,IAAI,KAAA;AAClE,KAAI,aAEH,EADkB,MAAM,SAAS,EACvB,IAAIA,kBAAAA,OAAc,SAAS,cAAc;EAAE,UAAU;EAAQ,QAAQ;EAAM,CAAC;CAGvF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;EACA,CAAC;AAED,EAAC,MAAM,WAAW,EAAE,QAAQ;AAE7B,QAAO,SAAS,WAAW"}
1
+ {"version":3,"file":"redirectToPreviewURL.cjs","names":["previewCookieAttributes","prismicCookie"],"sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { cookie as prismicCookie, type Client, type LinkResolverFunction } from \"@prismicio/client\"\n\nimport { previewCookieAttributes } from \"./lib/previewCookieAttributes\"\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 cookieAttributes = previewCookieAttributes(request.headers.get(\"host\"))\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tcookieJar.set(prismicCookie.preview, previewToken, cookieAttributes)\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 writes its Draft Mode cookie as `SameSite=Lax` in development,\n\t// which cross-site iframes drop. Rewrite it with the preview cookie's\n\t// attributes, keeping Next.js's value.\n\tconst draftCookie = cookieJar.get(\"__prerender_bypass\")\n\tif (draftCookie) {\n\t\tcookieJar.set({ ...draftCookie, ...cookieAttributes })\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,aAAa,QAAQ,QAAQ,aAAa,IAAI,aAAa,IAAI,KAAA;CAMrE,MAAM,YAAY,MAAM,SAAS;CACjC,MAAM,mBAAmBA,gCAAAA,wBAAwB,QAAQ,QAAQ,IAAI,OAAO,CAAC;CAC7E,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,QAAQ,IAAI,KAAA;AAClE,KAAI,aACH,WAAU,IAAIC,kBAAAA,OAAc,SAAS,cAAc,iBAAiB;CAGrE,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;EACA,CAAC;AAED,EAAC,MAAM,WAAW,EAAE,QAAQ;CAK7B,MAAM,cAAc,UAAU,IAAI,qBAAqB;AACvD,KAAI,YACH,WAAU,IAAI;EAAE,GAAG;EAAa,GAAG;EAAkB,CAAC;AAGvD,QAAO,SAAS,WAAW"}
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.d.cts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;;KAIY,0BAAA;4EAKX,MAAA,EAAQ,IAAA,CAAK,MAAA;EALwB;;;;;EAYrC,OAAA,EAAS,eAAA;EAO0B;;;;;EAAnC,YAAA,GAAe,oBAAA;EAPN;;;;;;EAeT,UAAA;AAAA;AAAA,iBAGqB,oBAAA,CAAqB,MAAA,EAAQ,0BAAA,GAA6B,OAAA"}
1
+ {"version":3,"file":"redirectToPreviewURL.d.cts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;;KAKY,0BAAA;4EAKX,MAAA,EAAQ,IAAA,CAAK,MAAA;EALwB;;;;;EAYrC,OAAA,EAAS,eAAA;EAO0B;;;;;EAAnC,YAAA,GAAe,oBAAA;EAPN;;;;;;EAeT,UAAA;AAAA;AAAA,iBAGqB,oBAAA,CAAqB,MAAA,EAAQ,0BAAA,GAA6B,OAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"redirectToPreviewURL.d.ts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;;KAIY,0BAAA;4EAKX,MAAA,EAAQ,IAAA,CAAK,MAAA;EALwB;;;;;EAYrC,OAAA,EAAS,eAAA;EAO0B;;;;;EAAnC,YAAA,GAAe,oBAAA;EAPN;;;;;;EAeT,UAAA;AAAA;AAAA,iBAGqB,oBAAA,CAAqB,MAAA,EAAQ,0BAAA,GAA6B,OAAA"}
1
+ {"version":3,"file":"redirectToPreviewURL.d.ts","names":[],"sources":["../src/redirectToPreviewURL.ts"],"mappings":";;;;KAKY,0BAAA;4EAKX,MAAA,EAAQ,IAAA,CAAK,MAAA;EALwB;;;;;EAYrC,OAAA,EAAS,eAAA;EAO0B;;;;;EAAnC,YAAA,GAAe,oBAAA;EAPN;;;;;;EAeT,UAAA;AAAA;AAAA,iBAGqB,oBAAA,CAAqB,MAAA,EAAQ,0BAAA,GAA6B,OAAA"}
@@ -1,3 +1,4 @@
1
+ import { previewCookieAttributes } from "./lib/previewCookieAttributes.js";
1
2
  import { cookie } from "@prismicio/client";
2
3
  //#region src/redirectToPreviewURL.ts
3
4
  async function redirectToPreviewURL(config) {
@@ -5,11 +6,10 @@ async function redirectToPreviewURL(config) {
5
6
  const { cookies, draftMode } = await import("next/headers.js");
6
7
  const { redirect } = await import("next/navigation");
7
8
  const documentID = request.nextUrl.searchParams.get("documentId") ?? void 0;
9
+ const cookieJar = await cookies();
10
+ const cookieAttributes = previewCookieAttributes(request.headers.get("host"));
8
11
  const previewToken = request.nextUrl.searchParams.get("token") ?? void 0;
9
- if (previewToken) (await cookies()).set(cookie.preview, previewToken, {
10
- sameSite: "none",
11
- secure: true
12
- });
12
+ if (previewToken) cookieJar.set(cookie.preview, previewToken, cookieAttributes);
13
13
  const previewURL = await client.resolvePreviewURL({
14
14
  documentID,
15
15
  previewToken,
@@ -17,6 +17,11 @@ async function redirectToPreviewURL(config) {
17
17
  linkResolver
18
18
  });
19
19
  (await draftMode()).enable();
20
+ const draftCookie = cookieJar.get("__prerender_bypass");
21
+ if (draftCookie) cookieJar.set({
22
+ ...draftCookie,
23
+ ...cookieAttributes
24
+ });
20
25
  return redirect(previewURL);
21
26
  }
22
27
  //#endregion
@@ -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\t//\n\t// `SameSite=None` lets browsers send the cookie when the site is embedded\n\t// in a cross-site iframe, like the Prismic editor.\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tconst cookieJar = await cookies()\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\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,aAAa,IAAI,KAAA;CASrE,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,QAAQ,IAAI,KAAA;AAClE,KAAI,aAEH,EADkB,MAAM,SAAS,EACvB,IAAIA,OAAc,SAAS,cAAc;EAAE,UAAU;EAAQ,QAAQ;EAAM,CAAC;CAGvF,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;EACA,CAAC;AAED,EAAC,MAAM,WAAW,EAAE,QAAQ;AAE7B,QAAO,SAAS,WAAW"}
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 { previewCookieAttributes } from \"./lib/previewCookieAttributes\"\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 cookieAttributes = previewCookieAttributes(request.headers.get(\"host\"))\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tcookieJar.set(prismicCookie.preview, previewToken, cookieAttributes)\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 writes its Draft Mode cookie as `SameSite=Lax` in development,\n\t// which cross-site iframes drop. Rewrite it with the preview cookie's\n\t// attributes, keeping Next.js's value.\n\tconst draftCookie = cookieJar.get(\"__prerender_bypass\")\n\tif (draftCookie) {\n\t\tcookieJar.set({ ...draftCookie, ...cookieAttributes })\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,aAAa,QAAQ,QAAQ,aAAa,IAAI,aAAa,IAAI,KAAA;CAMrE,MAAM,YAAY,MAAM,SAAS;CACjC,MAAM,mBAAmB,wBAAwB,QAAQ,QAAQ,IAAI,OAAO,CAAC;CAC7E,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,QAAQ,IAAI,KAAA;AAClE,KAAI,aACH,WAAU,IAAIA,OAAc,SAAS,cAAc,iBAAiB;CAGrE,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;EACA,CAAC;AAED,EAAC,MAAM,WAAW,EAAE,QAAQ;CAK7B,MAAM,cAAc,UAAU,IAAI,qBAAqB;AACvD,KAAI,YACH,WAAU,IAAI;EAAE,GAAG;EAAa,GAAG;EAAkB,CAAC;AAGvD,QAAO,SAAS,WAAW"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/next",
3
- "version": "2.3.1-pr.148.a101ed4",
3
+ "version": "2.3.1-pr.148.d5dbc1a",
4
4
  "description": "Helpers to integrate Prismic into Next.js apps",
5
5
  "keywords": [
6
6
  "next",
@@ -1,5 +1,7 @@
1
1
  import { cookie as prismicCookie } from "@prismicio/client"
2
2
 
3
+ import { previewCookieAttributes } from "./lib/previewCookieAttributes"
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.
@@ -19,14 +21,17 @@ export async function exitPreview(): Promise<Response> {
19
21
  // Need this to avoid the following Next.js build-time error:
20
22
  // You're importing a component that needs next/headers. That only works
21
23
  // in a Server Component which is not supported in the pages/ directory.
22
- const { cookies, draftMode } = await import("next/headers")
24
+ const { cookies, draftMode, headers } = await import("next/headers")
23
25
 
24
26
  ;(await draftMode()).disable()
25
27
 
26
- // `redirectToPreviewURL` writes the preview cookie, so `exitPreview`
27
- // clears it to close the preview-cookie loop. The attributes must match
28
- // the write: a cross-site iframe can only clear a `SameSite=None` cookie.
29
- ;(await cookies()).delete({ name: prismicCookie.preview, sameSite: "none", secure: true })
28
+ // Clear the cookies `redirectToPreviewURL` wrote with the attributes it
29
+ // used: a cross-site iframe can only clear `SameSite=None` cookies, which
30
+ // `draftMode().disable()` does not use in development.
31
+ const cookieJar = await cookies()
32
+ const cookieAttributes = previewCookieAttributes((await headers()).get("host"))
33
+ cookieJar.delete({ name: "__prerender_bypass", httpOnly: true, ...cookieAttributes })
34
+ cookieJar.delete({ name: prismicCookie.preview, ...cookieAttributes })
30
35
 
31
36
  // `Cache-Control` header is used to prevent CDN-level caching.
32
37
  return new Response(JSON.stringify({ success: true }), {
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Attributes for the preview cookie and Next.js's Draft Mode cookie.
3
+ *
4
+ * Browsers only send `SameSite=Lax` cookies on same-site requests, so the cookies need
5
+ * `SameSite=None; Secure` to work when the site is embedded in a cross-site iframe, like the
6
+ * Prismic editor. Browsers reject `Secure` cookies over plain HTTP unless the host is `localhost`,
7
+ * so `next dev` on any other host keeps Next.js's `Lax` default.
8
+ */
9
+ export function previewCookieAttributes(host: string | null | undefined): {
10
+ sameSite: "none" | "lax"
11
+ secure: boolean
12
+ } {
13
+ const secure = process.env.NODE_ENV !== "development" || host?.split(":")[0] === "localhost"
14
+
15
+ return { sameSite: secure ? "none" : "lax", secure }
16
+ }
@@ -1,5 +1,6 @@
1
1
  import { cookie as prismicCookie, type Client, type LinkResolverFunction } from "@prismicio/client"
2
2
 
3
+ import { previewCookieAttributes } from "./lib/previewCookieAttributes"
3
4
  import type { NextRequestLike } from "./types"
4
5
 
5
6
  export type RedirectToPreviewURLConfig = {
@@ -50,13 +51,11 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
50
51
  // to support unpublished previews. Without setting it here, the page
51
52
  // will try to render without the preview cookie, leading to a
52
53
  // PrismicNotFound error.
53
- //
54
- // `SameSite=None` lets browsers send the cookie when the site is embedded
55
- // in a cross-site iframe, like the Prismic editor.
54
+ const cookieJar = await cookies()
55
+ const cookieAttributes = previewCookieAttributes(request.headers.get("host"))
56
56
  const previewToken = request.nextUrl.searchParams.get("token") ?? undefined
57
57
  if (previewToken) {
58
- const cookieJar = await cookies()
59
- cookieJar.set(prismicCookie.preview, previewToken, { sameSite: "none", secure: true })
58
+ cookieJar.set(prismicCookie.preview, previewToken, cookieAttributes)
60
59
  }
61
60
 
62
61
  const previewURL = await client.resolvePreviewURL({
@@ -68,5 +67,13 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
68
67
 
69
68
  ;(await draftMode()).enable()
70
69
 
70
+ // Next.js writes its Draft Mode cookie as `SameSite=Lax` in development,
71
+ // which cross-site iframes drop. Rewrite it with the preview cookie's
72
+ // attributes, keeping Next.js's value.
73
+ const draftCookie = cookieJar.get("__prerender_bypass")
74
+ if (draftCookie) {
75
+ cookieJar.set({ ...draftCookie, ...cookieAttributes })
76
+ }
77
+
71
78
  return redirect(previewURL)
72
79
  }