@prismicio/next 2.3.1-pr.147.bf74d52 → 2.3.1-pr.147.f16ccf2
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/getPreviewRef.cjs +2 -2
- package/dist/getPreviewRef.cjs.map +1 -1
- package/dist/getPreviewRef.d.cts.map +1 -1
- package/dist/getPreviewRef.d.ts.map +1 -1
- package/dist/getPreviewRef.js +2 -2
- package/dist/getPreviewRef.js.map +1 -1
- package/dist/lib/previewRefFromCookie.cjs +24 -0
- package/dist/lib/previewRefFromCookie.cjs.map +1 -0
- package/dist/lib/previewRefFromCookie.js +24 -0
- package/dist/lib/previewRefFromCookie.js.map +1 -0
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/pages/setPreviewData.cjs +9 -1
- package/dist/pages/setPreviewData.cjs.map +1 -1
- package/dist/pages/setPreviewData.d.cts.map +1 -1
- package/dist/pages/setPreviewData.d.ts.map +1 -1
- package/dist/pages/setPreviewData.js +9 -1
- package/dist/pages/setPreviewData.js.map +1 -1
- package/package.json +1 -1
- package/src/getPreviewRef.ts +6 -6
- package/src/lib/previewRefFromCookie.ts +33 -0
- package/src/pages/setPreviewData.ts +13 -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/getPreviewRef.cjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require("./_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
const require_previewRefFromCookie = require("./lib/previewRefFromCookie.cjs");
|
|
2
3
|
let _prismicio_client = require("@prismicio/client");
|
|
3
4
|
//#region src/getPreviewRef.ts
|
|
4
5
|
/**
|
|
@@ -29,8 +30,7 @@ async function getPreviewRef() {
|
|
|
29
30
|
if (!isDraftModeEnabled) return;
|
|
30
31
|
const cookie = (await cookies()).get(_prismicio_client.cookie.preview)?.value;
|
|
31
32
|
if (!cookie) return;
|
|
32
|
-
|
|
33
|
-
return cookie;
|
|
33
|
+
return require_previewRefFromCookie.previewRefFromCookie(cookie);
|
|
34
34
|
}
|
|
35
35
|
//#endregion
|
|
36
36
|
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\
|
|
1
|
+
{"version":3,"file":"getPreviewRef.cjs","names":["prismicCookie","previewRefFromCookie"],"sources":["../src/getPreviewRef.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\nimport { previewRefFromCookie } from \"./lib/previewRefFromCookie\"\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"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAqBA,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,QAAOC,6BAAAA,qBAAqB,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPreviewRef.d.cts","names":[],"sources":["../src/getPreviewRef.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"getPreviewRef.d.cts","names":[],"sources":["../src/getPreviewRef.ts"],"mappings":";;AAqBA;;;;;;;;;;;;;;;;iBAAsB,aAAA,CAAA,GAAiB,OAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPreviewRef.d.ts","names":[],"sources":["../src/getPreviewRef.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"getPreviewRef.d.ts","names":[],"sources":["../src/getPreviewRef.ts"],"mappings":";;AAqBA;;;;;;;;;;;;;;;;iBAAsB,aAAA,CAAA,GAAiB,OAAA"}
|
package/dist/getPreviewRef.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { previewRefFromCookie } from "./lib/previewRefFromCookie.js";
|
|
1
2
|
import { cookie } from "@prismicio/client";
|
|
2
3
|
//#region src/getPreviewRef.ts
|
|
3
4
|
/**
|
|
@@ -28,8 +29,7 @@ async function getPreviewRef() {
|
|
|
28
29
|
if (!isDraftModeEnabled) return;
|
|
29
30
|
const cookie$1 = (await cookies()).get(cookie.preview)?.value;
|
|
30
31
|
if (!cookie$1) return;
|
|
31
|
-
|
|
32
|
-
return cookie$1;
|
|
32
|
+
return previewRefFromCookie(cookie$1);
|
|
33
33
|
}
|
|
34
34
|
//#endregion
|
|
35
35
|
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\
|
|
1
|
+
{"version":3,"file":"getPreviewRef.js","names":["cookie","prismicCookie"],"sources":["../src/getPreviewRef.ts"],"sourcesContent":["import { cookie as prismicCookie } from \"@prismicio/client\"\n\nimport { previewRefFromCookie } from \"./lib/previewRefFromCookie\"\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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqBA,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"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/lib/previewRefFromCookie.ts
|
|
2
|
+
/**
|
|
3
|
+
* Unwraps `io.prismic.preview` to a Content API ref.
|
|
4
|
+
*
|
|
5
|
+
* After toolbar `PreviewCookie.sync` / `upsertPreviewForDomain`, the cookie is
|
|
6
|
+
* `{ [domain]: { preview: string } }`. Returning that jar makes `enableAutoPreviews`
|
|
7
|
+
* / Cache Components send a non-ref (ParsingError / 404). A jar with more than one
|
|
8
|
+
* repository `preview` is ambiguous and is ignored.
|
|
9
|
+
*/
|
|
10
|
+
function previewRefFromCookie(cookie) {
|
|
11
|
+
try {
|
|
12
|
+
const parsed = JSON.parse(cookie);
|
|
13
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return;
|
|
14
|
+
const previewRefs = [];
|
|
15
|
+
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);
|
|
16
|
+
return previewRefs.length === 1 ? previewRefs[0] : void 0;
|
|
17
|
+
} catch {
|
|
18
|
+
return cookie;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
exports.previewRefFromCookie = previewRefFromCookie;
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=previewRefFromCookie.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"previewRefFromCookie.cjs","names":[],"sources":["../../src/lib/previewRefFromCookie.ts"],"sourcesContent":["/**\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). A jar with more than one\n * repository `preview` is ambiguous and is ignored.\n */\nexport function 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":";;;;;;;;;AAQA,SAAgB,qBAAqB,QAAoC;AACxE,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,24 @@
|
|
|
1
|
+
//#region src/lib/previewRefFromCookie.ts
|
|
2
|
+
/**
|
|
3
|
+
* Unwraps `io.prismic.preview` to a Content API ref.
|
|
4
|
+
*
|
|
5
|
+
* After toolbar `PreviewCookie.sync` / `upsertPreviewForDomain`, the cookie is
|
|
6
|
+
* `{ [domain]: { preview: string } }`. Returning that jar makes `enableAutoPreviews`
|
|
7
|
+
* / Cache Components send a non-ref (ParsingError / 404). A jar with more than one
|
|
8
|
+
* repository `preview` is ambiguous and is ignored.
|
|
9
|
+
*/
|
|
10
|
+
function previewRefFromCookie(cookie) {
|
|
11
|
+
try {
|
|
12
|
+
const parsed = JSON.parse(cookie);
|
|
13
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) return;
|
|
14
|
+
const previewRefs = [];
|
|
15
|
+
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);
|
|
16
|
+
return previewRefs.length === 1 ? previewRefs[0] : void 0;
|
|
17
|
+
} catch {
|
|
18
|
+
return cookie;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { previewRefFromCookie };
|
|
23
|
+
|
|
24
|
+
//# sourceMappingURL=previewRefFromCookie.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"previewRefFromCookie.js","names":[],"sources":["../../src/lib/previewRefFromCookie.ts"],"sourcesContent":["/**\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). A jar with more than one\n * repository `preview` is ambiguous and is ignored.\n */\nexport function 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":";;;;;;;;;AAQA,SAAgB,qBAAqB,QAAoC;AACxE,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/package.cjs
CHANGED
package/dist/package.js
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
require("../_virtual/_rolldown/runtime.cjs");
|
|
2
|
+
const require_previewRefFromCookie = require("../lib/previewRefFromCookie.cjs");
|
|
2
3
|
let _prismicio_client = require("@prismicio/client");
|
|
3
4
|
//#region src/pages/setPreviewData.ts
|
|
4
5
|
/** Set Prismic preview data for Next.js's Preview Mode. */
|
|
5
6
|
function setPreviewData({ req, res }) {
|
|
6
|
-
const
|
|
7
|
+
const token = req.query.token;
|
|
8
|
+
if (token) {
|
|
9
|
+
res.setPreviewData({ ref: token });
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
const previewCookie = req.cookies[_prismicio_client.cookie.preview];
|
|
13
|
+
if (!previewCookie) return;
|
|
14
|
+
const ref = require_previewRefFromCookie.previewRefFromCookie(previewCookie);
|
|
7
15
|
if (ref) res.setPreviewData({ ref });
|
|
8
16
|
}
|
|
9
17
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setPreviewData.cjs","names":["cookie"],"sources":["../../src/pages/setPreviewData.ts"],"sourcesContent":["import { cookie } from \"@prismicio/client\"\n\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\n/** Configuration for `setPreviewData`. */\nexport type SetPreviewDataConfig = {\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/** Set Prismic preview data for Next.js's Preview Mode. */\nexport function setPreviewData({ req, res }: SetPreviewDataConfig): void {\n\tconst
|
|
1
|
+
{"version":3,"file":"setPreviewData.cjs","names":["cookie","previewRefFromCookie"],"sources":["../../src/pages/setPreviewData.ts"],"sourcesContent":["import { cookie } from \"@prismicio/client\"\n\nimport { previewRefFromCookie } from \"../lib/previewRefFromCookie\"\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\n/** Configuration for `setPreviewData`. */\nexport type SetPreviewDataConfig = {\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/** Set Prismic preview data for Next.js's Preview Mode. */\nexport function setPreviewData({ req, res }: SetPreviewDataConfig): void {\n\tconst token = req.query.token\n\tif (token) {\n\t\tres.setPreviewData({ ref: token })\n\t\treturn\n\t}\n\n\t// Cookie-only path: unwrap the toolbar jar. `token` stays as-is above.\n\tconst previewCookie = req.cookies[cookie.preview]\n\tif (!previewCookie) {\n\t\treturn\n\t}\n\n\tconst ref = previewRefFromCookie(previewCookie)\n\tif (ref) {\n\t\tres.setPreviewData({ ref })\n\t}\n}\n"],"mappings":";;;;;AAuBA,SAAgB,eAAe,EAAE,KAAK,OAAmC;CACxE,MAAM,QAAQ,IAAI,MAAM;AACxB,KAAI,OAAO;AACV,MAAI,eAAe,EAAE,KAAK,OAAO,CAAC;AAClC;;CAID,MAAM,gBAAgB,IAAI,QAAQA,kBAAAA,OAAO;AACzC,KAAI,CAAC,cACJ;CAGD,MAAM,MAAMC,6BAAAA,qBAAqB,cAAc;AAC/C,KAAI,IACH,KAAI,eAAe,EAAE,KAAK,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setPreviewData.d.cts","names":[],"sources":["../../src/pages/setPreviewData.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"setPreviewData.d.cts","names":[],"sources":["../../src/pages/setPreviewData.ts"],"mappings":";;;;KAMY,oBAAA;EAAA;;;;;EAMX,GAAA,EAAK,kBAAA;EAOL;;;;AAID;EAJC,GAAA,EAAK,mBAAA;AAAA;;iBAIU,cAAA,CAAA;EAAiB,GAAA;EAAK;AAAA,GAAO,oBAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setPreviewData.d.ts","names":[],"sources":["../../src/pages/setPreviewData.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"setPreviewData.d.ts","names":[],"sources":["../../src/pages/setPreviewData.ts"],"mappings":";;;;KAMY,oBAAA;EAAA;;;;;EAMX,GAAA,EAAK,kBAAA;EAOL;;;;AAID;EAJC,GAAA,EAAK,mBAAA;AAAA;;iBAIU,cAAA,CAAA;EAAiB,GAAA;EAAK;AAAA,GAAO,oBAAA"}
|
|
@@ -1,8 +1,16 @@
|
|
|
1
|
+
import { previewRefFromCookie } from "../lib/previewRefFromCookie.js";
|
|
1
2
|
import { cookie } from "@prismicio/client";
|
|
2
3
|
//#region src/pages/setPreviewData.ts
|
|
3
4
|
/** Set Prismic preview data for Next.js's Preview Mode. */
|
|
4
5
|
function setPreviewData({ req, res }) {
|
|
5
|
-
const
|
|
6
|
+
const token = req.query.token;
|
|
7
|
+
if (token) {
|
|
8
|
+
res.setPreviewData({ ref: token });
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
const previewCookie = req.cookies[cookie.preview];
|
|
12
|
+
if (!previewCookie) return;
|
|
13
|
+
const ref = previewRefFromCookie(previewCookie);
|
|
6
14
|
if (ref) res.setPreviewData({ ref });
|
|
7
15
|
}
|
|
8
16
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setPreviewData.js","names":[],"sources":["../../src/pages/setPreviewData.ts"],"sourcesContent":["import { cookie } from \"@prismicio/client\"\n\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\n/** Configuration for `setPreviewData`. */\nexport type SetPreviewDataConfig = {\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/** Set Prismic preview data for Next.js's Preview Mode. */\nexport function setPreviewData({ req, res }: SetPreviewDataConfig): void {\n\tconst
|
|
1
|
+
{"version":3,"file":"setPreviewData.js","names":[],"sources":["../../src/pages/setPreviewData.ts"],"sourcesContent":["import { cookie } from \"@prismicio/client\"\n\nimport { previewRefFromCookie } from \"../lib/previewRefFromCookie\"\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types\"\n\n/** Configuration for `setPreviewData`. */\nexport type SetPreviewDataConfig = {\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/** Set Prismic preview data for Next.js's Preview Mode. */\nexport function setPreviewData({ req, res }: SetPreviewDataConfig): void {\n\tconst token = req.query.token\n\tif (token) {\n\t\tres.setPreviewData({ ref: token })\n\t\treturn\n\t}\n\n\t// Cookie-only path: unwrap the toolbar jar. `token` stays as-is above.\n\tconst previewCookie = req.cookies[cookie.preview]\n\tif (!previewCookie) {\n\t\treturn\n\t}\n\n\tconst ref = previewRefFromCookie(previewCookie)\n\tif (ref) {\n\t\tres.setPreviewData({ ref })\n\t}\n}\n"],"mappings":";;;;AAuBA,SAAgB,eAAe,EAAE,KAAK,OAAmC;CACxE,MAAM,QAAQ,IAAI,MAAM;AACxB,KAAI,OAAO;AACV,MAAI,eAAe,EAAE,KAAK,OAAO,CAAC;AAClC;;CAID,MAAM,gBAAgB,IAAI,QAAQ,OAAO;AACzC,KAAI,CAAC,cACJ;CAGD,MAAM,MAAM,qBAAqB,cAAc;AAC/C,KAAI,IACH,KAAI,eAAe,EAAE,KAAK,CAAC"}
|
package/package.json
CHANGED
package/src/getPreviewRef.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { cookie as prismicCookie } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
+
import { previewRefFromCookie } from "./lib/previewRefFromCookie"
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Reads the Prismic preview ref for the current request when an active preview session exists.
|
|
5
7
|
*
|
|
@@ -42,10 +44,8 @@ export async function getPreviewRef(): Promise<string | undefined> {
|
|
|
42
44
|
return
|
|
43
45
|
}
|
|
44
46
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
return cookie
|
|
47
|
+
// Draft Mode gates preview reads. The cookie may be a raw token or the
|
|
48
|
+
// toolbar jar (`{ "<repo>.prismic.io": { preview } }`). Only `preview` is a
|
|
49
|
+
// Content API ref; the jar string is not.
|
|
50
|
+
return previewRefFromCookie(cookie)
|
|
51
51
|
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unwraps `io.prismic.preview` to a Content API ref.
|
|
3
|
+
*
|
|
4
|
+
* After toolbar `PreviewCookie.sync` / `upsertPreviewForDomain`, the cookie is
|
|
5
|
+
* `{ [domain]: { preview: string } }`. Returning that jar makes `enableAutoPreviews`
|
|
6
|
+
* / Cache Components send a non-ref (ParsingError / 404). A jar with more than one
|
|
7
|
+
* repository `preview` is ambiguous and is ignored.
|
|
8
|
+
*/
|
|
9
|
+
export function previewRefFromCookie(cookie: string): string | undefined {
|
|
10
|
+
try {
|
|
11
|
+
const parsed: unknown = JSON.parse(cookie)
|
|
12
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
13
|
+
return
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const previewRefs: string[] = []
|
|
17
|
+
for (const value of Object.values(parsed)) {
|
|
18
|
+
if (
|
|
19
|
+
value !== null &&
|
|
20
|
+
typeof value === "object" &&
|
|
21
|
+
!Array.isArray(value) &&
|
|
22
|
+
"preview" in value &&
|
|
23
|
+
typeof value.preview === "string"
|
|
24
|
+
) {
|
|
25
|
+
previewRefs.push(value.preview)
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return previewRefs.length === 1 ? previewRefs[0] : undefined
|
|
30
|
+
} catch {
|
|
31
|
+
return cookie
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { cookie } from "@prismicio/client"
|
|
2
2
|
|
|
3
|
+
import { previewRefFromCookie } from "../lib/previewRefFromCookie"
|
|
3
4
|
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
4
5
|
|
|
5
6
|
/** Configuration for `setPreviewData`. */
|
|
@@ -21,8 +22,19 @@ export type SetPreviewDataConfig = {
|
|
|
21
22
|
|
|
22
23
|
/** Set Prismic preview data for Next.js's Preview Mode. */
|
|
23
24
|
export function setPreviewData({ req, res }: SetPreviewDataConfig): void {
|
|
24
|
-
const
|
|
25
|
+
const token = req.query.token
|
|
26
|
+
if (token) {
|
|
27
|
+
res.setPreviewData({ ref: token })
|
|
28
|
+
return
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Cookie-only path: unwrap the toolbar jar. `token` stays as-is above.
|
|
32
|
+
const previewCookie = req.cookies[cookie.preview]
|
|
33
|
+
if (!previewCookie) {
|
|
34
|
+
return
|
|
35
|
+
}
|
|
25
36
|
|
|
37
|
+
const ref = previewRefFromCookie(previewCookie)
|
|
26
38
|
if (ref) {
|
|
27
39
|
res.setPreviewData({ ref })
|
|
28
40
|
}
|