@prismicio/next 2.2.4-pr.114.37ca25f → 2.2.4-pr.114.39a5b55
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/enableAutoPreviews.cjs +2 -16
- package/dist/enableAutoPreviews.cjs.map +1 -1
- package/dist/enableAutoPreviews.d.cts.map +1 -1
- package/dist/enableAutoPreviews.d.ts.map +1 -1
- package/dist/enableAutoPreviews.js +2 -15
- package/dist/enableAutoPreviews.js.map +1 -1
- package/dist/exitPreview.cjs +4 -1
- 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 +3 -1
- package/dist/exitPreview.js.map +1 -1
- package/dist/getPreviewRef.cjs +39 -0
- package/dist/getPreviewRef.cjs.map +1 -0
- package/dist/getPreviewRef.d.cts +23 -0
- package/dist/getPreviewRef.d.cts.map +1 -0
- package/dist/getPreviewRef.d.ts +23 -0
- package/dist/getPreviewRef.d.ts.map +1 -0
- package/dist/getPreviewRef.js +38 -0
- package/dist/getPreviewRef.js.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/package.json +1 -1
- package/src/enableAutoPreviews.ts +4 -33
- package/src/exitPreview.ts +7 -1
- package/src/getPreviewRef.ts +52 -0
- package/src/index.ts +2 -0
|
@@ -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>, "rel" | "field" | "href" | "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>, "rel" | "field" | "href" | "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>, "rel" | "field" | "href" | "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>, "rel" | "field" | "href" | "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>, "rel" | "field" | "href" | "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>, "rel" | "field" | "href" | "document"> & {
|
|
45
45
|
linkResolver?: LinkResolverFunction;
|
|
46
46
|
rel?: string | AsLinkAttrsConfig["rel"];
|
|
47
47
|
} & {
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
require("./
|
|
2
|
-
let _prismicio_client = require("@prismicio/client");
|
|
1
|
+
const require_getPreviewRef = require("./getPreviewRef.cjs");
|
|
3
2
|
//#region src/enableAutoPreviews.ts
|
|
4
3
|
/**
|
|
5
4
|
* Configures a Prismic client to automatically query draft content during a preview session.
|
|
@@ -7,20 +6,7 @@ let _prismicio_client = require("@prismicio/client");
|
|
|
7
6
|
* @param config - Configuration for the function.
|
|
8
7
|
*/
|
|
9
8
|
function enableAutoPreviews(config) {
|
|
10
|
-
config.client.queryContentFromRef(
|
|
11
|
-
const { cookies, draftMode } = await import("next/headers.js");
|
|
12
|
-
let isDraftModeEnabled = false;
|
|
13
|
-
try {
|
|
14
|
-
isDraftModeEnabled = (await draftMode()).isEnabled;
|
|
15
|
-
} catch {
|
|
16
|
-
return;
|
|
17
|
-
}
|
|
18
|
-
if (!isDraftModeEnabled) return;
|
|
19
|
-
const cookie = (await cookies()).get(_prismicio_client.cookie.preview)?.value;
|
|
20
|
-
if (!cookie) return;
|
|
21
|
-
if (!cookie.includes("websitePreviewId=")) return;
|
|
22
|
-
return cookie;
|
|
23
|
-
});
|
|
9
|
+
config.client.queryContentFromRef(() => require_getPreviewRef.getPreviewRef());
|
|
24
10
|
}
|
|
25
11
|
//#endregion
|
|
26
12
|
exports.enableAutoPreviews = enableAutoPreviews;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enableAutoPreviews.cjs","names":["
|
|
1
|
+
{"version":3,"file":"enableAutoPreviews.cjs","names":["getPreviewRef"],"sources":["../src/enableAutoPreviews.ts"],"sourcesContent":["import type { Client } from \"@prismicio/client\"\n\nimport { getPreviewRef } from \"./getPreviewRef\"\n\n/** Configuration for `enableAutoPreviews`. */\nexport type EnableAutoPreviewsConfig = {\n\t/** Prismic client with which automatic previews will be enabled. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"queryContentFromRef\" | \"enableAutoPreviewsFromReq\">\n}\n\n/**\n * Configures a Prismic client to automatically query draft content during a preview session.\n *\n * @param config - Configuration for the function.\n */\nexport function enableAutoPreviews(config: EnableAutoPreviewsConfig): void {\n\t// We use a function value so the cookie is checked on every\n\t// request. We don't have a static value to read from.\n\tconfig.client.queryContentFromRef(() => getPreviewRef())\n}\n"],"mappings":";;;;;;;AAkBA,SAAgB,mBAAmB,QAAwC;AAG1E,QAAO,OAAO,0BAA0BA,sBAAAA,eAAe,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enableAutoPreviews.d.cts","names":[],"sources":["../src/enableAutoPreviews.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"enableAutoPreviews.d.cts","names":[],"sources":["../src/enableAutoPreviews.ts"],"mappings":";;;;KAKY,wBAAA;EAAA,oEAKX,MAAA,EAAQ,IAAA,CAAK,MAAA;AAAA;;;;;;iBAQE,kBAAA,CAAmB,MAAA,EAAQ,wBAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enableAutoPreviews.d.ts","names":[],"sources":["../src/enableAutoPreviews.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"enableAutoPreviews.d.ts","names":[],"sources":["../src/enableAutoPreviews.ts"],"mappings":";;;;KAKY,wBAAA;EAAA,oEAKX,MAAA,EAAQ,IAAA,CAAK,MAAA;AAAA;;;;;;iBAQE,kBAAA,CAAmB,MAAA,EAAQ,wBAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getPreviewRef } from "./getPreviewRef.js";
|
|
2
2
|
//#region src/enableAutoPreviews.ts
|
|
3
3
|
/**
|
|
4
4
|
* Configures a Prismic client to automatically query draft content during a preview session.
|
|
@@ -6,20 +6,7 @@ import { cookie } from "@prismicio/client";
|
|
|
6
6
|
* @param config - Configuration for the function.
|
|
7
7
|
*/
|
|
8
8
|
function enableAutoPreviews(config) {
|
|
9
|
-
config.client.queryContentFromRef(
|
|
10
|
-
const { cookies, draftMode } = await import("next/headers.js");
|
|
11
|
-
let isDraftModeEnabled = false;
|
|
12
|
-
try {
|
|
13
|
-
isDraftModeEnabled = (await draftMode()).isEnabled;
|
|
14
|
-
} catch {
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
if (!isDraftModeEnabled) return;
|
|
18
|
-
const cookie$1 = (await cookies()).get(cookie.preview)?.value;
|
|
19
|
-
if (!cookie$1) return;
|
|
20
|
-
if (!cookie$1.includes("websitePreviewId=")) return;
|
|
21
|
-
return cookie$1;
|
|
22
|
-
});
|
|
9
|
+
config.client.queryContentFromRef(() => getPreviewRef());
|
|
23
10
|
}
|
|
24
11
|
//#endregion
|
|
25
12
|
export { enableAutoPreviews };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enableAutoPreviews.js","names":[
|
|
1
|
+
{"version":3,"file":"enableAutoPreviews.js","names":[],"sources":["../src/enableAutoPreviews.ts"],"sourcesContent":["import type { Client } from \"@prismicio/client\"\n\nimport { getPreviewRef } from \"./getPreviewRef\"\n\n/** Configuration for `enableAutoPreviews`. */\nexport type EnableAutoPreviewsConfig = {\n\t/** Prismic client with which automatic previews will be enabled. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"queryContentFromRef\" | \"enableAutoPreviewsFromReq\">\n}\n\n/**\n * Configures a Prismic client to automatically query draft content during a preview session.\n *\n * @param config - Configuration for the function.\n */\nexport function enableAutoPreviews(config: EnableAutoPreviewsConfig): void {\n\t// We use a function value so the cookie is checked on every\n\t// request. We don't have a static value to read from.\n\tconfig.client.queryContentFromRef(() => getPreviewRef())\n}\n"],"mappings":";;;;;;;AAkBA,SAAgB,mBAAmB,QAAwC;AAG1E,QAAO,OAAO,0BAA0B,eAAe,CAAC"}
|
package/dist/exitPreview.cjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
require("./_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
let _prismicio_client = require("@prismicio/client");
|
|
1
3
|
//#region src/exitPreview.ts
|
|
2
4
|
/**
|
|
3
5
|
* Ends a Prismic preview session within a Next.js app. This function should be used in a Router
|
|
@@ -15,8 +17,9 @@
|
|
|
15
17
|
* ```
|
|
16
18
|
*/
|
|
17
19
|
async function exitPreview() {
|
|
18
|
-
const { draftMode } = await import("next/headers.js");
|
|
20
|
+
const { cookies, draftMode } = await import("next/headers.js");
|
|
19
21
|
(await draftMode()).disable();
|
|
22
|
+
(await cookies()).delete(_prismicio_client.cookie.preview);
|
|
20
23
|
return new Response(JSON.stringify({ success: true }), { headers: { "Cache-Control": "no-store" } });
|
|
21
24
|
}
|
|
22
25
|
//#endregion
|
package/dist/exitPreview.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.cjs","names":[],"sources":["../src/exitPreview.ts"],"sourcesContent":["/**\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 { draftMode } = await import(\"next/headers\")\n\n\t;(await draftMode()).disable()\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":"
|
|
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.\n\t;(await cookies()).delete(prismicCookie.preview)\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;AAI7B,EAAC,MAAM,SAAS,EAAE,OAAOA,kBAAAA,OAAc,QAAQ;AAGhD,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":";;
|
|
1
|
+
{"version":3,"file":"exitPreview.d.cts","names":[],"sources":["../src/exitPreview.ts"],"mappings":";;AAiBA;;;;;;;;;;;;;;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":";;AAiBA;;;;;;;;;;;;;;iBAAsB,WAAA,CAAA,GAAe,OAAA,CAAQ,QAAA"}
|
package/dist/exitPreview.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { cookie } from "@prismicio/client";
|
|
1
2
|
//#region src/exitPreview.ts
|
|
2
3
|
/**
|
|
3
4
|
* Ends a Prismic preview session within a Next.js app. This function should be used in a Router
|
|
@@ -15,8 +16,9 @@
|
|
|
15
16
|
* ```
|
|
16
17
|
*/
|
|
17
18
|
async function exitPreview() {
|
|
18
|
-
const { draftMode } = await import("next/headers.js");
|
|
19
|
+
const { cookies, draftMode } = await import("next/headers.js");
|
|
19
20
|
(await draftMode()).disable();
|
|
21
|
+
(await cookies()).delete(cookie.preview);
|
|
20
22
|
return new Response(JSON.stringify({ success: true }), { headers: { "Cache-Control": "no-store" } });
|
|
21
23
|
}
|
|
22
24
|
//#endregion
|
package/dist/exitPreview.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.js","names":[],"sources":["../src/exitPreview.ts"],"sourcesContent":["/**\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 { draftMode } = await import(\"next/headers\")\n\n\t;(await draftMode()).disable()\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":"
|
|
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.\n\t;(await cookies()).delete(prismicCookie.preview)\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;AAI7B,EAAC,MAAM,SAAS,EAAE,OAAOA,OAAc,QAAQ;AAGhD,QAAO,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,MAAM,CAAC,EAAE,EACtD,SAAS,EACR,iBAAiB,YACjB,EACD,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require("./_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
let _prismicio_client = require("@prismicio/client");
|
|
3
|
+
//#region src/getPreviewRef.ts
|
|
4
|
+
/**
|
|
5
|
+
* Reads the Prismic preview ref for the current request when an active preview
|
|
6
|
+
* session exists.
|
|
7
|
+
*
|
|
8
|
+
* This is the read-side counterpart to `redirectToPreviewURL`, which writes the
|
|
9
|
+
* preview cookie. Use it with Next.js Cache Components to read the ref _outside_
|
|
10
|
+
* a cached function and pass it _in_ as an argument so it becomes part of the
|
|
11
|
+
* cache key:
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ;```typescript
|
|
15
|
+
* import { getPreviewRef } from "@prismicio/next"
|
|
16
|
+
*
|
|
17
|
+
* const page = await fetchPage(uid, await getPreviewRef())
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @returns The active preview ref, or `undefined` if no preview session is active.
|
|
21
|
+
*/
|
|
22
|
+
async function getPreviewRef() {
|
|
23
|
+
const { cookies, draftMode } = await import("next/headers.js");
|
|
24
|
+
let isDraftModeEnabled = false;
|
|
25
|
+
try {
|
|
26
|
+
isDraftModeEnabled = (await draftMode()).isEnabled;
|
|
27
|
+
} catch {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
if (!isDraftModeEnabled) return;
|
|
31
|
+
const cookie = (await cookies()).get(_prismicio_client.cookie.preview)?.value;
|
|
32
|
+
if (!cookie) return;
|
|
33
|
+
if (!cookie.includes("websitePreviewId=")) return;
|
|
34
|
+
return cookie;
|
|
35
|
+
}
|
|
36
|
+
//#endregion
|
|
37
|
+
exports.getPreviewRef = getPreviewRef;
|
|
38
|
+
|
|
39
|
+
//# sourceMappingURL=getPreviewRef.cjs.map
|
|
@@ -0,0 +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\n * session exists.\n *\n * This is the read-side counterpart to `redirectToPreviewURL`, which writes the\n * preview cookie. Use it with Next.js Cache Components to read the ref _outside_\n * a cached function and pass it _in_ as an argument so it becomes part of the\n * 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 */\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 isActiveCookie = cookie.includes(\"websitePreviewId=\")\n\tif (!isActiveCookie) {\n\t\treturn\n\t}\n\n\treturn cookie\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAoBA,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;AAID,KAAI,CADmB,OAAO,SAAS,oBAAoB,CAE1D;AAGD,QAAO"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/getPreviewRef.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Reads the Prismic preview ref for the current request when an active preview
|
|
4
|
+
* session exists.
|
|
5
|
+
*
|
|
6
|
+
* This is the read-side counterpart to `redirectToPreviewURL`, which writes the
|
|
7
|
+
* preview cookie. Use it with Next.js Cache Components to read the ref _outside_
|
|
8
|
+
* a cached function and pass it _in_ as an argument so it becomes part of the
|
|
9
|
+
* cache key:
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ;```typescript
|
|
13
|
+
* import { getPreviewRef } from "@prismicio/next"
|
|
14
|
+
*
|
|
15
|
+
* const page = await fetchPage(uid, await getPreviewRef())
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @returns The active preview ref, or `undefined` if no preview session is active.
|
|
19
|
+
*/
|
|
20
|
+
declare function getPreviewRef(): Promise<string | undefined>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { getPreviewRef };
|
|
23
|
+
//# sourceMappingURL=getPreviewRef.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPreviewRef.d.cts","names":[],"sources":["../src/getPreviewRef.ts"],"mappings":";;AAoBA;;;;;;;;;;;;;;;;;iBAAsB,aAAA,CAAA,GAAiB,OAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/getPreviewRef.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Reads the Prismic preview ref for the current request when an active preview
|
|
4
|
+
* session exists.
|
|
5
|
+
*
|
|
6
|
+
* This is the read-side counterpart to `redirectToPreviewURL`, which writes the
|
|
7
|
+
* preview cookie. Use it with Next.js Cache Components to read the ref _outside_
|
|
8
|
+
* a cached function and pass it _in_ as an argument so it becomes part of the
|
|
9
|
+
* cache key:
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ;```typescript
|
|
13
|
+
* import { getPreviewRef } from "@prismicio/next"
|
|
14
|
+
*
|
|
15
|
+
* const page = await fetchPage(uid, await getPreviewRef())
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @returns The active preview ref, or `undefined` if no preview session is active.
|
|
19
|
+
*/
|
|
20
|
+
declare function getPreviewRef(): Promise<string | undefined>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { getPreviewRef };
|
|
23
|
+
//# sourceMappingURL=getPreviewRef.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPreviewRef.d.ts","names":[],"sources":["../src/getPreviewRef.ts"],"mappings":";;AAoBA;;;;;;;;;;;;;;;;;iBAAsB,aAAA,CAAA,GAAiB,OAAA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { cookie } from "@prismicio/client";
|
|
2
|
+
//#region src/getPreviewRef.ts
|
|
3
|
+
/**
|
|
4
|
+
* Reads the Prismic preview ref for the current request when an active preview
|
|
5
|
+
* session exists.
|
|
6
|
+
*
|
|
7
|
+
* This is the read-side counterpart to `redirectToPreviewURL`, which writes the
|
|
8
|
+
* preview cookie. Use it with Next.js Cache Components to read the ref _outside_
|
|
9
|
+
* a cached function and pass it _in_ as an argument so it becomes part of the
|
|
10
|
+
* cache key:
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ;```typescript
|
|
14
|
+
* import { getPreviewRef } from "@prismicio/next"
|
|
15
|
+
*
|
|
16
|
+
* const page = await fetchPage(uid, await getPreviewRef())
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @returns The active preview ref, or `undefined` if no preview session is active.
|
|
20
|
+
*/
|
|
21
|
+
async function getPreviewRef() {
|
|
22
|
+
const { cookies, draftMode } = await import("next/headers.js");
|
|
23
|
+
let isDraftModeEnabled = false;
|
|
24
|
+
try {
|
|
25
|
+
isDraftModeEnabled = (await draftMode()).isEnabled;
|
|
26
|
+
} catch {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
if (!isDraftModeEnabled) return;
|
|
30
|
+
const cookie$1 = (await cookies()).get(cookie.preview)?.value;
|
|
31
|
+
if (!cookie$1) return;
|
|
32
|
+
if (!cookie$1.includes("websitePreviewId=")) return;
|
|
33
|
+
return cookie$1;
|
|
34
|
+
}
|
|
35
|
+
//#endregion
|
|
36
|
+
export { getPreviewRef };
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=getPreviewRef.js.map
|
|
@@ -0,0 +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\n * session exists.\n *\n * This is the read-side counterpart to `redirectToPreviewURL`, which writes the\n * preview cookie. Use it with Next.js Cache Components to read the ref _outside_\n * a cached function and pass it _in_ as an argument so it becomes part of the\n * 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 */\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 isActiveCookie = cookie.includes(\"websitePreviewId=\")\n\tif (!isActiveCookie) {\n\t\treturn\n\t}\n\n\treturn cookie\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAoBA,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;AAID,KAAI,CADmBA,SAAO,SAAS,oBAAoB,CAE1D;AAGD,QAAOA"}
|
package/dist/index.cjs
CHANGED
|
@@ -2,6 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
2
2
|
const require_exitPreview = require("./exitPreview.cjs");
|
|
3
3
|
const require_PrismicPreview = require("./PrismicPreview.cjs");
|
|
4
4
|
const require_PrismicNextLink = require("./PrismicNextLink.cjs");
|
|
5
|
+
const require_getPreviewRef = require("./getPreviewRef.cjs");
|
|
5
6
|
const require_enableAutoPreviews = require("./enableAutoPreviews.cjs");
|
|
6
7
|
const require_redirectToPreviewURL = require("./redirectToPreviewURL.cjs");
|
|
7
8
|
const require_imgixLoader = require("./imgixLoader.cjs");
|
|
@@ -20,6 +21,7 @@ exports.cacheTagPrismicPages = require_cacheTagPrismicPages.cacheTagPrismicPages
|
|
|
20
21
|
exports.createLocaleRedirect = require_createLocaleRedirect.createLocaleRedirect;
|
|
21
22
|
exports.enableAutoPreviews = require_enableAutoPreviews.enableAutoPreviews;
|
|
22
23
|
exports.exitPreview = require_exitPreview.exitPreview;
|
|
24
|
+
exports.getPreviewRef = require_getPreviewRef.getPreviewRef;
|
|
23
25
|
exports.getPrismicCacheTags = require_getPrismicCacheTags.getPrismicCacheTags;
|
|
24
26
|
exports.getSlices = require_getSlices.getSlices;
|
|
25
27
|
exports.imgixLoader = require_imgixLoader.imgixLoader;
|
package/dist/index.d.cts
CHANGED
|
@@ -2,6 +2,7 @@ import { exitPreview } from "./exitPreview.cjs";
|
|
|
2
2
|
import { PrismicPreview, PrismicPreviewProps } from "./PrismicPreview.cjs";
|
|
3
3
|
import { PrismicNextLink, PrismicNextLinkProps } from "./PrismicNextLink.cjs";
|
|
4
4
|
import { EnableAutoPreviewsConfig, enableAutoPreviews } from "./enableAutoPreviews.cjs";
|
|
5
|
+
import { getPreviewRef } from "./getPreviewRef.cjs";
|
|
5
6
|
import { CreateClientConfig } from "./types.cjs";
|
|
6
7
|
import { RedirectToPreviewURLConfig, redirectToPreviewURL } from "./redirectToPreviewURL.cjs";
|
|
7
8
|
import { PrismicNextImage, PrismicNextImageProps } from "./PrismicNextImage.cjs";
|
|
@@ -12,4 +13,4 @@ import { CreateLocaleRedirectConfig, createLocaleRedirect } from "./createLocale
|
|
|
12
13
|
import { getPrismicCacheTags } from "./getPrismicCacheTags.cjs";
|
|
13
14
|
import { cacheTagPrismicPages } from "./cacheTagPrismicPages.cjs";
|
|
14
15
|
import { revalidatePrismicPages } from "./revalidatePrismicPages.cjs";
|
|
15
|
-
export { type CreateClientConfig, type CreateLocaleRedirectConfig, type EnableAutoPreviewsConfig, PrismicNextImage, type PrismicNextImageProps, PrismicNextLink, type PrismicNextLinkProps, PrismicPreview, type PrismicPreviewProps, type RedirectToPreviewURLConfig, SliceSimulator, type SliceSimulatorParams, type SliceSimulatorProps, cacheTagPrismicPages, createLocaleRedirect, enableAutoPreviews, exitPreview, getPrismicCacheTags, getSlices, imgixLoader, redirectToPreviewURL, revalidatePrismicPages };
|
|
16
|
+
export { type CreateClientConfig, type CreateLocaleRedirectConfig, type EnableAutoPreviewsConfig, PrismicNextImage, type PrismicNextImageProps, PrismicNextLink, type PrismicNextLinkProps, PrismicPreview, type PrismicPreviewProps, type RedirectToPreviewURLConfig, SliceSimulator, type SliceSimulatorParams, type SliceSimulatorProps, cacheTagPrismicPages, createLocaleRedirect, enableAutoPreviews, exitPreview, getPreviewRef, getPrismicCacheTags, getSlices, imgixLoader, redirectToPreviewURL, revalidatePrismicPages };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { exitPreview } from "./exitPreview.js";
|
|
|
2
2
|
import { PrismicPreview, PrismicPreviewProps } from "./PrismicPreview.js";
|
|
3
3
|
import { PrismicNextLink, PrismicNextLinkProps } from "./PrismicNextLink.js";
|
|
4
4
|
import { EnableAutoPreviewsConfig, enableAutoPreviews } from "./enableAutoPreviews.js";
|
|
5
|
+
import { getPreviewRef } from "./getPreviewRef.js";
|
|
5
6
|
import { CreateClientConfig } from "./types.js";
|
|
6
7
|
import { RedirectToPreviewURLConfig, redirectToPreviewURL } from "./redirectToPreviewURL.js";
|
|
7
8
|
import { PrismicNextImage, PrismicNextImageProps } from "./PrismicNextImage.js";
|
|
@@ -12,4 +13,4 @@ import { CreateLocaleRedirectConfig, createLocaleRedirect } from "./createLocale
|
|
|
12
13
|
import { getPrismicCacheTags } from "./getPrismicCacheTags.js";
|
|
13
14
|
import { cacheTagPrismicPages } from "./cacheTagPrismicPages.js";
|
|
14
15
|
import { revalidatePrismicPages } from "./revalidatePrismicPages.js";
|
|
15
|
-
export { type CreateClientConfig, type CreateLocaleRedirectConfig, type EnableAutoPreviewsConfig, PrismicNextImage, type PrismicNextImageProps, PrismicNextLink, type PrismicNextLinkProps, PrismicPreview, type PrismicPreviewProps, type RedirectToPreviewURLConfig, SliceSimulator, type SliceSimulatorParams, type SliceSimulatorProps, cacheTagPrismicPages, createLocaleRedirect, enableAutoPreviews, exitPreview, getPrismicCacheTags, getSlices, imgixLoader, redirectToPreviewURL, revalidatePrismicPages };
|
|
16
|
+
export { type CreateClientConfig, type CreateLocaleRedirectConfig, type EnableAutoPreviewsConfig, PrismicNextImage, type PrismicNextImageProps, PrismicNextLink, type PrismicNextLinkProps, PrismicPreview, type PrismicPreviewProps, type RedirectToPreviewURLConfig, SliceSimulator, type SliceSimulatorParams, type SliceSimulatorProps, cacheTagPrismicPages, createLocaleRedirect, enableAutoPreviews, exitPreview, getPreviewRef, getPrismicCacheTags, getSlices, imgixLoader, redirectToPreviewURL, revalidatePrismicPages };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { exitPreview } from "./exitPreview.js";
|
|
2
2
|
import { PrismicPreview } from "./PrismicPreview.js";
|
|
3
3
|
import { PrismicNextLink } from "./PrismicNextLink.js";
|
|
4
|
+
import { getPreviewRef } from "./getPreviewRef.js";
|
|
4
5
|
import { enableAutoPreviews } from "./enableAutoPreviews.js";
|
|
5
6
|
import { redirectToPreviewURL } from "./redirectToPreviewURL.js";
|
|
6
7
|
import { imgixLoader } from "./imgixLoader.js";
|
|
@@ -11,4 +12,4 @@ import { createLocaleRedirect } from "./createLocaleRedirect.js";
|
|
|
11
12
|
import { getPrismicCacheTags } from "./getPrismicCacheTags.js";
|
|
12
13
|
import { cacheTagPrismicPages } from "./cacheTagPrismicPages.js";
|
|
13
14
|
import { revalidatePrismicPages } from "./revalidatePrismicPages.js";
|
|
14
|
-
export { PrismicNextImage, PrismicNextLink, PrismicPreview, SliceSimulator, cacheTagPrismicPages, createLocaleRedirect, enableAutoPreviews, exitPreview, getPrismicCacheTags, getSlices, imgixLoader, redirectToPreviewURL, revalidatePrismicPages };
|
|
15
|
+
export { PrismicNextImage, PrismicNextLink, PrismicPreview, SliceSimulator, cacheTagPrismicPages, createLocaleRedirect, enableAutoPreviews, exitPreview, getPreviewRef, getPrismicCacheTags, getSlices, imgixLoader, redirectToPreviewURL, revalidatePrismicPages };
|
package/dist/package.cjs
CHANGED
package/dist/package.js
CHANGED
package/package.json
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Client } from "@prismicio/client"
|
|
2
|
+
|
|
3
|
+
import { getPreviewRef } from "./getPreviewRef"
|
|
2
4
|
|
|
3
5
|
/** Configuration for `enableAutoPreviews`. */
|
|
4
6
|
export type EnableAutoPreviewsConfig = {
|
|
@@ -17,36 +19,5 @@ export type EnableAutoPreviewsConfig = {
|
|
|
17
19
|
export function enableAutoPreviews(config: EnableAutoPreviewsConfig): void {
|
|
18
20
|
// We use a function value so the cookie is checked on every
|
|
19
21
|
// request. We don't have a static value to read from.
|
|
20
|
-
config.client.queryContentFromRef(
|
|
21
|
-
// Need this to avoid the following Next.js build-time error:
|
|
22
|
-
// You're importing a component that needs next/headers. That only works
|
|
23
|
-
// in a Server Component which is not supported in the pages/ directory.
|
|
24
|
-
const { cookies, draftMode } = await import("next/headers")
|
|
25
|
-
|
|
26
|
-
let isDraftModeEnabled = false
|
|
27
|
-
try {
|
|
28
|
-
isDraftModeEnabled = (await draftMode()).isEnabled
|
|
29
|
-
} catch {
|
|
30
|
-
// `draftMode()` may have been called in a palce that
|
|
31
|
-
// does not have access to its async storage. This
|
|
32
|
-
// occurs in places like `generateStaticParams()`. We
|
|
33
|
-
// can ignore this case.
|
|
34
|
-
return
|
|
35
|
-
}
|
|
36
|
-
if (!isDraftModeEnabled) {
|
|
37
|
-
return
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const cookie = (await cookies()).get(prismicCookie.preview)?.value
|
|
41
|
-
if (!cookie) {
|
|
42
|
-
return
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const isActiveCookie = cookie.includes("websitePreviewId=")
|
|
46
|
-
if (!isActiveCookie) {
|
|
47
|
-
return
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return cookie
|
|
51
|
-
})
|
|
22
|
+
config.client.queryContentFromRef(() => getPreviewRef())
|
|
52
23
|
}
|
package/src/exitPreview.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { cookie as prismicCookie } from "@prismicio/client"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Ends a Prismic preview session within a Next.js app. This function should be used in a Router
|
|
3
5
|
* Handler.
|
|
@@ -17,10 +19,14 @@ export async function exitPreview(): Promise<Response> {
|
|
|
17
19
|
// Need this to avoid the following Next.js build-time error:
|
|
18
20
|
// You're importing a component that needs next/headers. That only works
|
|
19
21
|
// in a Server Component which is not supported in the pages/ directory.
|
|
20
|
-
const { draftMode } = await import("next/headers")
|
|
22
|
+
const { cookies, draftMode } = await import("next/headers")
|
|
21
23
|
|
|
22
24
|
;(await draftMode()).disable()
|
|
23
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
|
+
|
|
24
30
|
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
25
31
|
return new Response(JSON.stringify({ success: true }), {
|
|
26
32
|
headers: {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { cookie as prismicCookie } from "@prismicio/client"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Reads the Prismic preview ref for the current request when an active preview
|
|
5
|
+
* session exists.
|
|
6
|
+
*
|
|
7
|
+
* This is the read-side counterpart to `redirectToPreviewURL`, which writes the
|
|
8
|
+
* preview cookie. Use it with Next.js Cache Components to read the ref _outside_
|
|
9
|
+
* a cached function and pass it _in_ as an argument so it becomes part of the
|
|
10
|
+
* cache key:
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ;```typescript
|
|
14
|
+
* import { getPreviewRef } from "@prismicio/next"
|
|
15
|
+
*
|
|
16
|
+
* const page = await fetchPage(uid, await getPreviewRef())
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* @returns The active preview ref, or `undefined` if no preview session is active.
|
|
20
|
+
*/
|
|
21
|
+
export async function getPreviewRef(): Promise<string | undefined> {
|
|
22
|
+
// Need this to avoid the following Next.js build-time error:
|
|
23
|
+
// You're importing a component that needs next/headers. That only works
|
|
24
|
+
// in a Server Component which is not supported in the pages/ directory.
|
|
25
|
+
const { cookies, draftMode } = await import("next/headers")
|
|
26
|
+
|
|
27
|
+
let isDraftModeEnabled = false
|
|
28
|
+
try {
|
|
29
|
+
isDraftModeEnabled = (await draftMode()).isEnabled
|
|
30
|
+
} catch {
|
|
31
|
+
// `draftMode()` may have been called in a place that
|
|
32
|
+
// does not have access to its async storage. This
|
|
33
|
+
// occurs in places like `generateStaticParams()`. We
|
|
34
|
+
// can ignore this case.
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
if (!isDraftModeEnabled) {
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const cookie = (await cookies()).get(prismicCookie.preview)?.value
|
|
42
|
+
if (!cookie) {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const isActiveCookie = cookie.includes("websitePreviewId=")
|
|
47
|
+
if (!isActiveCookie) {
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return cookie
|
|
52
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -9,6 +9,8 @@ export type { PrismicNextLinkProps } from "./PrismicNextLink"
|
|
|
9
9
|
export { enableAutoPreviews } from "./enableAutoPreviews"
|
|
10
10
|
export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews"
|
|
11
11
|
|
|
12
|
+
export { getPreviewRef } from "./getPreviewRef"
|
|
13
|
+
|
|
12
14
|
export { redirectToPreviewURL } from "./redirectToPreviewURL"
|
|
13
15
|
export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL"
|
|
14
16
|
|