@prismicio/next 2.3.1-pr.146.a87f9e7 → 2.3.1-pr.147.1659168
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.
- package/dist/PrismicNextLink.d.cts +3 -3
- package/dist/PrismicNextLink.d.ts +3 -3
- package/dist/exitPreview.cjs +4 -6
- package/dist/exitPreview.cjs.map +1 -1
- package/dist/exitPreview.d.cts.map +1 -1
- package/dist/exitPreview.d.ts.map +1 -1
- package/dist/exitPreview.js +4 -5
- package/dist/exitPreview.js.map +1 -1
- package/dist/getPreviewRef.cjs +19 -2
- package/dist/getPreviewRef.cjs.map +1 -1
- package/dist/getPreviewRef.js +19 -2
- package/dist/getPreviewRef.js.map +1 -1
- package/dist/lib/expiredPreviewCookies.cjs +27 -0
- package/dist/lib/expiredPreviewCookies.cjs.map +1 -0
- package/dist/lib/expiredPreviewCookies.js +25 -0
- package/dist/lib/expiredPreviewCookies.js.map +1 -0
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/pages/exitPreview.cjs +4 -0
- package/dist/pages/exitPreview.cjs.map +1 -1
- package/dist/pages/exitPreview.d.cts.map +1 -1
- package/dist/pages/exitPreview.d.ts.map +1 -1
- package/dist/pages/exitPreview.js +4 -0
- package/dist/pages/exitPreview.js.map +1 -1
- package/dist/pages/types.d.cts +1 -0
- package/dist/pages/types.d.cts.map +1 -1
- package/dist/pages/types.d.ts +1 -0
- package/dist/pages/types.d.ts.map +1 -1
- package/dist/redirectToPreviewURL.cjs +6 -1
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.js +6 -1
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/package.json +1 -1
- package/src/exitPreview.ts +16 -17
- package/src/getPreviewRef.ts +36 -5
- package/src/lib/expiredPreviewCookies.ts +32 -0
- package/src/pages/exitPreview.ts +21 -0
- package/src/pages/types.ts +1 -0
- package/src/redirectToPreviewURL.ts +11 -1
|
@@ -23,7 +23,7 @@ type PrismicNextLinkProps = Omit<ComponentProps<typeof Link>, "field" | "documen
|
|
|
23
23
|
});
|
|
24
24
|
declare const PrismicNextLink: _$react.ForwardRefExoticComponent<(Omit<Omit<Omit<_$react.AnchorHTMLAttributes<HTMLAnchorElement>, keyof _$next_link0.LinkProps<any>> & _$next_link0.LinkProps<any> & {
|
|
25
25
|
children?: React.ReactNode | undefined;
|
|
26
|
-
} & _$react.RefAttributes<HTMLAnchorElement>, "
|
|
26
|
+
} & _$react.RefAttributes<HTMLAnchorElement>, "href" | "rel" | "field" | "document"> & {
|
|
27
27
|
linkResolver?: LinkResolverFunction;
|
|
28
28
|
rel?: string | AsLinkAttrsConfig["rel"];
|
|
29
29
|
} & {
|
|
@@ -32,7 +32,7 @@ declare const PrismicNextLink: _$react.ForwardRefExoticComponent<(Omit<Omit<Omit
|
|
|
32
32
|
href?: never;
|
|
33
33
|
}, "ref"> | Omit<Omit<Omit<_$react.AnchorHTMLAttributes<HTMLAnchorElement>, keyof _$next_link0.LinkProps<any>> & _$next_link0.LinkProps<any> & {
|
|
34
34
|
children?: React.ReactNode | undefined;
|
|
35
|
-
} & _$react.RefAttributes<HTMLAnchorElement>, "
|
|
35
|
+
} & _$react.RefAttributes<HTMLAnchorElement>, "href" | "rel" | "field" | "document"> & {
|
|
36
36
|
linkResolver?: LinkResolverFunction;
|
|
37
37
|
rel?: string | AsLinkAttrsConfig["rel"];
|
|
38
38
|
} & {
|
|
@@ -41,7 +41,7 @@ declare const PrismicNextLink: _$react.ForwardRefExoticComponent<(Omit<Omit<Omit
|
|
|
41
41
|
href?: never;
|
|
42
42
|
}, "ref"> | Omit<Omit<Omit<_$react.AnchorHTMLAttributes<HTMLAnchorElement>, keyof _$next_link0.LinkProps<any>> & _$next_link0.LinkProps<any> & {
|
|
43
43
|
children?: React.ReactNode | undefined;
|
|
44
|
-
} & _$react.RefAttributes<HTMLAnchorElement>, "
|
|
44
|
+
} & _$react.RefAttributes<HTMLAnchorElement>, "href" | "rel" | "field" | "document"> & {
|
|
45
45
|
linkResolver?: LinkResolverFunction;
|
|
46
46
|
rel?: string | AsLinkAttrsConfig["rel"];
|
|
47
47
|
} & {
|
|
@@ -23,7 +23,7 @@ type PrismicNextLinkProps = Omit<ComponentProps<typeof Link>, "field" | "documen
|
|
|
23
23
|
});
|
|
24
24
|
declare const PrismicNextLink: _$react.ForwardRefExoticComponent<(Omit<Omit<Omit<_$react.AnchorHTMLAttributes<HTMLAnchorElement>, keyof _$next_link0.LinkProps<any>> & _$next_link0.LinkProps<any> & {
|
|
25
25
|
children?: React.ReactNode | undefined;
|
|
26
|
-
} & _$react.RefAttributes<HTMLAnchorElement>, "
|
|
26
|
+
} & _$react.RefAttributes<HTMLAnchorElement>, "href" | "rel" | "field" | "document"> & {
|
|
27
27
|
linkResolver?: LinkResolverFunction;
|
|
28
28
|
rel?: string | AsLinkAttrsConfig["rel"];
|
|
29
29
|
} & {
|
|
@@ -32,7 +32,7 @@ declare const PrismicNextLink: _$react.ForwardRefExoticComponent<(Omit<Omit<Omit
|
|
|
32
32
|
href?: never;
|
|
33
33
|
}, "ref"> | Omit<Omit<Omit<_$react.AnchorHTMLAttributes<HTMLAnchorElement>, keyof _$next_link0.LinkProps<any>> & _$next_link0.LinkProps<any> & {
|
|
34
34
|
children?: React.ReactNode | undefined;
|
|
35
|
-
} & _$react.RefAttributes<HTMLAnchorElement>, "
|
|
35
|
+
} & _$react.RefAttributes<HTMLAnchorElement>, "href" | "rel" | "field" | "document"> & {
|
|
36
36
|
linkResolver?: LinkResolverFunction;
|
|
37
37
|
rel?: string | AsLinkAttrsConfig["rel"];
|
|
38
38
|
} & {
|
|
@@ -41,7 +41,7 @@ declare const PrismicNextLink: _$react.ForwardRefExoticComponent<(Omit<Omit<Omit
|
|
|
41
41
|
href?: never;
|
|
42
42
|
}, "ref"> | Omit<Omit<Omit<_$react.AnchorHTMLAttributes<HTMLAnchorElement>, keyof _$next_link0.LinkProps<any>> & _$next_link0.LinkProps<any> & {
|
|
43
43
|
children?: React.ReactNode | undefined;
|
|
44
|
-
} & _$react.RefAttributes<HTMLAnchorElement>, "
|
|
44
|
+
} & _$react.RefAttributes<HTMLAnchorElement>, "href" | "rel" | "field" | "document"> & {
|
|
45
45
|
linkResolver?: LinkResolverFunction;
|
|
46
46
|
rel?: string | AsLinkAttrsConfig["rel"];
|
|
47
47
|
} & {
|
package/dist/exitPreview.cjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
require("./
|
|
2
|
-
let _prismicio_client = require("@prismicio/client");
|
|
1
|
+
const require_expiredPreviewCookies = require("./lib/expiredPreviewCookies.cjs");
|
|
3
2
|
//#region src/exitPreview.ts
|
|
4
3
|
/**
|
|
5
4
|
* Ends a Prismic preview session within a Next.js app. This function should be used in a Router
|
|
@@ -17,10 +16,9 @@ let _prismicio_client = require("@prismicio/client");
|
|
|
17
16
|
* ```
|
|
18
17
|
*/
|
|
19
18
|
async function exitPreview() {
|
|
20
|
-
const
|
|
21
|
-
(
|
|
22
|
-
|
|
23
|
-
return new Response(JSON.stringify({ success: true }), { headers: { "Cache-Control": "no-store" } });
|
|
19
|
+
const headers = new Headers({ "Cache-Control": "no-store" });
|
|
20
|
+
for (const setCookie of [...require_expiredPreviewCookies.expiredPreviewCookieHeaders(), ...require_expiredPreviewCookies.expiredDraftModeCookieHeaders()]) headers.append("Set-Cookie", setCookie);
|
|
21
|
+
return new Response(JSON.stringify({ success: true }), { headers });
|
|
24
22
|
}
|
|
25
23
|
//#endregion
|
|
26
24
|
exports.exitPreview = exitPreview;
|
package/dist/exitPreview.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.cjs","names":["
|
|
1
|
+
{"version":3,"file":"exitPreview.cjs","names":["expiredPreviewCookieHeaders","expiredDraftModeCookieHeaders"],"sources":["../src/exitPreview.ts"],"sourcesContent":["import {\n\texpiredDraftModeCookieHeaders,\n\texpiredPreviewCookieHeaders,\n} from \"./lib/expiredPreviewCookies\"\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// Do not call `cookies()` or `draftMode().disable()`. ResponseCookies is\n\t// name-keyed: `appendMutableCookies` rebuilds Set-Cookie by name and drops\n\t// the extra Lax expire. Emit every expire on the Response instead:\n\t// toolbar Lax + iframe None; Secure for `io.prismic.preview`, and both\n\t// shapes of `__prerender_bypass` that `disable()` would have cleared.\n\tconst headers = new Headers({\n\t\t\"Cache-Control\": \"no-store\",\n\t})\n\tfor (const setCookie of [...expiredPreviewCookieHeaders(), ...expiredDraftModeCookieHeaders()]) {\n\t\theaders.append(\"Set-Cookie\", setCookie)\n\t}\n\n\treturn new Response(JSON.stringify({ success: true }), { headers })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAoBA,eAAsB,cAAiC;CAMtD,MAAM,UAAU,IAAI,QAAQ,EAC3B,iBAAiB,YACjB,CAAC;AACF,MAAK,MAAM,aAAa,CAAC,GAAGA,8BAAAA,6BAA6B,EAAE,GAAGC,8BAAAA,+BAA+B,CAAC,CAC7F,SAAQ,OAAO,cAAc,UAAU;AAGxC,QAAO,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,MAAM,CAAC,EAAE,EAAE,SAAS,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;AAoBA;;;;;;;;;;;;;;iBAAsB,WAAA,CAAA,GAAe,OAAA,CAAQ,QAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;AAoBA;;;;;;;;;;;;;;iBAAsB,WAAA,CAAA,GAAe,OAAA,CAAQ,QAAA"}
|
package/dist/exitPreview.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { expiredDraftModeCookieHeaders, expiredPreviewCookieHeaders } from "./lib/expiredPreviewCookies.js";
|
|
2
2
|
//#region src/exitPreview.ts
|
|
3
3
|
/**
|
|
4
4
|
* Ends a Prismic preview session within a Next.js app. This function should be used in a Router
|
|
@@ -16,10 +16,9 @@ import { cookie } from "@prismicio/client";
|
|
|
16
16
|
* ```
|
|
17
17
|
*/
|
|
18
18
|
async function exitPreview() {
|
|
19
|
-
const
|
|
20
|
-
(
|
|
21
|
-
|
|
22
|
-
return new Response(JSON.stringify({ success: true }), { headers: { "Cache-Control": "no-store" } });
|
|
19
|
+
const headers = new Headers({ "Cache-Control": "no-store" });
|
|
20
|
+
for (const setCookie of [...expiredPreviewCookieHeaders(), ...expiredDraftModeCookieHeaders()]) headers.append("Set-Cookie", setCookie);
|
|
21
|
+
return new Response(JSON.stringify({ success: true }), { headers });
|
|
23
22
|
}
|
|
24
23
|
//#endregion
|
|
25
24
|
export { exitPreview };
|
package/dist/exitPreview.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.js","names":[
|
|
1
|
+
{"version":3,"file":"exitPreview.js","names":[],"sources":["../src/exitPreview.ts"],"sourcesContent":["import {\n\texpiredDraftModeCookieHeaders,\n\texpiredPreviewCookieHeaders,\n} from \"./lib/expiredPreviewCookies\"\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// Do not call `cookies()` or `draftMode().disable()`. ResponseCookies is\n\t// name-keyed: `appendMutableCookies` rebuilds Set-Cookie by name and drops\n\t// the extra Lax expire. Emit every expire on the Response instead:\n\t// toolbar Lax + iframe None; Secure for `io.prismic.preview`, and both\n\t// shapes of `__prerender_bypass` that `disable()` would have cleared.\n\tconst headers = new Headers({\n\t\t\"Cache-Control\": \"no-store\",\n\t})\n\tfor (const setCookie of [...expiredPreviewCookieHeaders(), ...expiredDraftModeCookieHeaders()]) {\n\t\theaders.append(\"Set-Cookie\", setCookie)\n\t}\n\n\treturn new Response(JSON.stringify({ success: true }), { headers })\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAoBA,eAAsB,cAAiC;CAMtD,MAAM,UAAU,IAAI,QAAQ,EAC3B,iBAAiB,YACjB,CAAC;AACF,MAAK,MAAM,aAAa,CAAC,GAAG,6BAA6B,EAAE,GAAG,+BAA+B,CAAC,CAC7F,SAAQ,OAAO,cAAc,UAAU;AAGxC,QAAO,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,MAAM,CAAC,EAAE,EAAE,SAAS,CAAC"}
|
package/dist/getPreviewRef.cjs
CHANGED
|
@@ -29,8 +29,25 @@ async function getPreviewRef() {
|
|
|
29
29
|
if (!isDraftModeEnabled) return;
|
|
30
30
|
const cookie = (await cookies()).get(_prismicio_client.cookie.preview)?.value;
|
|
31
31
|
if (!cookie) return;
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
return previewRefFromCookie(cookie);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Unwraps `io.prismic.preview` to a Content API ref.
|
|
36
|
+
*
|
|
37
|
+
* After toolbar `PreviewCookie.sync` / `upsertPreviewForDomain`, the cookie is
|
|
38
|
+
* `{ [domain]: { preview: string } }`. Returning that jar makes `enableAutoPreviews`
|
|
39
|
+
* / Cache Components send a non-ref (ParsingError / 404).
|
|
40
|
+
*/
|
|
41
|
+
function previewRefFromCookie(cookie) {
|
|
42
|
+
try {
|
|
43
|
+
const parsed = JSON.parse(cookie);
|
|
44
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return;
|
|
45
|
+
const previewRefs = [];
|
|
46
|
+
for (const value of Object.values(parsed)) if (value !== null && typeof value === "object" && !Array.isArray(value) && "preview" in value && typeof value.preview === "string") previewRefs.push(value.preview);
|
|
47
|
+
return previewRefs.length === 1 ? previewRefs[0] : void 0;
|
|
48
|
+
} catch {
|
|
49
|
+
return cookie;
|
|
50
|
+
}
|
|
34
51
|
}
|
|
35
52
|
//#endregion
|
|
36
53
|
exports.getPreviewRef = getPreviewRef;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPreviewRef.cjs","names":["prismicCookie"],"sources":["../src/getPreviewRef.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\n/**\n * Reads the Prismic preview ref for the current request when an active preview session exists.\n *\n * This is the read-side counterpart to `redirectToPreviewURL`, which writes the preview cookie. Use\n * it with Next.js Cache Components to read the ref _outside_ a cached function and pass it _in_ as\n * an argument so it becomes part of the cache key:\n *\n * @example\n * \t;```typescript\n * \timport { getPreviewRef } from \"@prismicio/next\"\n *\n * \tconst page = await fetchPage(uid, await getPreviewRef())\n * \t```\n *\n * @returns The active preview ref, or `undefined` if no preview session is active.\n * @experimental\n */\nexport async function getPreviewRef(): Promise<string | undefined> {\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\tlet isDraftModeEnabled = false\n\ttry {\n\t\tisDraftModeEnabled = (await draftMode()).isEnabled\n\t} catch {\n\t\t// `draftMode()` may have been called in a place that\n\t\t// does not have access to its async storage. This\n\t\t// occurs in places like `generateStaticParams()`. We\n\t\t// can ignore this case.\n\t\treturn\n\t}\n\tif (!isDraftModeEnabled) {\n\t\treturn\n\t}\n\n\tconst cookie = (await cookies()).get(prismicCookie.preview)?.value\n\tif (!cookie) {\n\t\treturn\n\t}\n\n\tconst
|
|
1
|
+
{"version":3,"file":"getPreviewRef.cjs","names":["prismicCookie"],"sources":["../src/getPreviewRef.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\n/**\n * Reads the Prismic preview ref for the current request when an active preview session exists.\n *\n * This is the read-side counterpart to `redirectToPreviewURL`, which writes the preview cookie. Use\n * it with Next.js Cache Components to read the ref _outside_ a cached function and pass it _in_ as\n * an argument so it becomes part of the cache key:\n *\n * @example\n * \t;```typescript\n * \timport { getPreviewRef } from \"@prismicio/next\"\n *\n * \tconst page = await fetchPage(uid, await getPreviewRef())\n * \t```\n *\n * @returns The active preview ref, or `undefined` if no preview session is active.\n * @experimental\n */\nexport async function getPreviewRef(): Promise<string | undefined> {\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\tlet isDraftModeEnabled = false\n\ttry {\n\t\tisDraftModeEnabled = (await draftMode()).isEnabled\n\t} catch {\n\t\t// `draftMode()` may have been called in a place that\n\t\t// does not have access to its async storage. This\n\t\t// occurs in places like `generateStaticParams()`. We\n\t\t// can ignore this case.\n\t\treturn\n\t}\n\tif (!isDraftModeEnabled) {\n\t\treturn\n\t}\n\n\tconst cookie = (await cookies()).get(prismicCookie.preview)?.value\n\tif (!cookie) {\n\t\treturn\n\t}\n\n\t// Draft Mode gates preview reads. The cookie may be a raw token or the\n\t// toolbar jar (`{ \"<repo>.prismic.io\": { preview } }`). Only `preview` is a\n\t// Content API ref; the jar string is not.\n\treturn previewRefFromCookie(cookie)\n}\n\n/**\n * Unwraps `io.prismic.preview` to a Content API ref.\n *\n * After toolbar `PreviewCookie.sync` / `upsertPreviewForDomain`, the cookie is\n * `{ [domain]: { preview: string } }`. Returning that jar makes `enableAutoPreviews`\n * / Cache Components send a non-ref (ParsingError / 404).\n */\nfunction previewRefFromCookie(cookie: string): string | undefined {\n\ttry {\n\t\tconst parsed: unknown = JSON.parse(cookie)\n\t\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n\t\t\treturn\n\t\t}\n\n\t\tconst previewRefs: string[] = []\n\t\tfor (const value of Object.values(parsed)) {\n\t\t\tif (\n\t\t\t\tvalue !== null &&\n\t\t\t\ttypeof value === \"object\" &&\n\t\t\t\t!Array.isArray(value) &&\n\t\t\t\t\"preview\" in value &&\n\t\t\t\ttypeof value.preview === \"string\"\n\t\t\t) {\n\t\t\t\tpreviewRefs.push(value.preview)\n\t\t\t}\n\t\t}\n\n\t\treturn previewRefs.length === 1 ? previewRefs[0] : undefined\n\t} catch {\n\t\treturn cookie\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAmBA,eAAsB,gBAA6C;CAIlE,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;CAE5C,IAAI,qBAAqB;AACzB,KAAI;AACH,wBAAsB,MAAM,WAAW,EAAE;SAClC;AAKP;;AAED,KAAI,CAAC,mBACJ;CAGD,MAAM,UAAU,MAAM,SAAS,EAAE,IAAIA,kBAAAA,OAAc,QAAQ,EAAE;AAC7D,KAAI,CAAC,OACJ;AAMD,QAAO,qBAAqB,OAAO;;;;;;;;;AAUpC,SAAS,qBAAqB,QAAoC;AACjE,KAAI;EACH,MAAM,SAAkB,KAAK,MAAM,OAAO;AAC1C,MAAI,WAAW,QAAQ,OAAO,WAAW,YAAY,MAAM,QAAQ,OAAO,CACzE;EAGD,MAAM,cAAwB,EAAE;AAChC,OAAK,MAAM,SAAS,OAAO,OAAO,OAAO,CACxC,KACC,UAAU,QACV,OAAO,UAAU,YACjB,CAAC,MAAM,QAAQ,MAAM,IACrB,aAAa,SACb,OAAO,MAAM,YAAY,SAEzB,aAAY,KAAK,MAAM,QAAQ;AAIjC,SAAO,YAAY,WAAW,IAAI,YAAY,KAAK,KAAA;SAC5C;AACP,SAAO"}
|
package/dist/getPreviewRef.js
CHANGED
|
@@ -28,8 +28,25 @@ async function getPreviewRef() {
|
|
|
28
28
|
if (!isDraftModeEnabled) return;
|
|
29
29
|
const cookie$1 = (await cookies()).get(cookie.preview)?.value;
|
|
30
30
|
if (!cookie$1) return;
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
return previewRefFromCookie(cookie$1);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Unwraps `io.prismic.preview` to a Content API ref.
|
|
35
|
+
*
|
|
36
|
+
* After toolbar `PreviewCookie.sync` / `upsertPreviewForDomain`, the cookie is
|
|
37
|
+
* `{ [domain]: { preview: string } }`. Returning that jar makes `enableAutoPreviews`
|
|
38
|
+
* / Cache Components send a non-ref (ParsingError / 404).
|
|
39
|
+
*/
|
|
40
|
+
function previewRefFromCookie(cookie) {
|
|
41
|
+
try {
|
|
42
|
+
const parsed = JSON.parse(cookie);
|
|
43
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return;
|
|
44
|
+
const previewRefs = [];
|
|
45
|
+
for (const value of Object.values(parsed)) if (value !== null && typeof value === "object" && !Array.isArray(value) && "preview" in value && typeof value.preview === "string") previewRefs.push(value.preview);
|
|
46
|
+
return previewRefs.length === 1 ? previewRefs[0] : void 0;
|
|
47
|
+
} catch {
|
|
48
|
+
return cookie;
|
|
49
|
+
}
|
|
33
50
|
}
|
|
34
51
|
//#endregion
|
|
35
52
|
export { getPreviewRef };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPreviewRef.js","names":["cookie","prismicCookie"],"sources":["../src/getPreviewRef.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\n/**\n * Reads the Prismic preview ref for the current request when an active preview session exists.\n *\n * This is the read-side counterpart to `redirectToPreviewURL`, which writes the preview cookie. Use\n * it with Next.js Cache Components to read the ref _outside_ a cached function and pass it _in_ as\n * an argument so it becomes part of the cache key:\n *\n * @example\n * \t;```typescript\n * \timport { getPreviewRef } from \"@prismicio/next\"\n *\n * \tconst page = await fetchPage(uid, await getPreviewRef())\n * \t```\n *\n * @returns The active preview ref, or `undefined` if no preview session is active.\n * @experimental\n */\nexport async function getPreviewRef(): Promise<string | undefined> {\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\tlet isDraftModeEnabled = false\n\ttry {\n\t\tisDraftModeEnabled = (await draftMode()).isEnabled\n\t} catch {\n\t\t// `draftMode()` may have been called in a place that\n\t\t// does not have access to its async storage. This\n\t\t// occurs in places like `generateStaticParams()`. We\n\t\t// can ignore this case.\n\t\treturn\n\t}\n\tif (!isDraftModeEnabled) {\n\t\treturn\n\t}\n\n\tconst cookie = (await cookies()).get(prismicCookie.preview)?.value\n\tif (!cookie) {\n\t\treturn\n\t}\n\n\tconst
|
|
1
|
+
{"version":3,"file":"getPreviewRef.js","names":["cookie","prismicCookie"],"sources":["../src/getPreviewRef.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\n/**\n * Reads the Prismic preview ref for the current request when an active preview session exists.\n *\n * This is the read-side counterpart to `redirectToPreviewURL`, which writes the preview cookie. Use\n * it with Next.js Cache Components to read the ref _outside_ a cached function and pass it _in_ as\n * an argument so it becomes part of the cache key:\n *\n * @example\n * \t;```typescript\n * \timport { getPreviewRef } from \"@prismicio/next\"\n *\n * \tconst page = await fetchPage(uid, await getPreviewRef())\n * \t```\n *\n * @returns The active preview ref, or `undefined` if no preview session is active.\n * @experimental\n */\nexport async function getPreviewRef(): Promise<string | undefined> {\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\tlet isDraftModeEnabled = false\n\ttry {\n\t\tisDraftModeEnabled = (await draftMode()).isEnabled\n\t} catch {\n\t\t// `draftMode()` may have been called in a place that\n\t\t// does not have access to its async storage. This\n\t\t// occurs in places like `generateStaticParams()`. We\n\t\t// can ignore this case.\n\t\treturn\n\t}\n\tif (!isDraftModeEnabled) {\n\t\treturn\n\t}\n\n\tconst cookie = (await cookies()).get(prismicCookie.preview)?.value\n\tif (!cookie) {\n\t\treturn\n\t}\n\n\t// Draft Mode gates preview reads. The cookie may be a raw token or the\n\t// toolbar jar (`{ \"<repo>.prismic.io\": { preview } }`). Only `preview` is a\n\t// Content API ref; the jar string is not.\n\treturn previewRefFromCookie(cookie)\n}\n\n/**\n * Unwraps `io.prismic.preview` to a Content API ref.\n *\n * After toolbar `PreviewCookie.sync` / `upsertPreviewForDomain`, the cookie is\n * `{ [domain]: { preview: string } }`. Returning that jar makes `enableAutoPreviews`\n * / Cache Components send a non-ref (ParsingError / 404).\n */\nfunction previewRefFromCookie(cookie: string): string | undefined {\n\ttry {\n\t\tconst parsed: unknown = JSON.parse(cookie)\n\t\tif (parsed === null || typeof parsed !== \"object\" || Array.isArray(parsed)) {\n\t\t\treturn\n\t\t}\n\n\t\tconst previewRefs: string[] = []\n\t\tfor (const value of Object.values(parsed)) {\n\t\t\tif (\n\t\t\t\tvalue !== null &&\n\t\t\t\ttypeof value === \"object\" &&\n\t\t\t\t!Array.isArray(value) &&\n\t\t\t\t\"preview\" in value &&\n\t\t\t\ttypeof value.preview === \"string\"\n\t\t\t) {\n\t\t\t\tpreviewRefs.push(value.preview)\n\t\t\t}\n\t\t}\n\n\t\treturn previewRefs.length === 1 ? previewRefs[0] : undefined\n\t} catch {\n\t\treturn cookie\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAmBA,eAAsB,gBAA6C;CAIlE,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;CAE5C,IAAI,qBAAqB;AACzB,KAAI;AACH,wBAAsB,MAAM,WAAW,EAAE;SAClC;AAKP;;AAED,KAAI,CAAC,mBACJ;CAGD,MAAMA,YAAU,MAAM,SAAS,EAAE,IAAIC,OAAc,QAAQ,EAAE;AAC7D,KAAI,CAACD,SACJ;AAMD,QAAO,qBAAqBA,SAAO;;;;;;;;;AAUpC,SAAS,qBAAqB,QAAoC;AACjE,KAAI;EACH,MAAM,SAAkB,KAAK,MAAM,OAAO;AAC1C,MAAI,WAAW,QAAQ,OAAO,WAAW,YAAY,MAAM,QAAQ,OAAO,CACzE;EAGD,MAAM,cAAwB,EAAE;AAChC,OAAK,MAAM,SAAS,OAAO,OAAO,OAAO,CACxC,KACC,UAAU,QACV,OAAO,UAAU,YACjB,CAAC,MAAM,QAAQ,MAAM,IACrB,aAAa,SACb,OAAO,MAAM,YAAY,SAEzB,aAAY,KAAK,MAAM,QAAQ;AAIjC,SAAO,YAAY,WAAW,IAAI,YAAY,KAAK,KAAA;SAC5C;AACP,SAAO"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
let _prismicio_client = require("@prismicio/client");
|
|
3
|
+
//#region src/lib/expiredPreviewCookies.ts
|
|
4
|
+
const epoch = /* @__PURE__ */ new Date(0);
|
|
5
|
+
function expiresAttr() {
|
|
6
|
+
return `Expires=${epoch.toUTCString()}`;
|
|
7
|
+
}
|
|
8
|
+
/** Expired `Set-Cookie` values for both leftover `io.prismic.preview` shapes. */
|
|
9
|
+
function expiredPreviewCookieHeaders(name = _prismicio_client.cookie.preview) {
|
|
10
|
+
const expires = expiresAttr();
|
|
11
|
+
return [`${name}=; Path=/; ${expires}; SameSite=Lax`, `${name}=; Path=/; ${expires}; SameSite=None; Secure`];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Expired `Set-Cookie` values for the App Router draft-mode cookie that `draftMode().disable()`
|
|
15
|
+
* would clear. Next sets `__prerender_bypass` as Lax (not Secure) in development and SameSite=None;
|
|
16
|
+
* Secure in production.
|
|
17
|
+
*/
|
|
18
|
+
function expiredDraftModeCookieHeaders() {
|
|
19
|
+
const expires = expiresAttr();
|
|
20
|
+
const name = "__prerender_bypass";
|
|
21
|
+
return [`${name}=; Path=/; ${expires}; SameSite=Lax; HttpOnly`, `${name}=; Path=/; ${expires}; SameSite=None; Secure; HttpOnly`];
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
exports.expiredDraftModeCookieHeaders = expiredDraftModeCookieHeaders;
|
|
25
|
+
exports.expiredPreviewCookieHeaders = expiredPreviewCookieHeaders;
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=expiredPreviewCookies.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expiredPreviewCookies.cjs","names":["prismicCookie"],"sources":["../../src/lib/expiredPreviewCookies.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\nconst epoch = new Date(0)\n\nfunction expiresAttr(): string {\n\treturn `Expires=${epoch.toUTCString()}`\n}\n\n/** Expired `Set-Cookie` values for both leftover `io.prismic.preview` shapes. */\nexport function expiredPreviewCookieHeaders(name = prismicCookie.preview): string[] {\n\tconst expires = expiresAttr()\n\n\treturn [\n\t\t`${name}=; Path=/; ${expires}; SameSite=Lax`,\n\t\t`${name}=; Path=/; ${expires}; SameSite=None; Secure`,\n\t]\n}\n\n/**\n * Expired `Set-Cookie` values for the App Router draft-mode cookie that `draftMode().disable()`\n * would clear. Next sets `__prerender_bypass` as Lax (not Secure) in development and SameSite=None;\n * Secure in production.\n */\nexport function expiredDraftModeCookieHeaders(): string[] {\n\tconst expires = expiresAttr()\n\tconst name = \"__prerender_bypass\"\n\n\treturn [\n\t\t`${name}=; Path=/; ${expires}; SameSite=Lax; HttpOnly`,\n\t\t`${name}=; Path=/; ${expires}; SameSite=None; Secure; HttpOnly`,\n\t]\n}\n"],"mappings":";;;AAEA,MAAM,wBAAQ,IAAI,KAAK,EAAE;AAEzB,SAAS,cAAsB;AAC9B,QAAO,WAAW,MAAM,aAAa;;;AAItC,SAAgB,4BAA4B,OAAOA,kBAAAA,OAAc,SAAmB;CACnF,MAAM,UAAU,aAAa;AAE7B,QAAO,CACN,GAAG,KAAK,aAAa,QAAQ,iBAC7B,GAAG,KAAK,aAAa,QAAQ,yBAC7B;;;;;;;AAQF,SAAgB,gCAA0C;CACzD,MAAM,UAAU,aAAa;CAC7B,MAAM,OAAO;AAEb,QAAO,CACN,GAAG,KAAK,aAAa,QAAQ,2BAC7B,GAAG,KAAK,aAAa,QAAQ,mCAC7B"}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { cookie } from "@prismicio/client";
|
|
2
|
+
//#region src/lib/expiredPreviewCookies.ts
|
|
3
|
+
const epoch = /* @__PURE__ */ new Date(0);
|
|
4
|
+
function expiresAttr() {
|
|
5
|
+
return `Expires=${epoch.toUTCString()}`;
|
|
6
|
+
}
|
|
7
|
+
/** Expired `Set-Cookie` values for both leftover `io.prismic.preview` shapes. */
|
|
8
|
+
function expiredPreviewCookieHeaders(name = cookie.preview) {
|
|
9
|
+
const expires = expiresAttr();
|
|
10
|
+
return [`${name}=; Path=/; ${expires}; SameSite=Lax`, `${name}=; Path=/; ${expires}; SameSite=None; Secure`];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Expired `Set-Cookie` values for the App Router draft-mode cookie that `draftMode().disable()`
|
|
14
|
+
* would clear. Next sets `__prerender_bypass` as Lax (not Secure) in development and SameSite=None;
|
|
15
|
+
* Secure in production.
|
|
16
|
+
*/
|
|
17
|
+
function expiredDraftModeCookieHeaders() {
|
|
18
|
+
const expires = expiresAttr();
|
|
19
|
+
const name = "__prerender_bypass";
|
|
20
|
+
return [`${name}=; Path=/; ${expires}; SameSite=Lax; HttpOnly`, `${name}=; Path=/; ${expires}; SameSite=None; Secure; HttpOnly`];
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { expiredDraftModeCookieHeaders, expiredPreviewCookieHeaders };
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=expiredPreviewCookies.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"expiredPreviewCookies.js","names":["prismicCookie"],"sources":["../../src/lib/expiredPreviewCookies.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\nconst epoch = new Date(0)\n\nfunction expiresAttr(): string {\n\treturn `Expires=${epoch.toUTCString()}`\n}\n\n/** Expired `Set-Cookie` values for both leftover `io.prismic.preview` shapes. */\nexport function expiredPreviewCookieHeaders(name = prismicCookie.preview): string[] {\n\tconst expires = expiresAttr()\n\n\treturn [\n\t\t`${name}=; Path=/; ${expires}; SameSite=Lax`,\n\t\t`${name}=; Path=/; ${expires}; SameSite=None; Secure`,\n\t]\n}\n\n/**\n * Expired `Set-Cookie` values for the App Router draft-mode cookie that `draftMode().disable()`\n * would clear. Next sets `__prerender_bypass` as Lax (not Secure) in development and SameSite=None;\n * Secure in production.\n */\nexport function expiredDraftModeCookieHeaders(): string[] {\n\tconst expires = expiresAttr()\n\tconst name = \"__prerender_bypass\"\n\n\treturn [\n\t\t`${name}=; Path=/; ${expires}; SameSite=Lax; HttpOnly`,\n\t\t`${name}=; Path=/; ${expires}; SameSite=None; Secure; HttpOnly`,\n\t]\n}\n"],"mappings":";;AAEA,MAAM,wBAAQ,IAAI,KAAK,EAAE;AAEzB,SAAS,cAAsB;AAC9B,QAAO,WAAW,MAAM,aAAa;;;AAItC,SAAgB,4BAA4B,OAAOA,OAAc,SAAmB;CACnF,MAAM,UAAU,aAAa;AAE7B,QAAO,CACN,GAAG,KAAK,aAAa,QAAQ,iBAC7B,GAAG,KAAK,aAAa,QAAQ,yBAC7B;;;;;;;AAQF,SAAgB,gCAA0C;CACzD,MAAM,UAAU,aAAa;CAC7B,MAAM,OAAO;AAEb,QAAO,CACN,GAAG,KAAK,aAAa,QAAQ,2BAC7B,GAAG,KAAK,aAAa,QAAQ,mCAC7B"}
|
package/dist/package.cjs
CHANGED
package/dist/package.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
const require_expiredPreviewCookies = require("../lib/expiredPreviewCookies.cjs");
|
|
1
2
|
//#region src/pages/exitPreview.ts
|
|
2
3
|
/**
|
|
3
4
|
* Ends a Prismic preview session within a Next.js app. This function should be used in an API
|
|
@@ -16,6 +17,9 @@
|
|
|
16
17
|
*/
|
|
17
18
|
function exitPreview(config) {
|
|
18
19
|
config.res.clearPreviewData();
|
|
20
|
+
const existingSetCookie = config.res.getHeader("Set-Cookie");
|
|
21
|
+
const expiredPreviewCookies = require_expiredPreviewCookies.expiredPreviewCookieHeaders();
|
|
22
|
+
config.res.setHeader("Set-Cookie", existingSetCookie == null ? expiredPreviewCookies : [...(Array.isArray(existingSetCookie) ? existingSetCookie : [existingSetCookie]).map(String), ...expiredPreviewCookies]);
|
|
19
23
|
config.res.setHeader("Cache-Control", "no-store");
|
|
20
24
|
config.res.json({ success: true });
|
|
21
25
|
}
|
|
@@ -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":"
|
|
1
|
+
{"version":3,"file":"exitPreview.cjs","names":["expiredPreviewCookieHeaders"],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import { expiredPreviewCookieHeaders } from \"../lib/expiredPreviewCookies\"\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\n\t// `clearPreviewData()` only clears Next.js preview cookies. Expire both\n\t// leftover `io.prismic.preview` shapes (toolbar Lax, not Secure; and the\n\t// iframe SameSite=None; Secure write). If either survives,\n\t// `<PrismicPreview>` calls `start()` → `/api/preview` and preview returns.\n\t// Do not use `cookies().set` — that is the App Router helper. Do not\n\t// replace the existing Set-Cookie header from `clearPreviewData()`.\n\tconst existingSetCookie = config.res.getHeader(\"Set-Cookie\")\n\tconst expiredPreviewCookies = expiredPreviewCookieHeaders()\n\tconfig.res.setHeader(\n\t\t\"Set-Cookie\",\n\t\texistingSetCookie == null\n\t\t\t? expiredPreviewCookies\n\t\t\t: [\n\t\t\t\t\t...(Array.isArray(existingSetCookie) ? existingSetCookie : [existingSetCookie]).map(\n\t\t\t\t\t\tString,\n\t\t\t\t\t),\n\t\t\t\t\t...expiredPreviewCookies,\n\t\t\t\t],\n\t)\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":";;;;;;;;;;;;;;;;;AAmCA,SAAgB,YAAY,QAAyC;AACpE,QAAO,IAAI,kBAAkB;CAQ7B,MAAM,oBAAoB,OAAO,IAAI,UAAU,aAAa;CAC5D,MAAM,wBAAwBA,8BAAAA,6BAA6B;AAC3D,QAAO,IAAI,UACV,cACA,qBAAqB,OAClB,wBACA,CACA,IAAI,MAAM,QAAQ,kBAAkB,GAAG,oBAAoB,CAAC,kBAAkB,EAAE,IAC/E,OACA,EACD,GAAG,sBACH,CACH;AAGD,QAAO,IAAI,UAAU,iBAAiB,WAAW;AAEjD,QAAO,IAAI,KAAK,EAAE,SAAS,MAAM,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;;KAIY,yBAAA;EAAA;;;;;EAMX,GAAA,GAAM,kBAAA;EAON;;;;AAkBD;EAlBC,GAAA,EAAK,mBAAA;AAAA;;;;;;;;;;;;;;;;iBAkBU,WAAA,CAAY,MAAA,EAAQ,yBAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"exitPreview.d.ts","names":[],"sources":["../../src/pages/exitPreview.ts"],"mappings":";;;;KAIY,yBAAA;EAAA;;;;;EAMX,GAAA,GAAM,kBAAA;EAON;;;;AAkBD;EAlBC,GAAA,EAAK,mBAAA;AAAA;;;;;;;;;;;;;;;;iBAkBU,WAAA,CAAY,MAAA,EAAQ,yBAAA"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { expiredPreviewCookieHeaders } from "../lib/expiredPreviewCookies.js";
|
|
1
2
|
//#region src/pages/exitPreview.ts
|
|
2
3
|
/**
|
|
3
4
|
* Ends a Prismic preview session within a Next.js app. This function should be used in an API
|
|
@@ -16,6 +17,9 @@
|
|
|
16
17
|
*/
|
|
17
18
|
function exitPreview(config) {
|
|
18
19
|
config.res.clearPreviewData();
|
|
20
|
+
const existingSetCookie = config.res.getHeader("Set-Cookie");
|
|
21
|
+
const expiredPreviewCookies = expiredPreviewCookieHeaders();
|
|
22
|
+
config.res.setHeader("Set-Cookie", existingSetCookie == null ? expiredPreviewCookies : [...(Array.isArray(existingSetCookie) ? existingSetCookie : [existingSetCookie]).map(String), ...expiredPreviewCookies]);
|
|
19
23
|
config.res.setHeader("Cache-Control", "no-store");
|
|
20
24
|
config.res.json({ success: true });
|
|
21
25
|
}
|
|
@@ -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":"
|
|
1
|
+
{"version":3,"file":"exitPreview.js","names":[],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import { expiredPreviewCookieHeaders } from \"../lib/expiredPreviewCookies\"\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\n\t// `clearPreviewData()` only clears Next.js preview cookies. Expire both\n\t// leftover `io.prismic.preview` shapes (toolbar Lax, not Secure; and the\n\t// iframe SameSite=None; Secure write). If either survives,\n\t// `<PrismicPreview>` calls `start()` → `/api/preview` and preview returns.\n\t// Do not use `cookies().set` — that is the App Router helper. Do not\n\t// replace the existing Set-Cookie header from `clearPreviewData()`.\n\tconst existingSetCookie = config.res.getHeader(\"Set-Cookie\")\n\tconst expiredPreviewCookies = expiredPreviewCookieHeaders()\n\tconfig.res.setHeader(\n\t\t\"Set-Cookie\",\n\t\texistingSetCookie == null\n\t\t\t? expiredPreviewCookies\n\t\t\t: [\n\t\t\t\t\t...(Array.isArray(existingSetCookie) ? existingSetCookie : [existingSetCookie]).map(\n\t\t\t\t\t\tString,\n\t\t\t\t\t),\n\t\t\t\t\t...expiredPreviewCookies,\n\t\t\t\t],\n\t)\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":";;;;;;;;;;;;;;;;;AAmCA,SAAgB,YAAY,QAAyC;AACpE,QAAO,IAAI,kBAAkB;CAQ7B,MAAM,oBAAoB,OAAO,IAAI,UAAU,aAAa;CAC5D,MAAM,wBAAwB,6BAA6B;AAC3D,QAAO,IAAI,UACV,cACA,qBAAqB,OAClB,wBACA,CACA,IAAI,MAAM,QAAQ,kBAAkB,GAAG,oBAAoB,CAAC,kBAAkB,EAAE,IAC/E,OACA,EACD,GAAG,sBACH,CACH;AAGD,QAAO,IAAI,UAAU,iBAAiB,WAAW;AAEjD,QAAO,IAAI,KAAK,EAAE,SAAS,MAAM,CAAC"}
|
package/dist/pages/types.d.cts
CHANGED
|
@@ -21,6 +21,7 @@ type NextApiRequestLike = {
|
|
|
21
21
|
type NextApiResponseLike = {
|
|
22
22
|
redirect(url: string): NextApiResponseLike;
|
|
23
23
|
clearPreviewData(): NextApiResponseLike;
|
|
24
|
+
getHeader(name: string): number | string | string[] | undefined;
|
|
24
25
|
setHeader(name: string, value: string | string[]): NextApiResponseLike;
|
|
25
26
|
json(body: any): void;
|
|
26
27
|
setPreviewData(data: object | string): NextApiResponseLike;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.cts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;;KAKY,kBAAA,GAAqB,YAAA,GAChC,IAAA,CAAK,wBAAA;AADN;;;;;AAAA,KAQY,kBAAA;EACX,KAAA,EAAO,OAAA,CAAQ,MAAA;EACf,OAAA,EAAS,OAAA,CAAQ,MAAA;AAAA;;;;;AAFlB;KAUY,mBAAA;EACX,QAAA,CAAS,GAAA,WAAc,mBAAA;EACvB,gBAAA,IAAoB,mBAAA;EACpB,SAAA,CAAU,IAAA,UAAc,KAAA,sBAA2B,mBAAA;EAEnD,IAAA,CAAK,IAAA;EACL,cAAA,CAAe,IAAA,oBAAwB,mBAAA;AAAA"}
|
|
1
|
+
{"version":3,"file":"types.d.cts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;;KAKY,kBAAA,GAAqB,YAAA,GAChC,IAAA,CAAK,wBAAA;AADN;;;;;AAAA,KAQY,kBAAA;EACX,KAAA,EAAO,OAAA,CAAQ,MAAA;EACf,OAAA,EAAS,OAAA,CAAQ,MAAA;AAAA;;;;;AAFlB;KAUY,mBAAA;EACX,QAAA,CAAS,GAAA,WAAc,mBAAA;EACvB,gBAAA,IAAoB,mBAAA;EACpB,SAAA,CAAU,IAAA;EACV,SAAA,CAAU,IAAA,UAAc,KAAA,sBAA2B,mBAAA;EAEnD,IAAA,CAAK,IAAA;EACL,cAAA,CAAe,IAAA,oBAAwB,mBAAA;AAAA"}
|
package/dist/pages/types.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ type NextApiRequestLike = {
|
|
|
21
21
|
type NextApiResponseLike = {
|
|
22
22
|
redirect(url: string): NextApiResponseLike;
|
|
23
23
|
clearPreviewData(): NextApiResponseLike;
|
|
24
|
+
getHeader(name: string): number | string | string[] | undefined;
|
|
24
25
|
setHeader(name: string, value: string | string[]): NextApiResponseLike;
|
|
25
26
|
json(body: any): void;
|
|
26
27
|
setPreviewData(data: object | string): NextApiResponseLike;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;;KAKY,kBAAA,GAAqB,YAAA,GAChC,IAAA,CAAK,wBAAA;AADN;;;;;AAAA,KAQY,kBAAA;EACX,KAAA,EAAO,OAAA,CAAQ,MAAA;EACf,OAAA,EAAS,OAAA,CAAQ,MAAA;AAAA;;;;;AAFlB;KAUY,mBAAA;EACX,QAAA,CAAS,GAAA,WAAc,mBAAA;EACvB,gBAAA,IAAoB,mBAAA;EACpB,SAAA,CAAU,IAAA,UAAc,KAAA,sBAA2B,mBAAA;EAEnD,IAAA,CAAK,IAAA;EACL,cAAA,CAAe,IAAA,oBAAwB,mBAAA;AAAA"}
|
|
1
|
+
{"version":3,"file":"types.d.ts","names":[],"sources":["../../src/pages/types.ts"],"mappings":";;;;;KAKY,kBAAA,GAAqB,YAAA,GAChC,IAAA,CAAK,wBAAA;AADN;;;;;AAAA,KAQY,kBAAA;EACX,KAAA,EAAO,OAAA,CAAQ,MAAA;EACf,OAAA,EAAS,OAAA,CAAQ,MAAA;AAAA;;;;;AAFlB;KAUY,mBAAA;EACX,QAAA,CAAS,GAAA,WAAc,mBAAA;EACvB,gBAAA,IAAoB,mBAAA;EACpB,SAAA,CAAU,IAAA;EACV,SAAA,CAAU,IAAA,UAAc,KAAA,sBAA2B,mBAAA;EAEnD,IAAA,CAAK,IAAA;EACL,cAAA,CAAe,IAAA,oBAAwB,mBAAA;AAAA"}
|
|
@@ -7,7 +7,12 @@ async function redirectToPreviewURL(config) {
|
|
|
7
7
|
const { redirect } = await import("next/navigation");
|
|
8
8
|
const documentID = request.nextUrl.searchParams.get("documentId") ?? void 0;
|
|
9
9
|
const previewToken = request.nextUrl.searchParams.get("token") ?? void 0;
|
|
10
|
-
if (previewToken) (await cookies()).set(_prismicio_client.cookie.preview, previewToken
|
|
10
|
+
if (previewToken) (await cookies()).set(_prismicio_client.cookie.preview, previewToken, {
|
|
11
|
+
path: "/",
|
|
12
|
+
sameSite: "none",
|
|
13
|
+
secure: true,
|
|
14
|
+
httpOnly: false
|
|
15
|
+
});
|
|
11
16
|
const previewURL = await client.resolvePreviewURL({
|
|
12
17
|
documentID,
|
|
13
18
|
previewToken,
|
|
@@ -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 previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tconst cookieJar = await cookies()\n\t\tcookieJar.set(prismicCookie.preview, previewToken)\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;
|
|
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// Path=/; SameSite=None; Secure so the cookie is sent when the site is\n\t// embedded in a cross-site iframe (e.g. the Platform editor). Chrome\n\t// defaults omitted SameSite to Lax and strips the cookie from those\n\t// requests. Do not set HttpOnly: the toolbar reads `document.cookie`.\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, {\n\t\t\tpath: \"/\",\n\t\t\tsameSite: \"none\",\n\t\t\tsecure: true,\n\t\t\thttpOnly: false,\n\t\t})\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;CAWrE,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,QAAQ,IAAI,KAAA;AAClE,KAAI,aAEH,EADkB,MAAM,SAAS,EACvB,IAAIA,kBAAAA,OAAc,SAAS,cAAc;EAClD,MAAM;EACN,UAAU;EACV,QAAQ;EACR,UAAU;EACV,CAAC;CAGH,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;EACA,CAAC;AAED,EAAC,MAAM,WAAW,EAAE,QAAQ;AAE7B,QAAO,SAAS,WAAW"}
|
|
@@ -6,7 +6,12 @@ async function redirectToPreviewURL(config) {
|
|
|
6
6
|
const { redirect } = await import("next/navigation");
|
|
7
7
|
const documentID = request.nextUrl.searchParams.get("documentId") ?? void 0;
|
|
8
8
|
const previewToken = request.nextUrl.searchParams.get("token") ?? void 0;
|
|
9
|
-
if (previewToken) (await cookies()).set(cookie.preview, previewToken
|
|
9
|
+
if (previewToken) (await cookies()).set(cookie.preview, previewToken, {
|
|
10
|
+
path: "/",
|
|
11
|
+
sameSite: "none",
|
|
12
|
+
secure: true,
|
|
13
|
+
httpOnly: false
|
|
14
|
+
});
|
|
10
15
|
const previewURL = await client.resolvePreviewURL({
|
|
11
16
|
documentID,
|
|
12
17
|
previewToken,
|
|
@@ -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 previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined\n\tif (previewToken) {\n\t\tconst cookieJar = await cookies()\n\t\tcookieJar.set(prismicCookie.preview, previewToken)\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;
|
|
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// Path=/; SameSite=None; Secure so the cookie is sent when the site is\n\t// embedded in a cross-site iframe (e.g. the Platform editor). Chrome\n\t// defaults omitted SameSite to Lax and strips the cookie from those\n\t// requests. Do not set HttpOnly: the toolbar reads `document.cookie`.\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, {\n\t\t\tpath: \"/\",\n\t\t\tsameSite: \"none\",\n\t\t\tsecure: true,\n\t\t\thttpOnly: false,\n\t\t})\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;CAWrE,MAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,QAAQ,IAAI,KAAA;AAClE,KAAI,aAEH,EADkB,MAAM,SAAS,EACvB,IAAIA,OAAc,SAAS,cAAc;EAClD,MAAM;EACN,UAAU;EACV,QAAQ;EACR,UAAU;EACV,CAAC;CAGH,MAAM,aAAa,MAAM,OAAO,kBAAkB;EACjD;EACA;EACA;EACA;EACA,CAAC;AAED,EAAC,MAAM,WAAW,EAAE,QAAQ;AAE7B,QAAO,SAAS,WAAW"}
|
package/package.json
CHANGED
package/src/exitPreview.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
expiredDraftModeCookieHeaders,
|
|
3
|
+
expiredPreviewCookieHeaders,
|
|
4
|
+
} from "./lib/expiredPreviewCookies"
|
|
2
5
|
|
|
3
6
|
/**
|
|
4
7
|
* Ends a Prismic preview session within a Next.js app. This function should be used in a Router
|
|
@@ -16,21 +19,17 @@ import { cookie as prismicCookie } from "@prismicio/client"
|
|
|
16
19
|
* ```
|
|
17
20
|
*/
|
|
18
21
|
export async function exitPreview(): Promise<Response> {
|
|
19
|
-
//
|
|
20
|
-
//
|
|
21
|
-
//
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// `redirectToPreviewURL` writes the preview cookie, so `exitPreview`
|
|
27
|
-
// clears it to close the preview-cookie loop.
|
|
28
|
-
;(await cookies()).delete(prismicCookie.preview)
|
|
29
|
-
|
|
30
|
-
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
31
|
-
return new Response(JSON.stringify({ success: true }), {
|
|
32
|
-
headers: {
|
|
33
|
-
"Cache-Control": "no-store",
|
|
34
|
-
},
|
|
22
|
+
// Do not call `cookies()` or `draftMode().disable()`. ResponseCookies is
|
|
23
|
+
// name-keyed: `appendMutableCookies` rebuilds Set-Cookie by name and drops
|
|
24
|
+
// the extra Lax expire. Emit every expire on the Response instead:
|
|
25
|
+
// toolbar Lax + iframe None; Secure for `io.prismic.preview`, and both
|
|
26
|
+
// shapes of `__prerender_bypass` that `disable()` would have cleared.
|
|
27
|
+
const headers = new Headers({
|
|
28
|
+
"Cache-Control": "no-store",
|
|
35
29
|
})
|
|
30
|
+
for (const setCookie of [...expiredPreviewCookieHeaders(), ...expiredDraftModeCookieHeaders()]) {
|
|
31
|
+
headers.append("Set-Cookie", setCookie)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return new Response(JSON.stringify({ success: true }), { headers })
|
|
36
35
|
}
|
package/src/getPreviewRef.ts
CHANGED
|
@@ -42,10 +42,41 @@ export async function getPreviewRef(): Promise<string | undefined> {
|
|
|
42
42
|
return
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
45
|
+
// Draft Mode gates preview reads. The cookie may be a raw token or the
|
|
46
|
+
// toolbar jar (`{ "<repo>.prismic.io": { preview } }`). Only `preview` is a
|
|
47
|
+
// Content API ref; the jar string is not.
|
|
48
|
+
return previewRefFromCookie(cookie)
|
|
49
|
+
}
|
|
49
50
|
|
|
50
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Unwraps `io.prismic.preview` to a Content API ref.
|
|
53
|
+
*
|
|
54
|
+
* After toolbar `PreviewCookie.sync` / `upsertPreviewForDomain`, the cookie is
|
|
55
|
+
* `{ [domain]: { preview: string } }`. Returning that jar makes `enableAutoPreviews`
|
|
56
|
+
* / Cache Components send a non-ref (ParsingError / 404).
|
|
57
|
+
*/
|
|
58
|
+
function previewRefFromCookie(cookie: string): string | undefined {
|
|
59
|
+
try {
|
|
60
|
+
const parsed: unknown = JSON.parse(cookie)
|
|
61
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
62
|
+
return
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const previewRefs: string[] = []
|
|
66
|
+
for (const value of Object.values(parsed)) {
|
|
67
|
+
if (
|
|
68
|
+
value !== null &&
|
|
69
|
+
typeof value === "object" &&
|
|
70
|
+
!Array.isArray(value) &&
|
|
71
|
+
"preview" in value &&
|
|
72
|
+
typeof value.preview === "string"
|
|
73
|
+
) {
|
|
74
|
+
previewRefs.push(value.preview)
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return previewRefs.length === 1 ? previewRefs[0] : undefined
|
|
79
|
+
} catch {
|
|
80
|
+
return cookie
|
|
81
|
+
}
|
|
51
82
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { cookie as prismicCookie } from "@prismicio/client"
|
|
2
|
+
|
|
3
|
+
const epoch = new Date(0)
|
|
4
|
+
|
|
5
|
+
function expiresAttr(): string {
|
|
6
|
+
return `Expires=${epoch.toUTCString()}`
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Expired `Set-Cookie` values for both leftover `io.prismic.preview` shapes. */
|
|
10
|
+
export function expiredPreviewCookieHeaders(name = prismicCookie.preview): string[] {
|
|
11
|
+
const expires = expiresAttr()
|
|
12
|
+
|
|
13
|
+
return [
|
|
14
|
+
`${name}=; Path=/; ${expires}; SameSite=Lax`,
|
|
15
|
+
`${name}=; Path=/; ${expires}; SameSite=None; Secure`,
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Expired `Set-Cookie` values for the App Router draft-mode cookie that `draftMode().disable()`
|
|
21
|
+
* would clear. Next sets `__prerender_bypass` as Lax (not Secure) in development and SameSite=None;
|
|
22
|
+
* Secure in production.
|
|
23
|
+
*/
|
|
24
|
+
export function expiredDraftModeCookieHeaders(): string[] {
|
|
25
|
+
const expires = expiresAttr()
|
|
26
|
+
const name = "__prerender_bypass"
|
|
27
|
+
|
|
28
|
+
return [
|
|
29
|
+
`${name}=; Path=/; ${expires}; SameSite=Lax; HttpOnly`,
|
|
30
|
+
`${name}=; Path=/; ${expires}; SameSite=None; Secure; HttpOnly`,
|
|
31
|
+
]
|
|
32
|
+
}
|
package/src/pages/exitPreview.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { expiredPreviewCookieHeaders } from "../lib/expiredPreviewCookies"
|
|
1
2
|
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
2
3
|
|
|
3
4
|
/** Configuration for `exitPreview()`. */
|
|
@@ -35,6 +36,26 @@ export type ExitPreviewAPIRouteConfig = {
|
|
|
35
36
|
export function exitPreview(config: ExitPreviewAPIRouteConfig): void {
|
|
36
37
|
config.res.clearPreviewData()
|
|
37
38
|
|
|
39
|
+
// `clearPreviewData()` only clears Next.js preview cookies. Expire both
|
|
40
|
+
// leftover `io.prismic.preview` shapes (toolbar Lax, not Secure; and the
|
|
41
|
+
// iframe SameSite=None; Secure write). If either survives,
|
|
42
|
+
// `<PrismicPreview>` calls `start()` → `/api/preview` and preview returns.
|
|
43
|
+
// Do not use `cookies().set` — that is the App Router helper. Do not
|
|
44
|
+
// replace the existing Set-Cookie header from `clearPreviewData()`.
|
|
45
|
+
const existingSetCookie = config.res.getHeader("Set-Cookie")
|
|
46
|
+
const expiredPreviewCookies = expiredPreviewCookieHeaders()
|
|
47
|
+
config.res.setHeader(
|
|
48
|
+
"Set-Cookie",
|
|
49
|
+
existingSetCookie == null
|
|
50
|
+
? expiredPreviewCookies
|
|
51
|
+
: [
|
|
52
|
+
...(Array.isArray(existingSetCookie) ? existingSetCookie : [existingSetCookie]).map(
|
|
53
|
+
String,
|
|
54
|
+
),
|
|
55
|
+
...expiredPreviewCookies,
|
|
56
|
+
],
|
|
57
|
+
)
|
|
58
|
+
|
|
38
59
|
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
39
60
|
config.res.setHeader("Cache-Control", "no-store")
|
|
40
61
|
|
package/src/pages/types.ts
CHANGED
|
@@ -24,6 +24,7 @@ export type NextApiRequestLike = {
|
|
|
24
24
|
export type NextApiResponseLike = {
|
|
25
25
|
redirect(url: string): NextApiResponseLike
|
|
26
26
|
clearPreviewData(): NextApiResponseLike
|
|
27
|
+
getHeader(name: string): number | string | string[] | undefined
|
|
27
28
|
setHeader(name: string, value: string | string[]): NextApiResponseLike
|
|
28
29
|
// oxlint-disable-next-line no-explicit-any explicit-module-boundary-types
|
|
29
30
|
json(body: any): void
|
|
@@ -50,10 +50,20 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
|
|
|
50
50
|
// to support unpublished previews. Without setting it here, the page
|
|
51
51
|
// will try to render without the preview cookie, leading to a
|
|
52
52
|
// PrismicNotFound error.
|
|
53
|
+
//
|
|
54
|
+
// Path=/; SameSite=None; Secure so the cookie is sent when the site is
|
|
55
|
+
// embedded in a cross-site iframe (e.g. the Platform editor). Chrome
|
|
56
|
+
// defaults omitted SameSite to Lax and strips the cookie from those
|
|
57
|
+
// requests. Do not set HttpOnly: the toolbar reads `document.cookie`.
|
|
53
58
|
const previewToken = request.nextUrl.searchParams.get("token") ?? undefined
|
|
54
59
|
if (previewToken) {
|
|
55
60
|
const cookieJar = await cookies()
|
|
56
|
-
cookieJar.set(prismicCookie.preview, previewToken
|
|
61
|
+
cookieJar.set(prismicCookie.preview, previewToken, {
|
|
62
|
+
path: "/",
|
|
63
|
+
sameSite: "none",
|
|
64
|
+
secure: true,
|
|
65
|
+
httpOnly: false,
|
|
66
|
+
})
|
|
57
67
|
}
|
|
58
68
|
|
|
59
69
|
const previewURL = await client.resolvePreviewURL({
|