@prismicio/next 1.7.0 → 2.0.0-alpha.0
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/README.md +5 -3
- package/dist/PrismicNextImage.d.ts +7 -8
- package/dist/PrismicNextImage.js +8 -8
- package/dist/PrismicNextImage.js.map +1 -1
- package/dist/PrismicNextLink.d.ts +99 -100
- package/dist/PrismicNextLink.js +3 -5
- package/dist/PrismicNextLink.js.map +1 -1
- package/dist/PrismicPreview.d.ts +5 -13
- package/dist/PrismicPreview.js +6 -9
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/PrismicPreviewClient.d.ts +5 -3
- package/dist/PrismicPreviewClient.js +58 -69
- package/dist/PrismicPreviewClient.js.map +1 -1
- package/dist/createLocaleRedirect.d.ts +8 -15
- package/dist/createLocaleRedirect.js +4 -28
- package/dist/createLocaleRedirect.js.map +1 -1
- package/dist/enableAutoPreviews.d.ts +7 -31
- package/dist/enableAutoPreviews.js +24 -35
- package/dist/enableAutoPreviews.js.map +1 -1
- package/dist/exitPreview.d.ts +5 -47
- package/dist/exitPreview.js +8 -15
- package/dist/exitPreview.js.map +1 -1
- package/dist/imgixLoader.js.map +1 -1
- package/dist/index.d.ts +15 -18
- package/dist/index.js +2 -4
- package/dist/index.js.map +1 -1
- package/dist/lib/devMsg.d.ts +1 -1
- package/dist/lib/devMsg.js.map +1 -1
- package/dist/package.json.js +1 -1
- package/dist/pages/PrismicPreview.d.ts +31 -0
- package/dist/pages/PrismicPreview.js +64 -0
- package/dist/pages/PrismicPreview.js.map +1 -0
- package/dist/pages/enableAutoPreviews.d.ts +31 -0
- package/dist/pages/enableAutoPreviews.js +19 -0
- package/dist/pages/enableAutoPreviews.js.map +1 -0
- package/dist/pages/exitPreview.d.ts +33 -0
- package/dist/pages/exitPreview.js +10 -0
- package/dist/pages/exitPreview.js.map +1 -0
- package/dist/pages/index.d.ts +18 -0
- package/dist/pages/redirectToPreviewURL.d.ts +43 -0
- package/dist/pages/redirectToPreviewURL.js +16 -0
- package/dist/pages/redirectToPreviewURL.js.map +1 -0
- package/dist/{setPreviewData.d.ts → pages/setPreviewData.d.ts} +3 -9
- package/dist/{setPreviewData.js → pages/setPreviewData.js} +2 -2
- package/dist/pages/setPreviewData.js.map +1 -0
- package/dist/pages/types.d.ts +30 -0
- package/dist/pages.js +21 -0
- package/dist/pages.js.map +1 -0
- package/dist/redirectToPreviewURL.d.ts +11 -41
- package/dist/redirectToPreviewURL.js +16 -22
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/dist/types.d.ts +2 -51
- package/package.json +61 -54
- package/src/PrismicNextImage.tsx +19 -16
- package/src/PrismicNextLink.tsx +17 -13
- package/src/PrismicPreview.tsx +18 -30
- package/src/PrismicPreviewClient.tsx +111 -138
- package/src/createLocaleRedirect.ts +14 -56
- package/src/enableAutoPreviews.ts +38 -110
- package/src/exitPreview.ts +18 -79
- package/src/index.ts +15 -26
- package/src/lib/devMsg.ts +1 -1
- package/src/pages/PrismicPreview.tsx +145 -0
- package/src/pages/enableAutoPreviews.ts +62 -0
- package/src/pages/exitPreview.ts +45 -0
- package/src/pages/index.ts +27 -0
- package/src/pages/redirectToPreviewURL.ts +77 -0
- package/src/{setPreviewData.ts → pages/setPreviewData.ts} +5 -11
- package/src/pages/types.ts +36 -0
- package/src/redirectToPreviewURL.ts +44 -95
- package/src/types.ts +2 -56
- package/dist/PrismicNextImage.cjs +0 -64
- package/dist/PrismicNextImage.cjs.map +0 -1
- package/dist/PrismicNextLink.cjs +0 -40
- package/dist/PrismicNextLink.cjs.map +0 -1
- package/dist/PrismicPreview.cjs +0 -18
- package/dist/PrismicPreview.cjs.map +0 -1
- package/dist/PrismicPreviewClient.cjs +0 -81
- package/dist/PrismicPreviewClient.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/cookie.cjs +0 -5
- package/dist/_node_modules/@prismicio/client/dist/cookie.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/cookie.js +0 -5
- package/dist/_node_modules/@prismicio/client/dist/cookie.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.cjs +0 -16
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.js +0 -16
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.cjs +0 -13
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.js +0 -13
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs +0 -47
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js +0 -47
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.cjs +0 -28
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.js +0 -28
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.cjs +0 -27
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.js +0 -27
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.cjs +0 -14
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.js +0 -14
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.cjs +0 -7
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.js +0 -7
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.cjs +0 -9
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.js +0 -9
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.cjs +0 -10
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.js +0 -10
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.js.map +0 -1
- package/dist/createLocaleRedirect.cjs +0 -32
- package/dist/createLocaleRedirect.cjs.map +0 -1
- package/dist/enableAutoPreviews.cjs +0 -40
- package/dist/enableAutoPreviews.cjs.map +0 -1
- package/dist/exitPreview.cjs +0 -20
- package/dist/exitPreview.cjs.map +0 -1
- package/dist/imgixLoader.cjs +0 -17
- package/dist/imgixLoader.cjs.map +0 -1
- package/dist/index.cjs +0 -21
- package/dist/index.cjs.map +0 -1
- package/dist/lib/devMsg.cjs +0 -8
- package/dist/lib/devMsg.cjs.map +0 -1
- package/dist/lib/getPreviewCookieRepositoryName.cjs +0 -7
- package/dist/lib/getPreviewCookieRepositoryName.cjs.map +0 -1
- package/dist/lib/getPreviewCookieRepositoryName.d.ts +0 -9
- package/dist/lib/getPreviewCookieRepositoryName.js +0 -7
- package/dist/lib/getPreviewCookieRepositoryName.js.map +0 -1
- package/dist/lib/getPrismicPreviewCookie.cjs +0 -21
- package/dist/lib/getPrismicPreviewCookie.cjs.map +0 -1
- package/dist/lib/getPrismicPreviewCookie.d.ts +0 -9
- package/dist/lib/getPrismicPreviewCookie.js +0 -21
- package/dist/lib/getPrismicPreviewCookie.js.map +0 -1
- package/dist/package.json.cjs +0 -5
- package/dist/package.json.cjs.map +0 -1
- package/dist/redirectToPreviewURL.cjs +0 -30
- package/dist/redirectToPreviewURL.cjs.map +0 -1
- package/dist/setPreviewData.cjs +0 -11
- package/dist/setPreviewData.cjs.map +0 -1
- package/dist/setPreviewData.js.map +0 -1
- package/src/lib/getPreviewCookieRepositoryName.ts +0 -14
- package/src/lib/getPrismicPreviewCookie.ts +0 -33
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type ReactNode, JSX } from "react";
|
|
2
|
+
/** Props for `<PrismicPreview>`. */
|
|
3
|
+
export type PrismicPreviewProps = {
|
|
4
|
+
/**
|
|
5
|
+
* The name of your Prismic repository. A Prismic Toolbar will be registered
|
|
6
|
+
* using this repository.
|
|
7
|
+
*/
|
|
8
|
+
repositoryName: string;
|
|
9
|
+
/**
|
|
10
|
+
* The URL of your app's Prismic preview endpoint (default: `/api/preview`).
|
|
11
|
+
* This URL will be fetched on preview update events.
|
|
12
|
+
*/
|
|
13
|
+
updatePreviewURL?: string;
|
|
14
|
+
/**
|
|
15
|
+
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`).
|
|
16
|
+
* This URL will be fetched on preview exit events.
|
|
17
|
+
*/
|
|
18
|
+
exitPreviewURL?: string;
|
|
19
|
+
/** Children to render adjacent to the Prismic Toolbar. */
|
|
20
|
+
children?: ReactNode;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* React component that sets up Prismic Previews using the Prismic Toolbar. When
|
|
24
|
+
* the Prismic Toolbar send events to the browser, such as on preview updates
|
|
25
|
+
* and exiting, this component will automatically refresh the page with the
|
|
26
|
+
* changes.
|
|
27
|
+
*
|
|
28
|
+
* This component can be wrapped around your app or added anywhere in your app's
|
|
29
|
+
* tree. It must be rendered on every page.
|
|
30
|
+
*/
|
|
31
|
+
export declare function PrismicPreview(props: PrismicPreviewProps): JSX.Element;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { jsxs, Fragment, jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useEffect } from "react";
|
|
3
|
+
import { useRouter } from "next/router";
|
|
4
|
+
import Script from "next/script";
|
|
5
|
+
import { getToolbarSrc, cookie } from "@prismicio/client";
|
|
6
|
+
function PrismicPreview(props) {
|
|
7
|
+
const { repositoryName, updatePreviewURL = "/api/preview", exitPreviewURL = "/api/exit-preview", children } = props;
|
|
8
|
+
const router = useRouter();
|
|
9
|
+
const toolbarSrc = getToolbarSrc(repositoryName);
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
const controller = new AbortController();
|
|
12
|
+
window.addEventListener("prismicPreviewUpdate", onUpdate, {
|
|
13
|
+
signal: controller.signal
|
|
14
|
+
});
|
|
15
|
+
window.addEventListener("prismicPreviewEnd", onEnd, {
|
|
16
|
+
signal: controller.signal
|
|
17
|
+
});
|
|
18
|
+
if (window.location.href.startsWith(window.location.origin + router.basePath) && getPreviewCookieRepositoryName() === repositoryName && !router.isPreview) {
|
|
19
|
+
start();
|
|
20
|
+
}
|
|
21
|
+
function onEnd(event) {
|
|
22
|
+
event.preventDefault();
|
|
23
|
+
fetch(router.basePath + exitPreviewURL, { signal: controller.signal }).then((res) => {
|
|
24
|
+
if (!res.ok) {
|
|
25
|
+
console.error(`[<PrismicPreview>] Failed to exit Preview Mode using the "${exitPreviewURL}" API endpoint. Does it exist?`);
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
refresh();
|
|
29
|
+
}).catch(() => {
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function onUpdate(event) {
|
|
33
|
+
event.preventDefault();
|
|
34
|
+
start();
|
|
35
|
+
}
|
|
36
|
+
function start() {
|
|
37
|
+
fetch(router.basePath + updatePreviewURL, {
|
|
38
|
+
redirect: "manual",
|
|
39
|
+
signal: controller.signal
|
|
40
|
+
}).then((res) => {
|
|
41
|
+
if (res.type !== "opaqueredirect") {
|
|
42
|
+
console.error(`[<PrismicPreview>] Failed to start or update the preview using "${updatePreviewURL}". Does it exist?`);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
refresh();
|
|
46
|
+
}).catch(() => {
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function refresh() {
|
|
50
|
+
router.replace(router.asPath, void 0, { scroll: false });
|
|
51
|
+
}
|
|
52
|
+
return () => controller.abort();
|
|
53
|
+
}, [exitPreviewURL, updatePreviewURL, repositoryName, router]);
|
|
54
|
+
return jsxs(Fragment, { children: [children, jsx(Script, { src: toolbarSrc, strategy: "lazyOnload" })] });
|
|
55
|
+
}
|
|
56
|
+
function getPreviewCookieRepositoryName() {
|
|
57
|
+
var _a;
|
|
58
|
+
const cookie$1 = (_a = window.document.cookie.split("; ").find((row) => row.startsWith(`${cookie.preview}=`))) == null ? void 0 : _a.split("=")[1];
|
|
59
|
+
return (decodeURIComponent(cookie$1 ?? "").match(/"([^"]+)\.prismic\.io"/) || [])[1];
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
PrismicPreview
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=PrismicPreview.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PrismicPreview.js","sources":["../../src/pages/PrismicPreview.tsx"],"sourcesContent":["import { type ReactNode, useEffect, JSX } from \"react\";\nimport { useRouter } from \"next/router\";\nimport Script from \"next/script\";\nimport { getToolbarSrc, cookie as prismicCookie } from \"@prismicio/client\";\n\n/** Props for `<PrismicPreview>`. */\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 * 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 * 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/** Children to render adjacent to the Prismic Toolbar. */\n\tchildren?: 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(props: PrismicPreviewProps): JSX.Element {\n\tconst {\n\t\trepositoryName,\n\t\tupdatePreviewURL = \"/api/preview\",\n\t\texitPreviewURL = \"/api/exit-preview\",\n\t\tchildren,\n\t} = props;\n\n\tconst router = useRouter();\n\n\tconst toolbarSrc = getToolbarSrc(repositoryName);\n\n\tuseEffect(() => {\n\t\tconst controller = new AbortController();\n\n\t\twindow.addEventListener(\"prismicPreviewUpdate\", onUpdate, {\n\t\t\tsignal: controller.signal,\n\t\t});\n\t\twindow.addEventListener(\"prismicPreviewEnd\", onEnd, {\n\t\t\tsignal: controller.signal,\n\t\t});\n\n\t\t// Start the preview for preview share links. Previews from\n\t\t// share links do not go to the `updatePreviewURL` like a normal\n\t\t// preview.\n\t\t//\n\t\t// We check that the current URL is a descendant of the base\n\t\t// path to prevent infinite refrehes.\n\t\tif (\n\t\t\twindow.location.href.startsWith(\n\t\t\t\twindow.location.origin + router.basePath,\n\t\t\t) &&\n\t\t\tgetPreviewCookieRepositoryName() === repositoryName &&\n\t\t\t!router.isPreview\n\t\t) {\n\t\t\tstart();\n\t\t}\n\n\t\tfunction onEnd(event: Event) {\n\t\t\tevent.preventDefault();\n\t\t\tfetch(router.basePath + exitPreviewURL, { signal: controller.signal })\n\t\t\t\t.then((res) => {\n\t\t\t\t\tif (!res.ok) {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`[<PrismicPreview>] Failed to exit Preview Mode using the \"${exitPreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\trefresh();\n\t\t\t\t})\n\t\t\t\t.catch(() => {});\n\t\t}\n\n\t\tfunction onUpdate(event: Event) {\n\t\t\tevent.preventDefault();\n\t\t\tstart();\n\t\t}\n\n\t\tfunction start() {\n\t\t\t// We check `opaqueredirect` because we don't care if\n\t\t\t// the redirect was successful or not. As long as it\n\t\t\t// redirects, we know the endpoint exists and at least\n\t\t\t// attempted to set preview data.\n\t\t\tfetch(router.basePath + updatePreviewURL, {\n\t\t\t\tredirect: \"manual\",\n\t\t\t\tsignal: controller.signal,\n\t\t\t})\n\t\t\t\t.then((res) => {\n\t\t\t\t\tif (res.type !== \"opaqueredirect\") {\n\t\t\t\t\t\tconsole.error(\n\t\t\t\t\t\t\t`[<PrismicPreview>] Failed to start or update the preview using \"${updatePreviewURL}\". Does it exist?`,\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\trefresh();\n\t\t\t\t})\n\t\t\t\t.catch(() => {});\n\t\t}\n\n\t\tfunction refresh() {\n\t\t\trouter.replace(router.asPath, undefined, { scroll: false });\n\t\t}\n\n\t\treturn () => controller.abort();\n\t}, [exitPreviewURL, updatePreviewURL, repositoryName, router]);\n\n\treturn (\n\t\t<>\n\t\t\t{children}\n\t\t\t<Script src={toolbarSrc} strategy=\"lazyOnload\" />\n\t\t</>\n\t);\n}\n\nfunction getPreviewCookieRepositoryName() {\n\tconst cookie = window.document.cookie\n\t\t.split(\"; \")\n\t\t.find((row) => row.startsWith(`${prismicCookie.preview}=`))\n\t\t?.split(\"=\")[1];\n\n\treturn (decodeURIComponent(cookie ?? \"\").match(/\"([^\"]+)\\.prismic\\.io\"/) ||\n\t\t[])[1];\n}\n"],"names":["_jsxs","_Fragment","_jsx","cookie","prismicCookie"],"mappings":";;;;;AAsCM,SAAU,eAAe,OAA0B;AACxD,QAAM,EACL,gBACA,mBAAmB,gBACnB,iBAAiB,qBACjB,aACG;AAEJ,QAAM,SAAS,UAAS;AAElB,QAAA,aAAa,cAAc,cAAc;AAE/C,YAAU,MAAK;AACR,UAAA,aAAa,IAAI;AAEhB,WAAA,iBAAiB,wBAAwB,UAAU;AAAA,MACzD,QAAQ,WAAW;AAAA,IAAA,CACnB;AACM,WAAA,iBAAiB,qBAAqB,OAAO;AAAA,MACnD,QAAQ,WAAW;AAAA,IAAA,CACnB;AAQD,QACC,OAAO,SAAS,KAAK,WACpB,OAAO,SAAS,SAAS,OAAO,QAAQ,KAEzC,+BAA8B,MAAO,kBACrC,CAAC,OAAO,WACP;;;AAIF,aAAS,MAAM,OAAY;AAC1B,YAAM,eAAc;AACd,YAAA,OAAO,WAAW,gBAAgB,EAAE,QAAQ,WAAW,OAAQ,CAAA,EACnE,KAAK,CAAC,QAAO;AACT,YAAA,CAAC,IAAI,IAAI;AACJ,kBAAA,MACP,6DAA6D,cAAc,gCAAgC;AAG5G;AAAA,QAAA;;OAID,EACA,MAAM,MAAO;AAAA,MAAA,CAAC;AAAA,IAAA;AAGjB,aAAS,SAAS,OAAY;AAC7B,YAAM,eAAc;;;AAIrB,aAAS,QAAK;AAKP,YAAA,OAAO,WAAW,kBAAkB;AAAA,QACzC,UAAU;AAAA,QACV,QAAQ,WAAW;AAAA,MAAA,CACnB,EACC,KAAK,CAAC,QAAO;AACT,YAAA,IAAI,SAAS,kBAAkB;AAC1B,kBAAA,MACP,mEAAmE,gBAAgB,mBAAmB;AAGvG;AAAA,QAAA;;OAID,EACA,MAAM,MAAO;AAAA,MAAA,CAAC;AAAA,IAAA;AAGjB,aAAS,UAAO;AACf,aAAO,QAAQ,OAAO,QAAQ,QAAW,EAAE,QAAQ,OAAO;AAAA,IAAA;AAGpD,WAAA,MAAM,WAAW;KACtB,CAAC,gBAAgB,kBAAkB,gBAAgB,MAAM,CAAC;AAE7D,SACCA,KACEC,UAAA,EAAA,UAAA,CAAA,UACDC,IAAC,QAAO,EAAA,KAAK,YAAY,UAAS,aAAe,CAAA,CAAA,GAAA;AAGpD;AAEA,SAAS,iCAA8B;;AAChC,QAAAC,YAAS,YAAO,SAAS,OAC7B,MAAM,IAAI,EACV,KAAK,CAAC,QAAQ,IAAI,WAAW,GAAGC,OAAc,OAAO,GAAG,CAAC,MAF5C,mBAGZ,MAAM,KAAK;AAEN,UAAA,mBAAmBD,YAAU,EAAE,EAAE,MAAM,wBAAwB,KACtE,CAAA,GAAI,CAAC;AACP;"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { PreviewData } from "next";
|
|
2
|
+
import type { Client } from "@prismicio/client";
|
|
3
|
+
import type { NextApiRequestLike } from "./types.js";
|
|
4
|
+
/**
|
|
5
|
+
* Configuration for `enableAutoPreviews`.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam TPreviewData - Next.js preview data object.
|
|
8
|
+
*/
|
|
9
|
+
export type EnableAutoPreviewsConfig = {
|
|
10
|
+
/** Prismic client with which automatic previews will be enabled. */
|
|
11
|
+
client: Pick<Client, "queryContentFromRef" | "enableAutoPreviewsFromReq">;
|
|
12
|
+
/**
|
|
13
|
+
* The `previewData` object provided in the `getStaticProps()` or
|
|
14
|
+
* `getServerSideProps()` context object.
|
|
15
|
+
*/
|
|
16
|
+
previewData?: PreviewData;
|
|
17
|
+
/**
|
|
18
|
+
* The `req` object from a Next.js API route.
|
|
19
|
+
*
|
|
20
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
21
|
+
*/
|
|
22
|
+
req?: NextApiRequestLike;
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Configures a Prismic client to automatically query draft content during a
|
|
26
|
+
* preview session. It either takes in a Next.js `getStaticProps` context object
|
|
27
|
+
* or a Next.js API endpoint request object.
|
|
28
|
+
*
|
|
29
|
+
* @param config - Configuration for the function.
|
|
30
|
+
*/
|
|
31
|
+
export declare function enableAutoPreviews(config: EnableAutoPreviewsConfig): void;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
function enableAutoPreviews(config) {
|
|
2
|
+
if ("previewData" in config && config.previewData) {
|
|
3
|
+
if (isPrismicPreviewData(config.previewData)) {
|
|
4
|
+
config.client.queryContentFromRef(config.previewData.ref);
|
|
5
|
+
}
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
if ("req" in config && config.req) {
|
|
9
|
+
config.client.enableAutoPreviewsFromReq(config.req);
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function isPrismicPreviewData(input) {
|
|
14
|
+
return typeof input === "object" && input !== null && "ref" in input;
|
|
15
|
+
}
|
|
16
|
+
export {
|
|
17
|
+
enableAutoPreviews
|
|
18
|
+
};
|
|
19
|
+
//# sourceMappingURL=enableAutoPreviews.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"enableAutoPreviews.js","sources":["../../src/pages/enableAutoPreviews.ts"],"sourcesContent":["import type { PreviewData } from \"next\";\nimport type { Client } from \"@prismicio/client\";\n\nimport type { NextApiRequestLike } from \"./types.js\";\n\n/**\n * Configuration for `enableAutoPreviews`.\n *\n * @typeParam TPreviewData - Next.js preview data object.\n */\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\t/**\n\t * The `previewData` object provided in the `getStaticProps()` or\n\t * `getServerSideProps()` context object.\n\t */\n\tpreviewData?: PreviewData;\n\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\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 function enableAutoPreviews(config: EnableAutoPreviewsConfig): 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.\n\n\t\tif (isPrismicPreviewData(config.previewData)) {\n\t\t\tconfig.client.queryContentFromRef(config.previewData.ref);\n\t\t}\n\n\t\treturn;\n\t}\n\n\tif (\"req\" in config && config.req) {\n\t\t// Assume we are in an API Route.\n\n\t\tconfig.client.enableAutoPreviewsFromReq(config.req);\n\n\t\treturn;\n\t}\n}\n\nfunction isPrismicPreviewData(input: unknown): input is { ref: string } {\n\treturn typeof input === \"object\" && input !== null && \"ref\" in input;\n}\n"],"names":[],"mappings":"AAsCM,SAAU,mBAAmB,QAAgC;AAC9D,MAAA,iBAAiB,UAAU,OAAO,aAAa;AAI9C,QAAA,qBAAqB,OAAO,WAAW,GAAG;AAC7C,aAAO,OAAO,oBAAoB,OAAO,YAAY,GAAG;AAAA,IAAA;AAGzD;AAAA,EAAA;AAGG,MAAA,SAAS,UAAU,OAAO,KAAK;AAG3B,WAAA,OAAO,0BAA0B,OAAO,GAAG;AAElD;AAAA,EAAA;AAEF;AAEA,SAAS,qBAAqB,OAAc;AAC3C,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,SAAS;AAChE;"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { NextApiRequestLike, NextApiResponseLike } from "./types.js";
|
|
2
|
+
/** Configuration for `exitPreview()`. */
|
|
3
|
+
export type ExitPreviewAPIRouteConfig = {
|
|
4
|
+
/**
|
|
5
|
+
* The `req` object from a Next.js API route.
|
|
6
|
+
*
|
|
7
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
8
|
+
*/
|
|
9
|
+
req?: NextApiRequestLike;
|
|
10
|
+
/**
|
|
11
|
+
* The `res` object from a Next.js API route.
|
|
12
|
+
*
|
|
13
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
14
|
+
*/
|
|
15
|
+
res: NextApiResponseLike;
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* Ends a Prismic preview session within a Next.js app. This function should be
|
|
19
|
+
* used in an API route.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
*
|
|
23
|
+
* ```typescript
|
|
24
|
+
* // src/pages/api/exit-preview.js
|
|
25
|
+
*
|
|
26
|
+
* import { exitPreview } from "@prismicio/next";
|
|
27
|
+
*
|
|
28
|
+
* export default function handler(_req, res) {
|
|
29
|
+
* return exitPreview({ res });
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare function exitPreview(config: ExitPreviewAPIRouteConfig): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exitPreview.js","sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import type { NextApiRequestLike, NextApiResponseLike } from \"./types.js\";\n\n/** Configuration for `exitPreview()`. */\nexport type ExitPreviewAPIRouteConfig = {\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\treq?: NextApiRequestLike;\n\n\t/**\n\t * The `res` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\tres: NextApiResponseLike;\n};\n\n/**\n * Ends a Prismic preview session within a Next.js app. This function should be\n * used in an API route.\n *\n * @example\n *\n * ```typescript\n * // src/pages/api/exit-preview.js\n *\n * import { exitPreview } from \"@prismicio/next\";\n *\n * export default function handler(_req, res) {\n * \treturn exitPreview({ res });\n * }\n * ```\n */\nexport function exitPreview(config: ExitPreviewAPIRouteConfig): void {\n\tconfig.res.clearPreviewData();\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\tconfig.res.setHeader(\"Cache-Control\", \"no-store\");\n\n\tconfig.res.json({ success: true });\n\n\treturn;\n}\n"],"names":[],"mappings":"AAmCM,SAAU,YAAY,QAAiC;AAC5D,SAAO,IAAI;AAGJ,SAAA,IAAI,UAAU,iBAAiB,UAAU;AAEhD,SAAO,IAAI,KAAK,EAAE,SAAS,MAAM;AAEjC;AACD;"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { PrismicNextLink } from "../PrismicNextLink.js";
|
|
2
|
+
export { type PrismicNextLinkProps } from "../PrismicNextLink.js";
|
|
3
|
+
export { PrismicNextImage } from "../PrismicNextImage.js";
|
|
4
|
+
export { type PrismicNextImageProps } from "../PrismicNextImage.js";
|
|
5
|
+
export { imgixLoader } from "../imgixLoader.js";
|
|
6
|
+
export { PrismicPreview } from "./PrismicPreview.js";
|
|
7
|
+
export type { PrismicPreviewProps } from "./PrismicPreview.js";
|
|
8
|
+
export { enableAutoPreviews } from "./enableAutoPreviews.js";
|
|
9
|
+
export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews.js";
|
|
10
|
+
export { redirectToPreviewURL } from "./redirectToPreviewURL.js";
|
|
11
|
+
export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL.js";
|
|
12
|
+
export { exitPreview } from "./exitPreview.js";
|
|
13
|
+
export type { ExitPreviewAPIRouteConfig } from "./exitPreview.js";
|
|
14
|
+
export { setPreviewData } from "./setPreviewData.js";
|
|
15
|
+
export type { SetPreviewDataConfig } from "./setPreviewData.js";
|
|
16
|
+
export type { CreateClientConfig } from "./types.js";
|
|
17
|
+
export { createLocaleRedirect } from "../createLocaleRedirect.js";
|
|
18
|
+
export type { CreateLocaleRedirectConfig } from "../createLocaleRedirect.js";
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { LinkResolverFunction, Client } from "@prismicio/client";
|
|
2
|
+
import type { NextApiRequestLike, NextApiResponseLike } from "./types.js";
|
|
3
|
+
export type RedirectToPreviewURLConfig = {
|
|
4
|
+
/** The Prismic client configured for the preview session's repository. */
|
|
5
|
+
client: Pick<Client, "resolvePreviewURL">;
|
|
6
|
+
/**
|
|
7
|
+
* The `req` object from a Next.js API route.
|
|
8
|
+
*
|
|
9
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\>
|
|
10
|
+
*/
|
|
11
|
+
req: NextApiRequestLike;
|
|
12
|
+
/**
|
|
13
|
+
* The `res` object from a Next.js API route.
|
|
14
|
+
*
|
|
15
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\>
|
|
16
|
+
*/
|
|
17
|
+
res: NextApiResponseLike;
|
|
18
|
+
/**
|
|
19
|
+
* A Link Resolver used to resolve the previewed document's URL.
|
|
20
|
+
*
|
|
21
|
+
* @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
|
|
22
|
+
*/
|
|
23
|
+
linkResolver?: LinkResolverFunction;
|
|
24
|
+
/**
|
|
25
|
+
* The default redirect URL if a URL cannot be determined for the previewed
|
|
26
|
+
* document.
|
|
27
|
+
*
|
|
28
|
+
* **Note**: If you `next.config.js` file contains a `basePath`, the
|
|
29
|
+
* `defaultURL` option must _not_ include it. Instead, provide the `basePath`
|
|
30
|
+
* property using the `basePath` option.
|
|
31
|
+
*/
|
|
32
|
+
defaultURL?: string;
|
|
33
|
+
/**
|
|
34
|
+
* The `basePath` for the Next.js app as it is defined in `next.config.js`.
|
|
35
|
+
* This option can be omitted if the app does not have a `basePath`.
|
|
36
|
+
*
|
|
37
|
+
* @remarks
|
|
38
|
+
* The Router Handler or API route is unable to detect the app's `basePath`
|
|
39
|
+
* automatically. It must be provided to `redirectToPreviewURL()` manually.
|
|
40
|
+
*/
|
|
41
|
+
basePath?: string;
|
|
42
|
+
};
|
|
43
|
+
export declare function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<void>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
async function redirectToPreviewURL(config) {
|
|
2
|
+
var _a;
|
|
3
|
+
const { client, req, res, linkResolver, defaultURL = "/", basePath = "" } = config;
|
|
4
|
+
const previewToken = (_a = req.query.token) == null ? void 0 : _a.toString();
|
|
5
|
+
const previewURL = await client.resolvePreviewURL({
|
|
6
|
+
linkResolver,
|
|
7
|
+
defaultURL,
|
|
8
|
+
previewToken
|
|
9
|
+
});
|
|
10
|
+
res.redirect(basePath + previewURL);
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
redirectToPreviewURL
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=redirectToPreviewURL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.js","sources":["../../src/pages/redirectToPreviewURL.ts"],"sourcesContent":["import type { LinkResolverFunction, Client } from \"@prismicio/client\";\n\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types.js\";\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"resolvePreviewURL\">;\n\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\\>\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/pages/building-your-application/routing/api-routes\\>\n\t */\n\tres: NextApiResponseLike;\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction;\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed\n\t * document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the\n\t * `defaultURL` option must _not_ include it. Instead, provide the `basePath`\n\t * property using the `basePath` option.\n\t */\n\tdefaultURL?: string;\n\n\t/**\n\t * The `basePath` for the Next.js app as it is defined in `next.config.js`.\n\t * This option can be omitted if the app does not have a `basePath`.\n\t *\n\t * @remarks\n\t * The Router Handler or API route is unable to detect the app's `basePath`\n\t * automatically. It must be provided to `redirectToPreviewURL()` manually.\n\t */\n\tbasePath?: string;\n};\n\nexport async function redirectToPreviewURL(\n\tconfig: RedirectToPreviewURLConfig,\n): Promise<void> {\n\tconst {\n\t\tclient,\n\t\treq,\n\t\tres,\n\t\tlinkResolver,\n\t\tdefaultURL = \"/\",\n\t\tbasePath = \"\",\n\t} = config;\n\n\tconst previewToken = req.query.token?.toString();\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tlinkResolver,\n\t\tdefaultURL,\n\t\tpreviewToken,\n\t});\n\n\tres.redirect(basePath + previewURL);\n\n\treturn;\n}\n"],"names":[],"mappings":"AAqDA,eAAsB,qBACrB,QAAkC;AADnC;AAGO,QAAA,EACL,QACA,KACA,KACA,cACA,aAAa,KACb,WAAW,GAAA,IACR;AAEJ,QAAM,gBAAe,SAAI,MAAM,UAAV,mBAAiB;AAEhC,QAAA,aAAa,MAAM,OAAO,kBAAkB;AAAA,IACjD;AAAA,IACA;AAAA,IACA;AAAA,EAAA,CACA;AAEG,MAAA,SAAS,WAAW,UAAU;AAElC;AACD;"}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { NextApiRequestLike, NextApiResponseLike } from "./types";
|
|
2
|
-
/**
|
|
3
|
-
* Configuration for `setPreviewData`.
|
|
4
|
-
*/
|
|
1
|
+
import type { NextApiRequestLike, NextApiResponseLike } from "./types.js";
|
|
2
|
+
/** Configuration for `setPreviewData`. */
|
|
5
3
|
export type SetPreviewDataConfig = {
|
|
6
4
|
/**
|
|
7
5
|
* The `req` object from a Next.js API route.
|
|
@@ -16,9 +14,5 @@ export type SetPreviewDataConfig = {
|
|
|
16
14
|
*/
|
|
17
15
|
res: NextApiResponseLike;
|
|
18
16
|
};
|
|
19
|
-
/**
|
|
20
|
-
* **Only use this function in the Pages Directory (/pages).**
|
|
21
|
-
*
|
|
22
|
-
* Set Prismic preview data for Next.js's Preview Mode.
|
|
23
|
-
*/
|
|
17
|
+
/** Set Prismic preview data for Next.js's Preview Mode. */
|
|
24
18
|
export declare function setPreviewData({ req, res }: SetPreviewDataConfig): void;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { cookie } from "@prismicio/client";
|
|
2
2
|
function setPreviewData({ req, res }) {
|
|
3
|
-
const ref = req.query.token || req.cookies[preview];
|
|
3
|
+
const ref = req.query.token || req.cookies[cookie.preview];
|
|
4
4
|
if (ref) {
|
|
5
5
|
res.setPreviewData({ ref });
|
|
6
6
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setPreviewData.js","sources":["../../src/pages/setPreviewData.ts"],"sourcesContent":["import { cookie } from \"@prismicio/client\";\n\nimport type { NextApiRequestLike, NextApiResponseLike } from \"./types.js\";\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 ref = req.query.token || req.cookies[cookie.preview];\n\n\tif (ref) {\n\t\tres.setPreviewData({ ref });\n\t}\n}\n"],"names":[],"mappings":";SAsBgB,eAAe,EAAE,KAAK,OAA2B;AAChE,QAAM,MAAM,IAAI,MAAM,SAAS,IAAI,QAAQ,OAAO,OAAO;AAEzD,MAAI,KAAK;AACJ,QAAA,eAAe,EAAE,KAAK;AAAA,EAAA;AAE5B;"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ClientConfig } from "@prismicio/client";
|
|
2
|
+
import { EnableAutoPreviewsConfig } from "./enableAutoPreviews.js";
|
|
3
|
+
/**
|
|
4
|
+
* Configuration for creating a Prismic client with automatic preview support in
|
|
5
|
+
* Next.js apps.
|
|
6
|
+
*/
|
|
7
|
+
export type CreateClientConfig = ClientConfig & Pick<EnableAutoPreviewsConfig, "previewData" | "req">;
|
|
8
|
+
/**
|
|
9
|
+
* The minimal set of properties needed from `next`'s `NextApiRequest` type.
|
|
10
|
+
*
|
|
11
|
+
* This request type is only compatible with API routes defined in the `pages`
|
|
12
|
+
* directory.
|
|
13
|
+
*/
|
|
14
|
+
export type NextApiRequestLike = {
|
|
15
|
+
query: Partial<Record<string, string | string[]>>;
|
|
16
|
+
cookies: Partial<Record<string, string>>;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The minimal set of properties needed from `next`'s `NextApiResponse` type.
|
|
20
|
+
*
|
|
21
|
+
* This request type is only compatible with API routes defined in the `pages`
|
|
22
|
+
* directory.
|
|
23
|
+
*/
|
|
24
|
+
export type NextApiResponseLike = {
|
|
25
|
+
redirect(url: string): NextApiResponseLike;
|
|
26
|
+
clearPreviewData(): NextApiResponseLike;
|
|
27
|
+
setHeader(name: string, value: string | string[]): NextApiResponseLike;
|
|
28
|
+
json(body: any): void;
|
|
29
|
+
setPreviewData(data: object | string): NextApiResponseLike;
|
|
30
|
+
};
|
package/dist/pages.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { PrismicNextLink } from "./PrismicNextLink.js";
|
|
2
|
+
import { PrismicNextImage } from "./PrismicNextImage.js";
|
|
3
|
+
import { imgixLoader } from "./imgixLoader.js";
|
|
4
|
+
import { PrismicPreview } from "./pages/PrismicPreview.js";
|
|
5
|
+
import { enableAutoPreviews } from "./pages/enableAutoPreviews.js";
|
|
6
|
+
import { redirectToPreviewURL } from "./pages/redirectToPreviewURL.js";
|
|
7
|
+
import { exitPreview } from "./pages/exitPreview.js";
|
|
8
|
+
import { setPreviewData } from "./pages/setPreviewData.js";
|
|
9
|
+
import { createLocaleRedirect } from "./createLocaleRedirect.js";
|
|
10
|
+
export {
|
|
11
|
+
PrismicNextImage,
|
|
12
|
+
PrismicNextLink,
|
|
13
|
+
PrismicPreview,
|
|
14
|
+
createLocaleRedirect,
|
|
15
|
+
enableAutoPreviews,
|
|
16
|
+
exitPreview,
|
|
17
|
+
imgixLoader,
|
|
18
|
+
redirectToPreviewURL,
|
|
19
|
+
setPreviewData
|
|
20
|
+
};
|
|
21
|
+
//# sourceMappingURL=pages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pages.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
|
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
type
|
|
1
|
+
import { type Client, type LinkResolverFunction } from "@prismicio/client";
|
|
2
|
+
import { NextRequestLike } from "./types.js";
|
|
3
|
+
export type RedirectToPreviewURLConfig = {
|
|
4
|
+
/** The Prismic client configured for the preview session's repository. */
|
|
5
|
+
client: Pick<Client, "resolvePreviewURL">;
|
|
4
6
|
/**
|
|
5
|
-
* The
|
|
7
|
+
* The `request` object from a Next.js Route Handler.
|
|
8
|
+
*
|
|
9
|
+
* @see Next.js Route Handler docs: \<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\>
|
|
6
10
|
*/
|
|
7
|
-
|
|
11
|
+
request: NextRequestLike;
|
|
8
12
|
/**
|
|
9
13
|
* A Link Resolver used to resolve the previewed document's URL.
|
|
10
14
|
*
|
|
11
15
|
* @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
|
|
12
16
|
*/
|
|
13
|
-
linkResolver?:
|
|
17
|
+
linkResolver?: LinkResolverFunction;
|
|
14
18
|
/**
|
|
15
19
|
* The default redirect URL if a URL cannot be determined for the previewed
|
|
16
20
|
* document.
|
|
@@ -20,39 +24,5 @@ type RedirectToPreviewURLConfigBase = {
|
|
|
20
24
|
* property using the `basePath` option.
|
|
21
25
|
*/
|
|
22
26
|
defaultURL?: string;
|
|
23
|
-
/**
|
|
24
|
-
* The `basePath` for the Next.js app as it is defined in `next.config.js`.
|
|
25
|
-
* This option can be omitted if the app does not have a `basePath`.
|
|
26
|
-
*
|
|
27
|
-
* @remarks
|
|
28
|
-
* The Router Handler or API route is unable to detect the app's `basePath`
|
|
29
|
-
* automatically. It must be provided to `redirectToPreviewURL()` manually.
|
|
30
|
-
*/
|
|
31
|
-
basePath?: string;
|
|
32
|
-
};
|
|
33
|
-
export type RedirectToPreviewURLRouteHandlerConfig = RedirectToPreviewURLConfigBase & {
|
|
34
|
-
/**
|
|
35
|
-
* The `request` object from a Next.js Route Handler.
|
|
36
|
-
*
|
|
37
|
-
* @see Next.js Route Handler docs: \<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\>
|
|
38
|
-
*/
|
|
39
|
-
request: NextRequestLike;
|
|
40
|
-
};
|
|
41
|
-
export type RedirectToPreviewURLAPIEndpointConfig = RedirectToPreviewURLConfigBase & {
|
|
42
|
-
/**
|
|
43
|
-
* The `req` object from a Next.js API route.
|
|
44
|
-
*
|
|
45
|
-
* @see Next.js API route docs: \<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\>
|
|
46
|
-
*/
|
|
47
|
-
req: NextApiRequestLike;
|
|
48
|
-
/**
|
|
49
|
-
* The `res` object from a Next.js API route.
|
|
50
|
-
*
|
|
51
|
-
* @see Next.js API route docs: \<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\>
|
|
52
|
-
*/
|
|
53
|
-
res: NextApiResponseLike;
|
|
54
27
|
};
|
|
55
|
-
export
|
|
56
|
-
export declare function redirectToPreviewURL(config: RedirectToPreviewURLRouteHandlerConfig): Promise<never>;
|
|
57
|
-
export declare function redirectToPreviewURL(config: RedirectToPreviewURLAPIEndpointConfig): Promise<void>;
|
|
58
|
-
export {};
|
|
28
|
+
export declare function redirectToPreviewURL(config: RedirectToPreviewURLConfig): Promise<never>;
|
|
@@ -1,28 +1,22 @@
|
|
|
1
1
|
import { redirect } from "next/navigation";
|
|
2
|
-
import {
|
|
3
|
-
import { preview } from './_node_modules/@prismicio/client/dist/cookie.js';
|
|
2
|
+
import { cookie } from "@prismicio/client";
|
|
4
3
|
async function redirectToPreviewURL(config) {
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
if ("nextUrl" in request) {
|
|
13
|
-
draftMode().enable();
|
|
14
|
-
const previewCookie = request.nextUrl.searchParams.get("token");
|
|
15
|
-
if (previewCookie) {
|
|
16
|
-
cookies().set(preview, previewCookie);
|
|
17
|
-
}
|
|
18
|
-
redirect(basePath + previewUrl);
|
|
19
|
-
} else {
|
|
20
|
-
if (!("res" in config)) {
|
|
21
|
-
throw new Error("[redirectToPreviewURL] The `res` object from the API route must be provided to `redirectToPreviewURL()`.");
|
|
22
|
-
}
|
|
23
|
-
config.res.redirect(basePath + previewUrl);
|
|
24
|
-
return;
|
|
4
|
+
const { client, request, linkResolver, defaultURL = "/" } = config;
|
|
5
|
+
const { cookies, draftMode } = await import("next/headers");
|
|
6
|
+
const documentID = request.nextUrl.searchParams.get("documentId") ?? void 0;
|
|
7
|
+
const previewToken = request.nextUrl.searchParams.get("token") ?? void 0;
|
|
8
|
+
if (previewToken) {
|
|
9
|
+
const cookieJar = await cookies();
|
|
10
|
+
cookieJar.set(cookie.preview, previewToken);
|
|
25
11
|
}
|
|
12
|
+
const previewURL = await client.resolvePreviewURL({
|
|
13
|
+
documentID,
|
|
14
|
+
previewToken,
|
|
15
|
+
defaultURL,
|
|
16
|
+
linkResolver
|
|
17
|
+
});
|
|
18
|
+
(await draftMode()).enable();
|
|
19
|
+
redirect(previewURL);
|
|
26
20
|
}
|
|
27
21
|
export {
|
|
28
22
|
redirectToPreviewURL
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"redirectToPreviewURL.js","sources":["
|
|
1
|
+
{"version":3,"file":"redirectToPreviewURL.js","sources":["../src/redirectToPreviewURL.ts"],"sourcesContent":["import { redirect } from \"next/navigation\";\nimport {\n\tcookie as prismicCookie,\n\ttype Client,\n\ttype LinkResolverFunction,\n} from \"@prismicio/client\";\n\nimport { NextRequestLike } from \"./types.js\";\n\nexport type RedirectToPreviewURLConfig = {\n\t/** The Prismic client configured for the preview session's repository. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"resolvePreviewURL\">;\n\n\t/**\n\t * The `request` object from a Next.js Route Handler.\n\t *\n\t * @see Next.js Route Handler docs: \\<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\\>\n\t */\n\trequest: NextRequestLike;\n\n\t/**\n\t * A Link Resolver used to resolve the previewed document's URL.\n\t *\n\t * @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}\n\t */\n\tlinkResolver?: LinkResolverFunction;\n\n\t/**\n\t * The default redirect URL if a URL cannot be determined for the previewed\n\t * document.\n\t *\n\t * **Note**: If you `next.config.js` file contains a `basePath`, the\n\t * `defaultURL` option must _not_ include it. Instead, provide the `basePath`\n\t * property using the `basePath` option.\n\t */\n\tdefaultURL?: string;\n};\n\nexport async function redirectToPreviewURL(\n\tconfig: RedirectToPreviewURLConfig,\n): Promise<never> {\n\tconst { client, request, linkResolver, defaultURL = \"/\" } = config;\n\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { cookies, draftMode } = await import(\"next/headers\");\n\n\tconst documentID =\n\t\trequest.nextUrl.searchParams.get(\"documentId\") ?? undefined;\n\n\t// Set the initial preview cookie. Setting the cookie here is necessary\n\t// to support unpublished previews. Without setting it here, the page\n\t// will try to render without the preview cookie, leading to a\n\t// PrismicNotFound error.\n\tconst previewToken = request.nextUrl.searchParams.get(\"token\") ?? undefined;\n\tif (previewToken) {\n\t\tconst cookieJar = await cookies();\n\t\tcookieJar.set(prismicCookie.preview, previewToken);\n\t}\n\n\tconst previewURL = await client.resolvePreviewURL({\n\t\tdocumentID,\n\t\tpreviewToken,\n\t\tdefaultURL,\n\t\tlinkResolver,\n\t});\n\n\t(await draftMode()).enable();\n\n\tredirect(previewURL);\n}\n"],"names":["prismicCookie"],"mappings":";;AAyCA,eAAsB,qBACrB,QAAkC;AAElC,QAAM,EAAE,QAAQ,SAAS,cAAc,aAAa,QAAQ;AAK5D,QAAM,EAAE,SAAS,cAAc,MAAM,OAAO,cAAc;AAE1D,QAAM,aACL,QAAQ,QAAQ,aAAa,IAAI,YAAY,KAAK;AAMnD,QAAM,eAAe,QAAQ,QAAQ,aAAa,IAAI,OAAO,KAAK;AAClE,MAAI,cAAc;AACX,UAAA,YAAY,MAAM;AACd,cAAA,IAAIA,OAAc,SAAS,YAAY;AAAA,EAAA;AAG5C,QAAA,aAAa,MAAM,OAAO,kBAAkB;AAAA,IACjD;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EAAA,CACA;AAEA,GAAA,MAAM,UAAS,GAAI;AAEpB,WAAS,UAAU;AACpB;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,37 +1,11 @@
|
|
|
1
|
-
import type { PreviewData } from "next";
|
|
2
1
|
import type { ClientConfig } from "@prismicio/client";
|
|
3
2
|
declare module "@prismicio/client" {
|
|
4
3
|
interface RequestInitLike {
|
|
5
4
|
next?: RequestInit["next"];
|
|
6
5
|
}
|
|
7
6
|
}
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
*/
|
|
11
|
-
export type PrismicPreviewData = {
|
|
12
|
-
ref: string;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* Configuration for creating a Prismic client with automatic preview support in
|
|
16
|
-
* Next.js apps.
|
|
17
|
-
*/
|
|
18
|
-
export type CreateClientConfig = ClientConfig & {
|
|
19
|
-
/**
|
|
20
|
-
* **Only used in the Pages Directory (/pages).**
|
|
21
|
-
*
|
|
22
|
-
* The `previewData` object provided in the `getStaticProps()` or
|
|
23
|
-
* `getServerSideProps()` context object.
|
|
24
|
-
*/
|
|
25
|
-
previewData?: PreviewData;
|
|
26
|
-
/**
|
|
27
|
-
* **Only used in the Pages Directory (/pages).**
|
|
28
|
-
*
|
|
29
|
-
* The `req` object from a Next.js API route.
|
|
30
|
-
*
|
|
31
|
-
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
32
|
-
*/
|
|
33
|
-
req?: NextApiRequestLike;
|
|
34
|
-
};
|
|
7
|
+
/** @deprecated Use `@prismicio/client`'s `ClientConfig`. */
|
|
8
|
+
export type CreateClientConfig = ClientConfig;
|
|
35
9
|
/**
|
|
36
10
|
* The minimal set of properties needed from `next`'s `NextRequest` type.
|
|
37
11
|
*
|
|
@@ -50,26 +24,3 @@ export type NextRequestLike = {
|
|
|
50
24
|
};
|
|
51
25
|
};
|
|
52
26
|
};
|
|
53
|
-
/**
|
|
54
|
-
* The minimal set of properties needed from `next`'s `NextApiRequest` type.
|
|
55
|
-
*
|
|
56
|
-
* This request type is only compatible with API routes defined in the `pages`
|
|
57
|
-
* directory.
|
|
58
|
-
*/
|
|
59
|
-
export type NextApiRequestLike = {
|
|
60
|
-
query: Partial<Record<string, string | string[]>>;
|
|
61
|
-
cookies: Partial<Record<string, string>>;
|
|
62
|
-
};
|
|
63
|
-
/**
|
|
64
|
-
* The minimal set of properties needed from `next`'s `NextApiResponse` type.
|
|
65
|
-
*
|
|
66
|
-
* This request type is only compatible with API routes defined in the `pages`
|
|
67
|
-
* directory.
|
|
68
|
-
*/
|
|
69
|
-
export type NextApiResponseLike = {
|
|
70
|
-
redirect(url: string): NextApiResponseLike;
|
|
71
|
-
clearPreviewData(): NextApiResponseLike;
|
|
72
|
-
setHeader(name: string, value: string | string[]): NextApiResponseLike;
|
|
73
|
-
json(body: any): void;
|
|
74
|
-
setPreviewData(data: object | string): NextApiResponseLike;
|
|
75
|
-
};
|