@prismicio/next 1.2.1 → 1.3.0-alpha.1
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/PrismicNextImage.d.ts +1 -1
- package/dist/PrismicNextLink.d.ts +4 -4
- package/dist/PrismicPreview.cjs +3 -1
- package/dist/PrismicPreview.cjs.map +1 -1
- package/dist/PrismicPreview.d.ts +1 -1
- package/dist/PrismicPreview.js +3 -1
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/PrismicPreviewClient.cjs +9 -13
- package/dist/PrismicPreviewClient.cjs.map +1 -1
- package/dist/PrismicPreviewClient.d.ts +4 -2
- package/dist/PrismicPreviewClient.js +9 -13
- package/dist/PrismicPreviewClient.js.map +1 -1
- package/dist/enableAutoPreviews.cjs +9 -0
- package/dist/enableAutoPreviews.cjs.map +1 -1
- package/dist/enableAutoPreviews.d.ts +1 -1
- package/dist/enableAutoPreviews.js +10 -1
- package/dist/enableAutoPreviews.js.map +1 -1
- package/dist/exitPreview.cjs +14 -2
- package/dist/exitPreview.cjs.map +1 -1
- package/dist/exitPreview.d.ts +37 -7
- package/dist/exitPreview.js +14 -2
- package/dist/exitPreview.js.map +1 -1
- package/dist/lib/getPreviewCookieRepositoryName.cjs +1 -1
- package/dist/lib/getPreviewCookieRepositoryName.cjs.map +1 -1
- package/dist/lib/getPreviewCookieRepositoryName.js +1 -1
- package/dist/lib/getPreviewCookieRepositoryName.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/redirectToPreviewURL.d.ts +1 -1
- package/dist/setPreviewData.d.ts +1 -1
- package/dist/types.d.ts +6 -6
- package/package.json +3 -7
- package/src/PrismicPreview.tsx +8 -2
- package/src/PrismicPreviewClient.tsx +17 -20
- package/src/enableAutoPreviews.ts +21 -8
- package/src/exitPreview.ts +62 -13
- package/src/lib/getPreviewCookieRepositoryName.ts +1 -1
- package/src/types.ts +1 -1
- package/bin/prismic-next.js +0 -3
- package/dist/cli/index.d.ts +0 -1
- package/dist/cli.cjs +0 -123
- package/dist/cli.cjs.map +0 -1
- package/dist/cli.js +0 -104
- package/dist/cli.js.map +0 -1
- package/src/cli/index.ts +0 -168
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { ImageProps } from "next/image";
|
|
3
3
|
import { ImgixURLParams } from "imgix-url-builder";
|
|
4
4
|
import * as prismic from "@prismicio/client";
|
|
5
|
-
export
|
|
5
|
+
export type PrismicNextImageProps = Omit<ImageProps, "src" | "alt"> & {
|
|
6
6
|
/**
|
|
7
7
|
* The Prismic Image field or thumbnail to render.
|
|
8
8
|
*/
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import * as React from "react";
|
|
3
3
|
import * as prismic from "@prismicio/client";
|
|
4
4
|
import Link from "next/link";
|
|
5
|
-
export
|
|
5
|
+
export type PrismicNextLinkProps = Omit<React.ComponentProps<typeof Link>, "field" | "document" | "href" | "rel"> & {
|
|
6
6
|
linkResolver?: prismic.LinkResolverFunction;
|
|
7
7
|
rel?: string | prismic.AsLinkAttrsConfig["rel"];
|
|
8
8
|
} & ({
|
|
@@ -47,7 +47,7 @@ export declare const PrismicNextLink: React.ForwardRefExoticComponent<(Pick<Omit
|
|
|
47
47
|
} & {
|
|
48
48
|
children?: React.ReactNode;
|
|
49
49
|
} & React.RefAttributes<HTMLAnchorElement>, "field" | "document" | "href" | "rel"> & {
|
|
50
|
-
linkResolver?: prismic.LinkResolverFunction
|
|
50
|
+
linkResolver?: prismic.LinkResolverFunction | undefined;
|
|
51
51
|
rel?: string | prismic.AsLinkAttrsConfig["rel"];
|
|
52
52
|
} & {
|
|
53
53
|
field: prismic.LinkField | null | undefined;
|
|
@@ -82,7 +82,7 @@ export declare const PrismicNextLink: React.ForwardRefExoticComponent<(Pick<Omit
|
|
|
82
82
|
} & {
|
|
83
83
|
children?: React.ReactNode;
|
|
84
84
|
} & React.RefAttributes<HTMLAnchorElement>, "field" | "document" | "href" | "rel"> & {
|
|
85
|
-
linkResolver?: prismic.LinkResolverFunction
|
|
85
|
+
linkResolver?: prismic.LinkResolverFunction | undefined;
|
|
86
86
|
rel?: string | prismic.AsLinkAttrsConfig["rel"];
|
|
87
87
|
} & {
|
|
88
88
|
field?: undefined;
|
|
@@ -117,7 +117,7 @@ export declare const PrismicNextLink: React.ForwardRefExoticComponent<(Pick<Omit
|
|
|
117
117
|
} & {
|
|
118
118
|
children?: React.ReactNode;
|
|
119
119
|
} & React.RefAttributes<HTMLAnchorElement>, "field" | "document" | "href" | "rel"> & {
|
|
120
|
-
linkResolver?: prismic.LinkResolverFunction
|
|
120
|
+
linkResolver?: prismic.LinkResolverFunction | undefined;
|
|
121
121
|
rel?: string | prismic.AsLinkAttrsConfig["rel"];
|
|
122
122
|
} & {
|
|
123
123
|
field?: undefined;
|
package/dist/PrismicPreview.cjs
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const jsxRuntime = require("react/jsx-runtime");
|
|
4
4
|
const Script = require("next/script");
|
|
5
|
+
const headers = require("next/headers");
|
|
5
6
|
const PrismicPreviewClient = require("./PrismicPreviewClient.cjs");
|
|
6
7
|
const getToolbarSrc = require('./_node_modules/@prismicio/client/dist/getToolbarSrc.cjs');
|
|
7
8
|
function PrismicPreview({ repositoryName, children, ...props }) {
|
|
8
9
|
const toolbarSrc = getToolbarSrc.getToolbarSrc(repositoryName);
|
|
9
|
-
|
|
10
|
+
const isDraftMode = headers.draftMode().isEnabled;
|
|
11
|
+
return jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [children, jsxRuntime.jsx(PrismicPreviewClient.PrismicPreviewClient, { repositoryName, isDraftMode, ...props }), jsxRuntime.jsx(Script, { src: toolbarSrc, strategy: "lazyOnload" })] });
|
|
10
12
|
}
|
|
11
13
|
exports.PrismicPreview = PrismicPreview;
|
|
12
14
|
//# sourceMappingURL=PrismicPreview.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreview.cjs","sources":["../../src/PrismicPreview.tsx"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"PrismicPreview.cjs","sources":["../../src/PrismicPreview.tsx"],"sourcesContent":["import Script from \"next/script\";\nimport { draftMode } from \"next/headers\";\nimport * as React from \"react\";\nimport * as prismic from \"@prismicio/client\";\n\nimport { PrismicPreviewClient } from \"./PrismicPreviewClient\";\n\n/**\n * Props for `<PrismicPreview>`.\n */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered\n\t * using this repository.\n\t */\n\trepositoryName: string;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\n\t *\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`).\n\t * This URL will be fetched on preview update events.\n\t */\n\tupdatePreviewURL?: string;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\n\t *\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`).\n\t * This URL will be fetched on preview exit events.\n\t */\n\texitPreviewURL?: string;\n\n\t/**\n\t * Children to render adjacent to the Prismic Toolbar.\n\t */\n\tchildren?: React.ReactNode;\n};\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When\n * the Prismic Toolbar send events to the browser, such as on preview updates\n * and exiting, this component will automatically refresh the page with the\n * changes.\n *\n * This component can be wrapped around your app or added anywhere in your app's\n * tree. It must be rendered on every page.\n */\nexport function PrismicPreview({\n\trepositoryName,\n\tchildren,\n\t...props\n}: PrismicPreviewProps): JSX.Element {\n\tconst toolbarSrc = prismic.getToolbarSrc(repositoryName);\n\tconst isDraftMode = draftMode().isEnabled;\n\n\treturn (\n\t\t<>\n\t\t\t{children}\n\t\t\t<PrismicPreviewClient\n\t\t\t\trepositoryName={repositoryName}\n\t\t\t\tisDraftMode={isDraftMode}\n\t\t\t\t{...props}\n\t\t\t/>\n\t\t\t<Script src={toolbarSrc} strategy=\"lazyOnload\" />\n\t\t</>\n\t);\n}\n"],"names":["prismic.getToolbarSrc","draftMode","_jsxs","_Fragment","_jsx","PrismicPreviewClient"],"mappings":";;;;;;;AAgDM,SAAU,eAAe,EAC9B,gBACA,UACA,GAAG,SACkB;AACf,QAAA,aAAaA,4BAAsB,cAAc;AACjD,QAAA,cAAcC,kBAAY,EAAA;AAG/B,SAAAC,WAAA,KAAAC,qBAAA,EAAA,UAAA,CACE,UACDC,WAAAA,IAACC,qBAAA,sBACA,EAAA,gBACA,aAAwB,GACpB,OAAK,GAEVD,WAAAA,IAAC,QAAM,EAAC,KAAK,YAAY,UAAS,aAAe,CAAA,CAAA,EAAA,CAAA;AAGpD;;"}
|
package/dist/PrismicPreview.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import * as React from "react";
|
|
|
2
2
|
/**
|
|
3
3
|
* Props for `<PrismicPreview>`.
|
|
4
4
|
*/
|
|
5
|
-
export
|
|
5
|
+
export type PrismicPreviewProps = {
|
|
6
6
|
/**
|
|
7
7
|
* The name of your Prismic repository. A Prismic Toolbar will be registered
|
|
8
8
|
* using this repository.
|
package/dist/PrismicPreview.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { jsxs, Fragment, jsx } from "react/jsx-runtime";
|
|
2
2
|
import Script from "next/script";
|
|
3
|
+
import { draftMode } from "next/headers";
|
|
3
4
|
import { PrismicPreviewClient } from "./PrismicPreviewClient.js";
|
|
4
5
|
import { getToolbarSrc } from './_node_modules/@prismicio/client/dist/getToolbarSrc.js';
|
|
5
6
|
function PrismicPreview({ repositoryName, children, ...props }) {
|
|
6
7
|
const toolbarSrc = getToolbarSrc(repositoryName);
|
|
7
|
-
|
|
8
|
+
const isDraftMode = draftMode().isEnabled;
|
|
9
|
+
return jsxs(Fragment, { children: [children, jsx(PrismicPreviewClient, { repositoryName, isDraftMode, ...props }), jsx(Script, { src: toolbarSrc, strategy: "lazyOnload" })] });
|
|
8
10
|
}
|
|
9
11
|
export {
|
|
10
12
|
PrismicPreview
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreview.js","sources":["../../src/PrismicPreview.tsx"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"PrismicPreview.js","sources":["../../src/PrismicPreview.tsx"],"sourcesContent":["import Script from \"next/script\";\nimport { draftMode } from \"next/headers\";\nimport * as React from \"react\";\nimport * as prismic from \"@prismicio/client\";\n\nimport { PrismicPreviewClient } from \"./PrismicPreviewClient\";\n\n/**\n * Props for `<PrismicPreview>`.\n */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered\n\t * using this repository.\n\t */\n\trepositoryName: string;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\n\t *\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`).\n\t * This URL will be fetched on preview update events.\n\t */\n\tupdatePreviewURL?: string;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\n\t *\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`).\n\t * This URL will be fetched on preview exit events.\n\t */\n\texitPreviewURL?: string;\n\n\t/**\n\t * Children to render adjacent to the Prismic Toolbar.\n\t */\n\tchildren?: React.ReactNode;\n};\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When\n * the Prismic Toolbar send events to the browser, such as on preview updates\n * and exiting, this component will automatically refresh the page with the\n * changes.\n *\n * This component can be wrapped around your app or added anywhere in your app's\n * tree. It must be rendered on every page.\n */\nexport function PrismicPreview({\n\trepositoryName,\n\tchildren,\n\t...props\n}: PrismicPreviewProps): JSX.Element {\n\tconst toolbarSrc = prismic.getToolbarSrc(repositoryName);\n\tconst isDraftMode = draftMode().isEnabled;\n\n\treturn (\n\t\t<>\n\t\t\t{children}\n\t\t\t<PrismicPreviewClient\n\t\t\t\trepositoryName={repositoryName}\n\t\t\t\tisDraftMode={isDraftMode}\n\t\t\t\t{...props}\n\t\t\t/>\n\t\t\t<Script src={toolbarSrc} strategy=\"lazyOnload\" />\n\t\t</>\n\t);\n}\n"],"names":["prismic.getToolbarSrc","_jsxs","_Fragment","_jsx"],"mappings":";;;;;AAgDM,SAAU,eAAe,EAC9B,gBACA,UACA,GAAG,SACkB;AACf,QAAA,aAAaA,cAAsB,cAAc;AACjD,QAAA,cAAc,UAAY,EAAA;AAG/B,SAAAC,KAAAC,UAAA,EAAA,UAAA,CACE,UACDC,IAAC,sBACA,EAAA,gBACA,aAAwB,GACpB,OAAK,GAEVA,IAAC,QAAM,EAAC,KAAK,YAAY,UAAS,aAAe,CAAA,CAAA,EAAA,CAAA;AAGpD;"}
|
|
@@ -7,16 +7,16 @@ const router = require("next/router");
|
|
|
7
7
|
const navigation = require("next/navigation");
|
|
8
8
|
const getPrismicPreviewCookie = require("./lib/getPrismicPreviewCookie.cjs");
|
|
9
9
|
const getPreviewCookieRepositoryName = require("./lib/getPreviewCookieRepositoryName.cjs");
|
|
10
|
-
function PrismicPreviewClient({ repositoryName, updatePreviewURL = "/api/preview", exitPreviewURL = "/api/exit-preview" }) {
|
|
10
|
+
function PrismicPreviewClient({ repositoryName, updatePreviewURL = "/api/preview", exitPreviewURL = "/api/exit-preview", isDraftMode }) {
|
|
11
|
+
let isPreviewActive = isDraftMode;
|
|
11
12
|
let isAppRouter = true;
|
|
12
|
-
let isPreviewMode = false;
|
|
13
13
|
let basePath = "";
|
|
14
14
|
let refresh;
|
|
15
15
|
try {
|
|
16
16
|
const router$1 = router.useRouter();
|
|
17
17
|
isAppRouter = false;
|
|
18
18
|
basePath = router$1.basePath;
|
|
19
|
-
|
|
19
|
+
isPreviewActive || (isPreviewActive = router$1.isPreview);
|
|
20
20
|
refresh = () => router$1.replace(router$1.asPath, void 0, { scroll: false });
|
|
21
21
|
} catch {
|
|
22
22
|
const router2 = navigation.useRouter();
|
|
@@ -42,21 +42,17 @@ function PrismicPreviewClient({ repositoryName, updatePreviewURL = "/api/preview
|
|
|
42
42
|
};
|
|
43
43
|
const handlePrismicPreviewEnd = async (event) => {
|
|
44
44
|
event.preventDefault();
|
|
45
|
-
|
|
45
|
+
const resolvedExitPreviewURL = basePath + exitPreviewURL;
|
|
46
|
+
const res = await globalThis.fetch(resolvedExitPreviewURL);
|
|
47
|
+
if (res.ok) {
|
|
46
48
|
refresh();
|
|
47
49
|
} else {
|
|
48
|
-
|
|
49
|
-
const res = await globalThis.fetch(resolvedExitPreviewURL);
|
|
50
|
-
if (res.ok) {
|
|
51
|
-
refresh();
|
|
52
|
-
} else {
|
|
53
|
-
console.error(`[<PrismicPreview>] Failed to exit Preview Mode using the "${resolvedExitPreviewURL}" API endpoint. Does it exist?`);
|
|
54
|
-
}
|
|
50
|
+
console.error(`[<PrismicPreview>] Failed to exit Preview Mode using the "${resolvedExitPreviewURL}" API endpoint. Does it exist?`);
|
|
55
51
|
}
|
|
56
52
|
};
|
|
57
53
|
window.addEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
|
|
58
54
|
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
59
|
-
if (!
|
|
55
|
+
if (!isPreviewActive) {
|
|
60
56
|
const prismicPreviewCookie = getPrismicPreviewCookie.getPrismicPreviewCookie(globalThis.document.cookie);
|
|
61
57
|
if (prismicPreviewCookie) {
|
|
62
58
|
const locationIsDescendantOfBasePath = window.location.href.startsWith(window.location.origin + basePath);
|
|
@@ -74,7 +70,7 @@ function PrismicPreviewClient({ repositoryName, updatePreviewURL = "/api/preview
|
|
|
74
70
|
basePath,
|
|
75
71
|
exitPreviewURL,
|
|
76
72
|
isAppRouter,
|
|
77
|
-
|
|
73
|
+
isPreviewActive,
|
|
78
74
|
refresh,
|
|
79
75
|
repositoryName,
|
|
80
76
|
updatePreviewURL
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreviewClient.cjs","sources":["../../src/PrismicPreviewClient.tsx"],"sourcesContent":["\"use client\";\n\nimport { useEffect } from \"react\";\nimport { useRouter as usePagesRouter } from \"next/router\";\nimport { useRouter } from \"next/navigation\";\n\nimport { getPrismicPreviewCookie } from \"./lib/getPrismicPreviewCookie\";\nimport { getPreviewCookieRepositoryName } from \"./lib/getPreviewCookieRepositoryName\";\n\nimport { PrismicPreviewProps } from \"./PrismicPreview\";\n\ntype PrismicPreviewClientProps = Omit<PrismicPreviewProps, \"children\"
|
|
1
|
+
{"version":3,"file":"PrismicPreviewClient.cjs","sources":["../../src/PrismicPreviewClient.tsx"],"sourcesContent":["\"use client\";\n\nimport { useEffect } from \"react\";\nimport { useRouter as usePagesRouter } from \"next/router\";\nimport { useRouter } from \"next/navigation\";\n\nimport { getPrismicPreviewCookie } from \"./lib/getPrismicPreviewCookie\";\nimport { getPreviewCookieRepositoryName } from \"./lib/getPreviewCookieRepositoryName\";\n\nimport { PrismicPreviewProps } from \"./PrismicPreview\";\n\ntype PrismicPreviewClientProps = Omit<PrismicPreviewProps, \"children\"> & {\n\tisDraftMode: boolean;\n};\n\nexport function PrismicPreviewClient({\n\trepositoryName,\n\tupdatePreviewURL = \"/api/preview\",\n\texitPreviewURL = \"/api/exit-preview\",\n\tisDraftMode,\n}: PrismicPreviewClientProps): null {\n\tlet isPreviewActive = isDraftMode;\n\tlet isAppRouter = true;\n\tlet basePath = \"\";\n\tlet refresh: () => void;\n\n\ttry {\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tconst router = usePagesRouter();\n\n\t\tisAppRouter = false;\n\t\tbasePath = router.basePath;\n\t\tisPreviewActive ||= router.isPreview;\n\t\trefresh = () => router.replace(router.asPath, undefined, { scroll: false });\n\t} catch {\n\t\t// Assume we are in App Router. Ignore the error.\n\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tconst router = useRouter();\n\n\t\trefresh = router.refresh;\n\t}\n\n\tuseEffect(() => {\n\t\t/**\n\t\t * Starts Preview Mode and refreshes the page's props.\n\t\t */\n\t\tconst startPreviewMode = async () => {\n\t\t\tconst resolvedUpdatePreviewURL = basePath + updatePreviewURL;\n\n\t\t\t// Start Next.js Preview Mode via the given preview API endpoint.\n\t\t\tconst res = await globalThis.fetch(resolvedUpdatePreviewURL);\n\n\t\t\t// We check for `res.redirected` rather than `res.ok`\n\t\t\t// since the update preview endpoint may redirect to a\n\t\t\t// 404 page. As long as it redirects, we know the\n\t\t\t// endpoint exists and at least attempted to set\n\t\t\t// preview data.\n\t\t\tif (res.redirected) {\n\t\t\t\trefresh();\n\t\t\t} else {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[<PrismicPreview>] Failed to start or update Preview Mode using the \"${resolvedUpdatePreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t);\n\t\t\t}\n\t\t};\n\n\t\tconst handlePrismicPreviewUpdate = async (event: Event) => {\n\t\t\t// Prevent the toolbar from reloading the page.\n\t\t\tevent.preventDefault();\n\n\t\t\tif (isAppRouter) {\n\t\t\t\trefresh();\n\t\t\t} else {\n\t\t\t\tawait startPreviewMode();\n\t\t\t}\n\t\t};\n\n\t\tconst handlePrismicPreviewEnd = async (event: Event) => {\n\t\t\t// Prevent the toolbar from reloading the page.\n\t\t\tevent.preventDefault();\n\n\t\t\tconst resolvedExitPreviewURL = basePath + exitPreviewURL;\n\n\t\t\t// Exit Next.js Preview Mode via the given preview API endpoint.\n\t\t\tconst res = await globalThis.fetch(resolvedExitPreviewURL);\n\n\t\t\tif (res.ok) {\n\t\t\t\trefresh();\n\t\t\t} else {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[<PrismicPreview>] Failed to exit Preview Mode using the \"${resolvedExitPreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t);\n\t\t\t}\n\t\t};\n\n\t\twindow.addEventListener(\"prismicPreviewUpdate\", handlePrismicPreviewUpdate);\n\t\twindow.addEventListener(\"prismicPreviewEnd\", handlePrismicPreviewEnd);\n\n\t\tif (!isPreviewActive) {\n\t\t\tconst prismicPreviewCookie = getPrismicPreviewCookie(\n\t\t\t\tglobalThis.document.cookie,\n\t\t\t);\n\n\t\t\tif (prismicPreviewCookie) {\n\t\t\t\t// If a Prismic preview cookie is present, but Next.js Preview\n\t\t\t\t// Mode is not active, we must activate Preview Mode manually.\n\t\t\t\t//\n\t\t\t\t// This will happen when a visitor accesses the page using a\n\t\t\t\t// Prismic preview share link.\n\n\t\t\t\t/**\n\t\t\t\t * Determines if the current location is a descendant of the app's base\n\t\t\t\t * path.\n\t\t\t\t *\n\t\t\t\t * This is used to prevent infinite refrehes; when\n\t\t\t\t * `isDescendantOfBasePath` is `false`, `router.isPreview` is also\n\t\t\t\t * `false`.\n\t\t\t\t *\n\t\t\t\t * If the app does not have a base path, this should always be `true`.\n\t\t\t\t */\n\t\t\t\tconst locationIsDescendantOfBasePath = window.location.href.startsWith(\n\t\t\t\t\twindow.location.origin + basePath,\n\t\t\t\t);\n\n\t\t\t\tconst prismicPreviewCookieRepositoryName =\n\t\t\t\t\tgetPreviewCookieRepositoryName(prismicPreviewCookie);\n\n\t\t\t\tif (\n\t\t\t\t\tlocationIsDescendantOfBasePath &&\n\t\t\t\t\tprismicPreviewCookieRepositoryName === repositoryName\n\t\t\t\t) {\n\t\t\t\t\tstartPreviewMode();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn () => {\n\t\t\twindow.removeEventListener(\n\t\t\t\t\"prismicPreviewUpdate\",\n\t\t\t\thandlePrismicPreviewUpdate,\n\t\t\t);\n\t\t\twindow.removeEventListener(\"prismicPreviewEnd\", handlePrismicPreviewEnd);\n\t\t};\n\t}, [\n\t\tbasePath,\n\t\texitPreviewURL,\n\t\tisAppRouter,\n\t\tisPreviewActive,\n\t\trefresh,\n\t\trepositoryName,\n\t\tupdatePreviewURL,\n\t]);\n\n\treturn null;\n}\n"],"names":["useEffect"],"mappings":";;;;;;;;;AAegB;AAMf;AACA;AACA;AACI;AAEA;AAEH;AAEc;AACd;AACA;AACU;AAAgE;AAK1E;AAEA;AAAiB;AAGlBA;AAIC;AACC;AAGA;AAOA;;;AAGS;AACyH;AAEjI;AAGI;AAEL;AAEA;;;AAGC;;AACA;AAGI;AAEL;AAEA;AAGA;AAEA;;;AAGS;AAC4G;AAEpH;AAGK;AACA;AAEP;AACC;AAIA;AAiBO;AAIA;AAIL;;;AAIA;AACD;AAGF;AACQ;AAIA;AAAgE;AAAA;AAEtE;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AAGM;AACR;;"}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { PrismicPreviewProps } from "./PrismicPreview";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
type PrismicPreviewClientProps = Omit<PrismicPreviewProps, "children"> & {
|
|
3
|
+
isDraftMode: boolean;
|
|
4
|
+
};
|
|
5
|
+
export declare function PrismicPreviewClient({ repositoryName, updatePreviewURL, exitPreviewURL, isDraftMode, }: PrismicPreviewClientProps): null;
|
|
4
6
|
export {};
|
|
@@ -5,16 +5,16 @@ import { useRouter } from "next/router";
|
|
|
5
5
|
import { useRouter as useRouter$1 } from "next/navigation";
|
|
6
6
|
import { getPrismicPreviewCookie } from "./lib/getPrismicPreviewCookie.js";
|
|
7
7
|
import { getPreviewCookieRepositoryName } from "./lib/getPreviewCookieRepositoryName.js";
|
|
8
|
-
function PrismicPreviewClient({ repositoryName, updatePreviewURL = "/api/preview", exitPreviewURL = "/api/exit-preview" }) {
|
|
8
|
+
function PrismicPreviewClient({ repositoryName, updatePreviewURL = "/api/preview", exitPreviewURL = "/api/exit-preview", isDraftMode }) {
|
|
9
|
+
let isPreviewActive = isDraftMode;
|
|
9
10
|
let isAppRouter = true;
|
|
10
|
-
let isPreviewMode = false;
|
|
11
11
|
let basePath = "";
|
|
12
12
|
let refresh;
|
|
13
13
|
try {
|
|
14
14
|
const router = useRouter();
|
|
15
15
|
isAppRouter = false;
|
|
16
16
|
basePath = router.basePath;
|
|
17
|
-
|
|
17
|
+
isPreviewActive || (isPreviewActive = router.isPreview);
|
|
18
18
|
refresh = () => router.replace(router.asPath, void 0, { scroll: false });
|
|
19
19
|
} catch {
|
|
20
20
|
const router = useRouter$1();
|
|
@@ -40,21 +40,17 @@ function PrismicPreviewClient({ repositoryName, updatePreviewURL = "/api/preview
|
|
|
40
40
|
};
|
|
41
41
|
const handlePrismicPreviewEnd = async (event) => {
|
|
42
42
|
event.preventDefault();
|
|
43
|
-
|
|
43
|
+
const resolvedExitPreviewURL = basePath + exitPreviewURL;
|
|
44
|
+
const res = await globalThis.fetch(resolvedExitPreviewURL);
|
|
45
|
+
if (res.ok) {
|
|
44
46
|
refresh();
|
|
45
47
|
} else {
|
|
46
|
-
|
|
47
|
-
const res = await globalThis.fetch(resolvedExitPreviewURL);
|
|
48
|
-
if (res.ok) {
|
|
49
|
-
refresh();
|
|
50
|
-
} else {
|
|
51
|
-
console.error(`[<PrismicPreview>] Failed to exit Preview Mode using the "${resolvedExitPreviewURL}" API endpoint. Does it exist?`);
|
|
52
|
-
}
|
|
48
|
+
console.error(`[<PrismicPreview>] Failed to exit Preview Mode using the "${resolvedExitPreviewURL}" API endpoint. Does it exist?`);
|
|
53
49
|
}
|
|
54
50
|
};
|
|
55
51
|
window.addEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
|
|
56
52
|
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
57
|
-
if (!
|
|
53
|
+
if (!isPreviewActive) {
|
|
58
54
|
const prismicPreviewCookie = getPrismicPreviewCookie(globalThis.document.cookie);
|
|
59
55
|
if (prismicPreviewCookie) {
|
|
60
56
|
const locationIsDescendantOfBasePath = window.location.href.startsWith(window.location.origin + basePath);
|
|
@@ -72,7 +68,7 @@ function PrismicPreviewClient({ repositoryName, updatePreviewURL = "/api/preview
|
|
|
72
68
|
basePath,
|
|
73
69
|
exitPreviewURL,
|
|
74
70
|
isAppRouter,
|
|
75
|
-
|
|
71
|
+
isPreviewActive,
|
|
76
72
|
refresh,
|
|
77
73
|
repositoryName,
|
|
78
74
|
updatePreviewURL
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreviewClient.js","sources":["../../src/PrismicPreviewClient.tsx"],"sourcesContent":["\"use client\";\n\nimport { useEffect } from \"react\";\nimport { useRouter as usePagesRouter } from \"next/router\";\nimport { useRouter } from \"next/navigation\";\n\nimport { getPrismicPreviewCookie } from \"./lib/getPrismicPreviewCookie\";\nimport { getPreviewCookieRepositoryName } from \"./lib/getPreviewCookieRepositoryName\";\n\nimport { PrismicPreviewProps } from \"./PrismicPreview\";\n\ntype PrismicPreviewClientProps = Omit<PrismicPreviewProps, \"children\"
|
|
1
|
+
{"version":3,"file":"PrismicPreviewClient.js","sources":["../../src/PrismicPreviewClient.tsx"],"sourcesContent":["\"use client\";\n\nimport { useEffect } from \"react\";\nimport { useRouter as usePagesRouter } from \"next/router\";\nimport { useRouter } from \"next/navigation\";\n\nimport { getPrismicPreviewCookie } from \"./lib/getPrismicPreviewCookie\";\nimport { getPreviewCookieRepositoryName } from \"./lib/getPreviewCookieRepositoryName\";\n\nimport { PrismicPreviewProps } from \"./PrismicPreview\";\n\ntype PrismicPreviewClientProps = Omit<PrismicPreviewProps, \"children\"> & {\n\tisDraftMode: boolean;\n};\n\nexport function PrismicPreviewClient({\n\trepositoryName,\n\tupdatePreviewURL = \"/api/preview\",\n\texitPreviewURL = \"/api/exit-preview\",\n\tisDraftMode,\n}: PrismicPreviewClientProps): null {\n\tlet isPreviewActive = isDraftMode;\n\tlet isAppRouter = true;\n\tlet basePath = \"\";\n\tlet refresh: () => void;\n\n\ttry {\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tconst router = usePagesRouter();\n\n\t\tisAppRouter = false;\n\t\tbasePath = router.basePath;\n\t\tisPreviewActive ||= router.isPreview;\n\t\trefresh = () => router.replace(router.asPath, undefined, { scroll: false });\n\t} catch {\n\t\t// Assume we are in App Router. Ignore the error.\n\n\t\t// eslint-disable-next-line react-hooks/rules-of-hooks\n\t\tconst router = useRouter();\n\n\t\trefresh = router.refresh;\n\t}\n\n\tuseEffect(() => {\n\t\t/**\n\t\t * Starts Preview Mode and refreshes the page's props.\n\t\t */\n\t\tconst startPreviewMode = async () => {\n\t\t\tconst resolvedUpdatePreviewURL = basePath + updatePreviewURL;\n\n\t\t\t// Start Next.js Preview Mode via the given preview API endpoint.\n\t\t\tconst res = await globalThis.fetch(resolvedUpdatePreviewURL);\n\n\t\t\t// We check for `res.redirected` rather than `res.ok`\n\t\t\t// since the update preview endpoint may redirect to a\n\t\t\t// 404 page. As long as it redirects, we know the\n\t\t\t// endpoint exists and at least attempted to set\n\t\t\t// preview data.\n\t\t\tif (res.redirected) {\n\t\t\t\trefresh();\n\t\t\t} else {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[<PrismicPreview>] Failed to start or update Preview Mode using the \"${resolvedUpdatePreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t);\n\t\t\t}\n\t\t};\n\n\t\tconst handlePrismicPreviewUpdate = async (event: Event) => {\n\t\t\t// Prevent the toolbar from reloading the page.\n\t\t\tevent.preventDefault();\n\n\t\t\tif (isAppRouter) {\n\t\t\t\trefresh();\n\t\t\t} else {\n\t\t\t\tawait startPreviewMode();\n\t\t\t}\n\t\t};\n\n\t\tconst handlePrismicPreviewEnd = async (event: Event) => {\n\t\t\t// Prevent the toolbar from reloading the page.\n\t\t\tevent.preventDefault();\n\n\t\t\tconst resolvedExitPreviewURL = basePath + exitPreviewURL;\n\n\t\t\t// Exit Next.js Preview Mode via the given preview API endpoint.\n\t\t\tconst res = await globalThis.fetch(resolvedExitPreviewURL);\n\n\t\t\tif (res.ok) {\n\t\t\t\trefresh();\n\t\t\t} else {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[<PrismicPreview>] Failed to exit Preview Mode using the \"${resolvedExitPreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t);\n\t\t\t}\n\t\t};\n\n\t\twindow.addEventListener(\"prismicPreviewUpdate\", handlePrismicPreviewUpdate);\n\t\twindow.addEventListener(\"prismicPreviewEnd\", handlePrismicPreviewEnd);\n\n\t\tif (!isPreviewActive) {\n\t\t\tconst prismicPreviewCookie = getPrismicPreviewCookie(\n\t\t\t\tglobalThis.document.cookie,\n\t\t\t);\n\n\t\t\tif (prismicPreviewCookie) {\n\t\t\t\t// If a Prismic preview cookie is present, but Next.js Preview\n\t\t\t\t// Mode is not active, we must activate Preview Mode manually.\n\t\t\t\t//\n\t\t\t\t// This will happen when a visitor accesses the page using a\n\t\t\t\t// Prismic preview share link.\n\n\t\t\t\t/**\n\t\t\t\t * Determines if the current location is a descendant of the app's base\n\t\t\t\t * path.\n\t\t\t\t *\n\t\t\t\t * This is used to prevent infinite refrehes; when\n\t\t\t\t * `isDescendantOfBasePath` is `false`, `router.isPreview` is also\n\t\t\t\t * `false`.\n\t\t\t\t *\n\t\t\t\t * If the app does not have a base path, this should always be `true`.\n\t\t\t\t */\n\t\t\t\tconst locationIsDescendantOfBasePath = window.location.href.startsWith(\n\t\t\t\t\twindow.location.origin + basePath,\n\t\t\t\t);\n\n\t\t\t\tconst prismicPreviewCookieRepositoryName =\n\t\t\t\t\tgetPreviewCookieRepositoryName(prismicPreviewCookie);\n\n\t\t\t\tif (\n\t\t\t\t\tlocationIsDescendantOfBasePath &&\n\t\t\t\t\tprismicPreviewCookieRepositoryName === repositoryName\n\t\t\t\t) {\n\t\t\t\t\tstartPreviewMode();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn () => {\n\t\t\twindow.removeEventListener(\n\t\t\t\t\"prismicPreviewUpdate\",\n\t\t\t\thandlePrismicPreviewUpdate,\n\t\t\t);\n\t\t\twindow.removeEventListener(\"prismicPreviewEnd\", handlePrismicPreviewEnd);\n\t\t};\n\t}, [\n\t\tbasePath,\n\t\texitPreviewURL,\n\t\tisAppRouter,\n\t\tisPreviewActive,\n\t\trefresh,\n\t\trepositoryName,\n\t\tupdatePreviewURL,\n\t]);\n\n\treturn null;\n}\n"],"names":[],"mappings":";;;;;;;AAegB;AAMf;AACA;AACA;AACI;AAEA;AAEH;AAEc;AACd;AACA;AACU;AAAgE;AAK1E;AAEA;AAAiB;AAGlB;AAIC;AACC;AAGA;AAOA;;;AAGS;AACyH;AAEjI;AAGI;AAEL;AAEA;;;AAGC;;AACA;AAGI;AAEL;AAEA;AAGA;AAEA;;;AAGS;AAC4G;AAEpH;AAGK;AACA;AAEP;AACC;AAIA;AAiBO;AAIA;AAIL;;;AAIA;AACD;AAGF;AACQ;AAIA;AAAgE;AAAA;AAEtE;AACF;AACA;AACA;AACA;AACA;AACA;AACA;AAGM;AACR;;;;"}
|
|
@@ -15,6 +15,15 @@ const enableAutoPreviews = (config) => {
|
|
|
15
15
|
} else {
|
|
16
16
|
config.client.queryContentFromRef(() => {
|
|
17
17
|
var _a;
|
|
18
|
+
let isDraftModeEnabled = false;
|
|
19
|
+
try {
|
|
20
|
+
isDraftModeEnabled = headers.draftMode().isEnabled;
|
|
21
|
+
} catch {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (!isDraftModeEnabled) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
18
27
|
let cookie$1;
|
|
19
28
|
try {
|
|
20
29
|
cookie$1 = (_a = headers.cookies().get(cookie.preview)) == null ? void 0 : _a.value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enableAutoPreviews.cjs","sources":["../../src/enableAutoPreviews.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"enableAutoPreviews.cjs","sources":["../../src/enableAutoPreviews.ts"],"sourcesContent":["import { draftMode, cookies } from \"next/headers\";\nimport { PreviewData } from \"next\";\nimport * as prismic from \"@prismicio/client\";\n\nimport { NextApiRequestLike, PrismicPreviewData } from \"./types\";\n\n/**\n * Configuration for `enableAutoPreviews`.\n *\n * @typeParam TPreviewData - Next.js preview data object.\n */\nexport type EnableAutoPreviewsConfig<\n\tTPreviewData extends PreviewData = PreviewData,\n> = {\n\t/**\n\t * Prismic client with which automatic previews will be enabled.\n\t */\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<\n\t\tprismic.Client,\n\t\t\"queryContentFromRef\" | \"enableAutoPreviewsFromReq\"\n\t>;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\n\t *\n\t * The `previewData` object provided in the `getStaticProps()` or\n\t * `getServerSideProps()` context object.\n\t */\n\tpreviewData?: TPreviewData;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\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\nconst isPrismicPreviewData = (input: unknown): input is PrismicPreviewData => {\n\treturn typeof input === \"object\" && input !== null && \"ref\" in input;\n};\n\n/**\n * Configures a Prismic client to automatically query draft content during a\n * preview session. It either takes in a Next.js `getStaticProps` context object\n * or a Next.js API endpoint request object.\n *\n * @param config - Configuration for the function.\n */\nexport const enableAutoPreviews = <TPreviewData extends PreviewData>(\n\tconfig: EnableAutoPreviewsConfig<TPreviewData>,\n): void => {\n\tif (\"previewData\" in config && config.previewData) {\n\t\t// Assume we are in `getStaticProps()` or\n\t\t// `getServerSideProps()` with active Preview Mode (`pages`\n\t\t// directory).\n\n\t\tif (isPrismicPreviewData(config.previewData)) {\n\t\t\tconfig.client.queryContentFromRef(config.previewData.ref);\n\t\t}\n\t} else if (\"req\" in config && config.req) {\n\t\t// Assume we are in an API Route (`pages` directory).\n\n\t\tconfig.client.enableAutoPreviewsFromReq(config.req);\n\t} else {\n\t\t// Assume we are in App Router (`app` directory) OR\n\t\t// `getStaticProps()`/`getServerSideProps()` with an inactive\n\t\t// Preview Mode (`pages` directory).\n\n\t\t// We use a function value so the cookie is checked on every\n\t\t// request. We don't have a static value to read from.\n\t\tconfig.client.queryContentFromRef(() => {\n\t\t\tlet isDraftModeEnabled = false;\n\t\t\ttry {\n\t\t\t\tisDraftModeEnabled = draftMode().isEnabled;\n\t\t\t} catch {\n\t\t\t\t// This catch block may be reached if\n\t\t\t\t// `draftMode()` is called in a place that does\n\t\t\t\t// not have access to its async storage. We can\n\t\t\t\t// ignore this case.\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (!isDraftModeEnabled) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet cookie: string | undefined;\n\t\t\ttry {\n\t\t\t\tcookie = cookies().get(prismic.cookie.preview)?.value;\n\t\t\t} catch {\n\t\t\t\t// We are probably in `getStaticProps()` or\n\t\t\t\t// `getServerSideProps()` with inactive Preview\n\t\t\t\t// Mode where `cookies()` does not work. We\n\t\t\t\t// don't need to do any preview handling.\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// We only return the cookie if a Prismic Preview session is active.\n\t\t\t//\n\t\t\t// An inactive cookie looks like this (URL encoded):\n\t\t\t// \t{\n\t\t\t// \t\t\"_tracker\": \"abc123\"\n\t\t\t// \t}\n\t\t\t//\n\t\t\t// An active cookie looks like this (URL encoded):\n\t\t\t// \t{\n\t\t\t// \t\t\"_tracker\": \"abc123\",\n\t\t\t// \t\t\"example-prismic-repo.prismic.io\": {\n\t\t\t// \t\t\tpreview: \"https://example-prismic-repo.prismic.io/previews/abc:123?websitePreviewId=xyz\"\n\t\t\t// \t\t}\n\t\t\t// \t}\n\t\t\tif (cookie && /\\.prismic\\.io/.test(cookie)) {\n\t\t\t\treturn cookie;\n\t\t\t}\n\t\t});\n\t}\n};\n"],"names":["draftMode","cookie","cookies","prismic.cookie.preview"],"mappings":";;;;AA2CA,MAAM,uBAAuB,CAAC,UAA+C;AAC5E,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,SAAS;AAChE;AASa,MAAA,qBAAqB,CACjC,WACS;AACL,MAAA,iBAAiB,UAAU,OAAO,aAAa;AAK9C,QAAA,qBAAqB,OAAO,WAAW,GAAG;AAC7C,aAAO,OAAO,oBAAoB,OAAO,YAAY,GAAG;AAAA,IACxD;AAAA,EACS,WAAA,SAAS,UAAU,OAAO,KAAK;AAGlC,WAAA,OAAO,0BAA0B,OAAO,GAAG;AAAA,EAAA,OAC5C;AAOC,WAAA,OAAO,oBAAoB,MAAK;;AACtC,UAAI,qBAAqB;AACrB,UAAA;AACH,6BAAqBA,QAAAA,UAAY,EAAA;AAAA,MAAA,QAChC;AAMD;AAAA,MACA;AAED,UAAI,CAAC,oBAAoB;AACxB;AAAA,MACA;AAEG,UAAAC;AACA,UAAA;AACHA,oBAASC,aAAAA,QAAU,EAAA,IAAIC,OAAsB,OAAA,MAApCD,mBAAuC;AAAA,MAAA,QAC/C;AAMD;AAAA,MACA;AAgBD,UAAID,YAAU,gBAAgB,KAAKA,QAAM,GAAG;AACpC,eAAAA;AAAAA,MACP;AAAA,IAAA,CACD;AAAA,EACD;AACF;;"}
|
|
@@ -6,7 +6,7 @@ import { NextApiRequestLike } from "./types";
|
|
|
6
6
|
*
|
|
7
7
|
* @typeParam TPreviewData - Next.js preview data object.
|
|
8
8
|
*/
|
|
9
|
-
export
|
|
9
|
+
export type EnableAutoPreviewsConfig<TPreviewData extends PreviewData = PreviewData> = {
|
|
10
10
|
/**
|
|
11
11
|
* Prismic client with which automatic previews will be enabled.
|
|
12
12
|
*/
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { cookies } from "next/headers";
|
|
1
|
+
import { draftMode, cookies } from "next/headers";
|
|
2
2
|
import { preview } from './_node_modules/@prismicio/client/dist/cookie.js';
|
|
3
3
|
const isPrismicPreviewData = (input) => {
|
|
4
4
|
return typeof input === "object" && input !== null && "ref" in input;
|
|
@@ -13,6 +13,15 @@ const enableAutoPreviews = (config) => {
|
|
|
13
13
|
} else {
|
|
14
14
|
config.client.queryContentFromRef(() => {
|
|
15
15
|
var _a;
|
|
16
|
+
let isDraftModeEnabled = false;
|
|
17
|
+
try {
|
|
18
|
+
isDraftModeEnabled = draftMode().isEnabled;
|
|
19
|
+
} catch {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
if (!isDraftModeEnabled) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
16
25
|
let cookie;
|
|
17
26
|
try {
|
|
18
27
|
cookie = (_a = cookies().get(preview)) == null ? void 0 : _a.value;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"enableAutoPreviews.js","sources":["../../src/enableAutoPreviews.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"enableAutoPreviews.js","sources":["../../src/enableAutoPreviews.ts"],"sourcesContent":["import { draftMode, cookies } from \"next/headers\";\nimport { PreviewData } from \"next\";\nimport * as prismic from \"@prismicio/client\";\n\nimport { NextApiRequestLike, PrismicPreviewData } from \"./types\";\n\n/**\n * Configuration for `enableAutoPreviews`.\n *\n * @typeParam TPreviewData - Next.js preview data object.\n */\nexport type EnableAutoPreviewsConfig<\n\tTPreviewData extends PreviewData = PreviewData,\n> = {\n\t/**\n\t * Prismic client with which automatic previews will be enabled.\n\t */\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<\n\t\tprismic.Client,\n\t\t\"queryContentFromRef\" | \"enableAutoPreviewsFromReq\"\n\t>;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\n\t *\n\t * The `previewData` object provided in the `getStaticProps()` or\n\t * `getServerSideProps()` context object.\n\t */\n\tpreviewData?: TPreviewData;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\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\nconst isPrismicPreviewData = (input: unknown): input is PrismicPreviewData => {\n\treturn typeof input === \"object\" && input !== null && \"ref\" in input;\n};\n\n/**\n * Configures a Prismic client to automatically query draft content during a\n * preview session. It either takes in a Next.js `getStaticProps` context object\n * or a Next.js API endpoint request object.\n *\n * @param config - Configuration for the function.\n */\nexport const enableAutoPreviews = <TPreviewData extends PreviewData>(\n\tconfig: EnableAutoPreviewsConfig<TPreviewData>,\n): void => {\n\tif (\"previewData\" in config && config.previewData) {\n\t\t// Assume we are in `getStaticProps()` or\n\t\t// `getServerSideProps()` with active Preview Mode (`pages`\n\t\t// directory).\n\n\t\tif (isPrismicPreviewData(config.previewData)) {\n\t\t\tconfig.client.queryContentFromRef(config.previewData.ref);\n\t\t}\n\t} else if (\"req\" in config && config.req) {\n\t\t// Assume we are in an API Route (`pages` directory).\n\n\t\tconfig.client.enableAutoPreviewsFromReq(config.req);\n\t} else {\n\t\t// Assume we are in App Router (`app` directory) OR\n\t\t// `getStaticProps()`/`getServerSideProps()` with an inactive\n\t\t// Preview Mode (`pages` directory).\n\n\t\t// We use a function value so the cookie is checked on every\n\t\t// request. We don't have a static value to read from.\n\t\tconfig.client.queryContentFromRef(() => {\n\t\t\tlet isDraftModeEnabled = false;\n\t\t\ttry {\n\t\t\t\tisDraftModeEnabled = draftMode().isEnabled;\n\t\t\t} catch {\n\t\t\t\t// This catch block may be reached if\n\t\t\t\t// `draftMode()` is called in a place that does\n\t\t\t\t// not have access to its async storage. We can\n\t\t\t\t// ignore this case.\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tif (!isDraftModeEnabled) {\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\tlet cookie: string | undefined;\n\t\t\ttry {\n\t\t\t\tcookie = cookies().get(prismic.cookie.preview)?.value;\n\t\t\t} catch {\n\t\t\t\t// We are probably in `getStaticProps()` or\n\t\t\t\t// `getServerSideProps()` with inactive Preview\n\t\t\t\t// Mode where `cookies()` does not work. We\n\t\t\t\t// don't need to do any preview handling.\n\n\t\t\t\treturn;\n\t\t\t}\n\n\t\t\t// We only return the cookie if a Prismic Preview session is active.\n\t\t\t//\n\t\t\t// An inactive cookie looks like this (URL encoded):\n\t\t\t// \t{\n\t\t\t// \t\t\"_tracker\": \"abc123\"\n\t\t\t// \t}\n\t\t\t//\n\t\t\t// An active cookie looks like this (URL encoded):\n\t\t\t// \t{\n\t\t\t// \t\t\"_tracker\": \"abc123\",\n\t\t\t// \t\t\"example-prismic-repo.prismic.io\": {\n\t\t\t// \t\t\tpreview: \"https://example-prismic-repo.prismic.io/previews/abc:123?websitePreviewId=xyz\"\n\t\t\t// \t\t}\n\t\t\t// \t}\n\t\t\tif (cookie && /\\.prismic\\.io/.test(cookie)) {\n\t\t\t\treturn cookie;\n\t\t\t}\n\t\t});\n\t}\n};\n"],"names":["prismic.cookie.preview"],"mappings":";;AA2CA,MAAM,uBAAuB,CAAC,UAA+C;AAC5E,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,SAAS;AAChE;AASa,MAAA,qBAAqB,CACjC,WACS;AACL,MAAA,iBAAiB,UAAU,OAAO,aAAa;AAK9C,QAAA,qBAAqB,OAAO,WAAW,GAAG;AAC7C,aAAO,OAAO,oBAAoB,OAAO,YAAY,GAAG;AAAA,IACxD;AAAA,EACS,WAAA,SAAS,UAAU,OAAO,KAAK;AAGlC,WAAA,OAAO,0BAA0B,OAAO,GAAG;AAAA,EAAA,OAC5C;AAOC,WAAA,OAAO,oBAAoB,MAAK;;AACtC,UAAI,qBAAqB;AACrB,UAAA;AACH,6BAAqB,UAAY,EAAA;AAAA,MAAA,QAChC;AAMD;AAAA,MACA;AAED,UAAI,CAAC,oBAAoB;AACxB;AAAA,MACA;AAEG,UAAA;AACA,UAAA;AACH,kBAAS,aAAU,EAAA,IAAIA,OAAsB,MAApC,mBAAuC;AAAA,MAAA,QAC/C;AAMD;AAAA,MACA;AAgBD,UAAI,UAAU,gBAAgB,KAAK,MAAM,GAAG;AACpC,eAAA;AAAA,MACP;AAAA,IAAA,CACD;AAAA,EACD;AACF;"}
|
package/dist/exitPreview.cjs
CHANGED
|
@@ -1,8 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const headers = require("next/headers");
|
|
3
4
|
async function exitPreview(config) {
|
|
4
|
-
config.res
|
|
5
|
-
|
|
5
|
+
if (config == null ? void 0 : config.res) {
|
|
6
|
+
config.res.clearPreviewData();
|
|
7
|
+
config.res.json({ success: true });
|
|
8
|
+
config.res.setHeader("Cache-Control", "no-store");
|
|
9
|
+
return;
|
|
10
|
+
} else {
|
|
11
|
+
headers.draftMode().disable();
|
|
12
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
13
|
+
headers: {
|
|
14
|
+
"Cache-Control": "no-store"
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
}
|
|
6
18
|
}
|
|
7
19
|
exports.exitPreview = exitPreview;
|
|
8
20
|
//# sourceMappingURL=exitPreview.cjs.map
|
package/dist/exitPreview.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.cjs","sources":["../../src/exitPreview.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"exitPreview.cjs","sources":["../../src/exitPreview.ts"],"sourcesContent":["import { draftMode } from \"next/headers\";\n\nimport { NextApiRequestLike, NextApiResponseLike } from \"./types\";\n\n/**\n * Configuration for `exitPreview`.\n */\nexport type ExitPreviewConfig = {\n\t/**\n\t * **Only use this parameter in the Pages Directory (/pages).**\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 * **Only use this parameter in the Pages Directory (/pages).**\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\n * used in a Router Handler or an API Route, depending on which you are using\n * the App Router or Pages Router.\n *\n * `exitPreview()` assumes Draft Mode is being used unless a Pages Router API\n * Route `res` object is provided to the function.\n *\n * @example Usage within an App Router Route Handler.\n *\n * ```typescript\n * // src/app/exit-preview/route.js\n *\n * import { exitPreview } from \"@prismicio/next\";\n *\n * export async function GET() {\n * \tawait exitPreview();\n * }\n * ```\n *\n * @example Usage within a Pages Router API Route.\n *\n * ```typescript\n * // src/pages/api/exit-preview.js\n *\n * import { exitPreview } from \"@prismicio/next\";\n *\n * export default async function handler(req, res) {\n * \tawait exitPreview({ req, res });\n * }\n * ```\n */\nexport async function exitPreview(\n\tconfig?: ExitPreviewConfig,\n): Promise<Response | void> {\n\tif (config?.res) {\n\t\t// Assume Preview Mode is being used.\n\n\t\tconfig.res.clearPreviewData();\n\n\t\t// 205 status is used to prevent CDN-level caching. The default 200\n\t\t// status code is typically treated as non-changing and cacheable.\n\t\tconfig.res.json({ success: true });\n\t\tconfig.res.setHeader(\"Cache-Control\", \"no-store\");\n\n\t\treturn;\n\t} else {\n\t\t// Assume Draft Mode is being used.\n\n\t\tdraftMode().disable();\n\n\t\t// 205 status is used to prevent CDN-level caching. The default 200\n\t\t// status code is typically treated as non-changing and cacheable.\n\t\treturn new Response(JSON.stringify({ success: true }), {\n\t\t\theaders: {\n\t\t\t\t\"Cache-Control\": \"no-store\",\n\t\t\t},\n\t\t});\n\t}\n}\n"],"names":["draftMode"],"mappings":";;;AA2DA,eAAsB,YACrB,QAA0B;AAE1B,MAAI,iCAAQ,KAAK;AAGhB,WAAO,IAAI;AAIX,WAAO,IAAI,KAAK,EAAE,SAAS,KAAM,CAAA;AAC1B,WAAA,IAAI,UAAU,iBAAiB,UAAU;AAEhD;AAAA,EAAA,OACM;AAGNA,YAAA,UAAA,EAAY;AAIL,WAAA,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,KAAA,CAAM,GAAG;AAAA,MACtD,SAAS;AAAA,QACR,iBAAiB;AAAA,MACjB;AAAA,IAAA,CACD;AAAA,EACD;AACF;;"}
|
package/dist/exitPreview.d.ts
CHANGED
|
@@ -1,25 +1,55 @@
|
|
|
1
|
-
import { NextResponse } from "next/server";
|
|
2
1
|
import { NextApiRequestLike, NextApiResponseLike } from "./types";
|
|
3
2
|
/**
|
|
4
3
|
* Configuration for `exitPreview`.
|
|
5
4
|
*/
|
|
6
|
-
export
|
|
5
|
+
export type ExitPreviewConfig = {
|
|
7
6
|
/**
|
|
7
|
+
* **Only use this parameter in the Pages Directory (/pages).**
|
|
8
|
+
*
|
|
8
9
|
* The `req` object from a Next.js API route.
|
|
9
10
|
*
|
|
10
11
|
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
11
12
|
*/
|
|
12
|
-
req
|
|
13
|
+
req?: NextApiRequestLike;
|
|
13
14
|
/**
|
|
15
|
+
* **Only use this parameter in the Pages Directory (/pages).**
|
|
16
|
+
*
|
|
14
17
|
* The `res` object from a Next.js API route.
|
|
15
18
|
*
|
|
16
19
|
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
17
20
|
*/
|
|
18
|
-
res
|
|
21
|
+
res?: NextApiResponseLike;
|
|
19
22
|
};
|
|
20
23
|
/**
|
|
21
|
-
*
|
|
24
|
+
* Ends a Prismic preview session within a Next.js app. This function should be
|
|
25
|
+
* used in a Router Handler or an API Route, depending on which you are using
|
|
26
|
+
* the App Router or Pages Router.
|
|
27
|
+
*
|
|
28
|
+
* `exitPreview()` assumes Draft Mode is being used unless a Pages Router API
|
|
29
|
+
* Route `res` object is provided to the function.
|
|
30
|
+
*
|
|
31
|
+
* @example Usage within an App Router Route Handler.
|
|
32
|
+
*
|
|
33
|
+
* ```typescript
|
|
34
|
+
* // src/app/exit-preview/route.js
|
|
35
|
+
*
|
|
36
|
+
* import { exitPreview } from "@prismicio/next";
|
|
37
|
+
*
|
|
38
|
+
* export async function GET() {
|
|
39
|
+
* await exitPreview();
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @example Usage within a Pages Router API Route.
|
|
44
|
+
*
|
|
45
|
+
* ```typescript
|
|
46
|
+
* // src/pages/api/exit-preview.js
|
|
47
|
+
*
|
|
48
|
+
* import { exitPreview } from "@prismicio/next";
|
|
22
49
|
*
|
|
23
|
-
*
|
|
50
|
+
* export default async function handler(req, res) {
|
|
51
|
+
* await exitPreview({ req, res });
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
24
54
|
*/
|
|
25
|
-
export declare function exitPreview(config
|
|
55
|
+
export declare function exitPreview(config?: ExitPreviewConfig): Promise<Response | void>;
|
package/dist/exitPreview.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
|
+
import { draftMode } from "next/headers";
|
|
1
2
|
async function exitPreview(config) {
|
|
2
|
-
config.res
|
|
3
|
-
|
|
3
|
+
if (config == null ? void 0 : config.res) {
|
|
4
|
+
config.res.clearPreviewData();
|
|
5
|
+
config.res.json({ success: true });
|
|
6
|
+
config.res.setHeader("Cache-Control", "no-store");
|
|
7
|
+
return;
|
|
8
|
+
} else {
|
|
9
|
+
draftMode().disable();
|
|
10
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
11
|
+
headers: {
|
|
12
|
+
"Cache-Control": "no-store"
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
}
|
|
4
16
|
}
|
|
5
17
|
export {
|
|
6
18
|
exitPreview
|
package/dist/exitPreview.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exitPreview.js","sources":["../../src/exitPreview.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"exitPreview.js","sources":["../../src/exitPreview.ts"],"sourcesContent":["import { draftMode } from \"next/headers\";\n\nimport { NextApiRequestLike, NextApiResponseLike } from \"./types\";\n\n/**\n * Configuration for `exitPreview`.\n */\nexport type ExitPreviewConfig = {\n\t/**\n\t * **Only use this parameter in the Pages Directory (/pages).**\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 * **Only use this parameter in the Pages Directory (/pages).**\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\n * used in a Router Handler or an API Route, depending on which you are using\n * the App Router or Pages Router.\n *\n * `exitPreview()` assumes Draft Mode is being used unless a Pages Router API\n * Route `res` object is provided to the function.\n *\n * @example Usage within an App Router Route Handler.\n *\n * ```typescript\n * // src/app/exit-preview/route.js\n *\n * import { exitPreview } from \"@prismicio/next\";\n *\n * export async function GET() {\n * \tawait exitPreview();\n * }\n * ```\n *\n * @example Usage within a Pages Router API Route.\n *\n * ```typescript\n * // src/pages/api/exit-preview.js\n *\n * import { exitPreview } from \"@prismicio/next\";\n *\n * export default async function handler(req, res) {\n * \tawait exitPreview({ req, res });\n * }\n * ```\n */\nexport async function exitPreview(\n\tconfig?: ExitPreviewConfig,\n): Promise<Response | void> {\n\tif (config?.res) {\n\t\t// Assume Preview Mode is being used.\n\n\t\tconfig.res.clearPreviewData();\n\n\t\t// 205 status is used to prevent CDN-level caching. The default 200\n\t\t// status code is typically treated as non-changing and cacheable.\n\t\tconfig.res.json({ success: true });\n\t\tconfig.res.setHeader(\"Cache-Control\", \"no-store\");\n\n\t\treturn;\n\t} else {\n\t\t// Assume Draft Mode is being used.\n\n\t\tdraftMode().disable();\n\n\t\t// 205 status is used to prevent CDN-level caching. The default 200\n\t\t// status code is typically treated as non-changing and cacheable.\n\t\treturn new Response(JSON.stringify({ success: true }), {\n\t\t\theaders: {\n\t\t\t\t\"Cache-Control\": \"no-store\",\n\t\t\t},\n\t\t});\n\t}\n}\n"],"names":[],"mappings":";AA2DA,eAAsB,YACrB,QAA0B;AAE1B,MAAI,iCAAQ,KAAK;AAGhB,WAAO,IAAI;AAIX,WAAO,IAAI,KAAK,EAAE,SAAS,KAAM,CAAA;AAC1B,WAAA,IAAI,UAAU,iBAAiB,UAAU;AAEhD;AAAA,EAAA,OACM;AAGN,cAAA,EAAY;AAIL,WAAA,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,KAAA,CAAM,GAAG;AAAA,MACtD,SAAS;AAAA,QACR,iBAAiB;AAAA,MACjB;AAAA,IAAA,CACD;AAAA,EACD;AACF;"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const getPreviewCookieRepositoryName = (previewCookie) => {
|
|
4
|
-
return (decodeURIComponent(previewCookie).match(
|
|
4
|
+
return (decodeURIComponent(previewCookie).match(/,"(.+).prismic.io"/) || [])[1];
|
|
5
5
|
};
|
|
6
6
|
exports.getPreviewCookieRepositoryName = getPreviewCookieRepositoryName;
|
|
7
7
|
//# sourceMappingURL=getPreviewCookieRepositoryName.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPreviewCookieRepositoryName.cjs","sources":["../../../src/lib/getPreviewCookieRepositoryName.ts"],"sourcesContent":["/**\n * Extracts preview reference repo name from stringified Prismic preview cookie\n *\n * @param previewCookie - The Prismic preview cookie.\n *\n * @returns The repository name for the Prismic preview cookie. If the cookie\n * represents an inactive preview session, `undefined` will be returned.\n */\nexport const getPreviewCookieRepositoryName = (\n\tpreviewCookie: string,\n): string | undefined => {\n\treturn (decodeURIComponent(previewCookie).match(
|
|
1
|
+
{"version":3,"file":"getPreviewCookieRepositoryName.cjs","sources":["../../../src/lib/getPreviewCookieRepositoryName.ts"],"sourcesContent":["/**\n * Extracts preview reference repo name from stringified Prismic preview cookie\n *\n * @param previewCookie - The Prismic preview cookie.\n *\n * @returns The repository name for the Prismic preview cookie. If the cookie\n * represents an inactive preview session, `undefined` will be returned.\n */\nexport const getPreviewCookieRepositoryName = (\n\tpreviewCookie: string,\n): string | undefined => {\n\treturn (decodeURIComponent(previewCookie).match(/,\"(.+).prismic.io\"/) ||\n\t\t[])[1];\n};\n"],"names":[],"mappings":";;AAQa,MAAA,iCAAiC,CAC7C,kBACuB;AACf,UAAA,mBAAmB,aAAa,EAAE,MAAM,oBAAoB,KACnE,CAAA,GAAI,CAAC;AACP;;"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const getPreviewCookieRepositoryName = (previewCookie) => {
|
|
2
|
-
return (decodeURIComponent(previewCookie).match(
|
|
2
|
+
return (decodeURIComponent(previewCookie).match(/,"(.+).prismic.io"/) || [])[1];
|
|
3
3
|
};
|
|
4
4
|
export {
|
|
5
5
|
getPreviewCookieRepositoryName
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getPreviewCookieRepositoryName.js","sources":["../../../src/lib/getPreviewCookieRepositoryName.ts"],"sourcesContent":["/**\n * Extracts preview reference repo name from stringified Prismic preview cookie\n *\n * @param previewCookie - The Prismic preview cookie.\n *\n * @returns The repository name for the Prismic preview cookie. If the cookie\n * represents an inactive preview session, `undefined` will be returned.\n */\nexport const getPreviewCookieRepositoryName = (\n\tpreviewCookie: string,\n): string | undefined => {\n\treturn (decodeURIComponent(previewCookie).match(
|
|
1
|
+
{"version":3,"file":"getPreviewCookieRepositoryName.js","sources":["../../../src/lib/getPreviewCookieRepositoryName.ts"],"sourcesContent":["/**\n * Extracts preview reference repo name from stringified Prismic preview cookie\n *\n * @param previewCookie - The Prismic preview cookie.\n *\n * @returns The repository name for the Prismic preview cookie. If the cookie\n * represents an inactive preview session, `undefined` will be returned.\n */\nexport const getPreviewCookieRepositoryName = (\n\tpreviewCookie: string,\n): string | undefined => {\n\treturn (decodeURIComponent(previewCookie).match(/,\"(.+).prismic.io\"/) ||\n\t\t[])[1];\n};\n"],"names":[],"mappings":"AAQa,MAAA,iCAAiC,CAC7C,kBACuB;AACf,UAAA,mBAAmB,aAAa,EAAE,MAAM,oBAAoB,KACnE,CAAA,GAAI,CAAC;AACP;"}
|
package/dist/package.json.cjs
CHANGED
package/dist/package.json.js
CHANGED