@prismicio/next 1.3.0 → 1.3.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.
@@ -7,7 +7,11 @@ const PrismicPreviewClient = require("./PrismicPreviewClient.cjs");
7
7
  const getToolbarSrc = require('./_node_modules/@prismicio/client/dist/getToolbarSrc.cjs');
8
8
  function PrismicPreview({ repositoryName, children, ...props }) {
9
9
  const toolbarSrc = getToolbarSrc.getToolbarSrc(repositoryName);
10
- const isDraftMode = headers.draftMode().isEnabled;
10
+ let isDraftMode = false;
11
+ try {
12
+ isDraftMode = headers.draftMode().isEnabled;
13
+ } catch {
14
+ }
11
15
  return jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [children, jsxRuntime.jsx(PrismicPreviewClient.PrismicPreviewClient, { repositoryName, isDraftMode, ...props }), jsxRuntime.jsx(Script, { src: toolbarSrc, strategy: "lazyOnload" })] });
12
16
  }
13
17
  exports.PrismicPreview = PrismicPreview;
@@ -1 +1 @@
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;;"}
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\n\tlet isDraftMode = false;\n\ttry {\n\t\tisDraftMode = draftMode().isEnabled;\n\t} catch {\n\t\t// noop - `requestAsyncStorage` propbably doesn't exist, such as\n\t\t// in the Pages Router, which causes `draftMode()` to throw. We\n\t\t// can ignore this case and assume Draft Mode is disabled.\n\t}\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;AAEvD,MAAI,cAAc;AACd,MAAA;AACH,kBAAcC,QAAAA,UAAY,EAAA;AAAA,EAAA,QACzB;AAAA,EAID;AAGA,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;;"}
@@ -5,7 +5,11 @@ import { PrismicPreviewClient } from "./PrismicPreviewClient.js";
5
5
  import { getToolbarSrc } from './_node_modules/@prismicio/client/dist/getToolbarSrc.js';
6
6
  function PrismicPreview({ repositoryName, children, ...props }) {
7
7
  const toolbarSrc = getToolbarSrc(repositoryName);
8
- const isDraftMode = draftMode().isEnabled;
8
+ let isDraftMode = false;
9
+ try {
10
+ isDraftMode = draftMode().isEnabled;
11
+ } catch {
12
+ }
9
13
  return jsxs(Fragment, { children: [children, jsx(PrismicPreviewClient, { repositoryName, isDraftMode, ...props }), jsx(Script, { src: toolbarSrc, strategy: "lazyOnload" })] });
10
14
  }
11
15
  export {
@@ -1 +1 @@
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;"}
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\n\tlet isDraftMode = false;\n\ttry {\n\t\tisDraftMode = draftMode().isEnabled;\n\t} catch {\n\t\t// noop - `requestAsyncStorage` propbably doesn't exist, such as\n\t\t// in the Pages Router, which causes `draftMode()` to throw. We\n\t\t// can ignore this case and assume Draft Mode is disabled.\n\t}\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;AAEvD,MAAI,cAAc;AACd,MAAA;AACH,kBAAc,UAAY,EAAA;AAAA,EAAA,QACzB;AAAA,EAID;AAGA,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;"}
@@ -1,11 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const headers = require("next/headers");
4
- async function exitPreview(config) {
4
+ function exitPreview(config) {
5
5
  if (config == null ? void 0 : config.res) {
6
6
  config.res.clearPreviewData();
7
- config.res.json({ success: true });
8
7
  config.res.setHeader("Cache-Control", "no-store");
8
+ config.res.json({ success: true });
9
9
  return;
10
10
  } else {
11
11
  headers.draftMode().disable();
@@ -1 +1 @@
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;;"}
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 function exitPreview(config?: ExitPreviewConfig): 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// `Cache-Control` header is used to prevent CDN-level caching.\n\t\tconfig.res.setHeader(\"Cache-Control\", \"no-store\");\n\n\t\tconfig.res.json({ success: true });\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// `Cache-Control` header is used to prevent CDN-level caching.\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":";;;AA2DM,SAAU,YAAY,QAA0B;AACrD,MAAI,iCAAQ,KAAK;AAGhB,WAAO,IAAI;AAGJ,WAAA,IAAI,UAAU,iBAAiB,UAAU;AAEhD,WAAO,IAAI,KAAK,EAAE,SAAS,KAAM,CAAA;AAEjC;AAAA,EAAA,OACM;AAGNA,YAAA,UAAA,EAAY;AAGL,WAAA,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,KAAA,CAAM,GAAG;AAAA,MACtD,SAAS;AAAA,QACR,iBAAiB;AAAA,MACjB;AAAA,IAAA,CACD;AAAA,EACD;AACF;;"}
@@ -52,4 +52,4 @@ export type ExitPreviewConfig = {
52
52
  * }
53
53
  * ```
54
54
  */
55
- export declare function exitPreview(config?: ExitPreviewConfig): Promise<Response | void>;
55
+ export declare function exitPreview(config?: ExitPreviewConfig): Response | void;
@@ -1,9 +1,9 @@
1
1
  import { draftMode } from "next/headers";
2
- async function exitPreview(config) {
2
+ function exitPreview(config) {
3
3
  if (config == null ? void 0 : config.res) {
4
4
  config.res.clearPreviewData();
5
- config.res.json({ success: true });
6
5
  config.res.setHeader("Cache-Control", "no-store");
6
+ config.res.json({ success: true });
7
7
  return;
8
8
  } else {
9
9
  draftMode().disable();
@@ -1 +1 @@
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
+ {"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 function exitPreview(config?: ExitPreviewConfig): 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// `Cache-Control` header is used to prevent CDN-level caching.\n\t\tconfig.res.setHeader(\"Cache-Control\", \"no-store\");\n\n\t\tconfig.res.json({ success: true });\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// `Cache-Control` header is used to prevent CDN-level caching.\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":";AA2DM,SAAU,YAAY,QAA0B;AACrD,MAAI,iCAAQ,KAAK;AAGhB,WAAO,IAAI;AAGJ,WAAA,IAAI,UAAU,iBAAiB,UAAU;AAEhD,WAAO,IAAI,KAAK,EAAE,SAAS,KAAM,CAAA;AAEjC;AAAA,EAAA,OACM;AAGN,cAAA,EAAY;AAGL,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,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const version = "1.3.0";
3
+ const version = "1.3.1";
4
4
  exports.version = version;
5
5
  //# sourceMappingURL=package.json.cjs.map
@@ -1,4 +1,4 @@
1
- const version = "1.3.0";
1
+ const version = "1.3.1";
2
2
  export {
3
3
  version
4
4
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/next",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
4
4
  "description": "Helpers to integrate Prismic into Next.js apps",
5
5
  "keywords": [
6
6
  "typescript",
@@ -52,7 +52,15 @@ export function PrismicPreview({
52
52
  ...props
53
53
  }: PrismicPreviewProps): JSX.Element {
54
54
  const toolbarSrc = prismic.getToolbarSrc(repositoryName);
55
- const isDraftMode = draftMode().isEnabled;
55
+
56
+ let isDraftMode = false;
57
+ try {
58
+ isDraftMode = draftMode().isEnabled;
59
+ } catch {
60
+ // noop - `requestAsyncStorage` propbably doesn't exist, such as
61
+ // in the Pages Router, which causes `draftMode()` to throw. We
62
+ // can ignore this case and assume Draft Mode is disabled.
63
+ }
56
64
 
57
65
  return (
58
66
  <>
@@ -57,27 +57,24 @@ export type ExitPreviewConfig = {
57
57
  * }
58
58
  * ```
59
59
  */
60
- export async function exitPreview(
61
- config?: ExitPreviewConfig,
62
- ): Promise<Response | void> {
60
+ export function exitPreview(config?: ExitPreviewConfig): Response | void {
63
61
  if (config?.res) {
64
62
  // Assume Preview Mode is being used.
65
63
 
66
64
  config.res.clearPreviewData();
67
65
 
68
- // 205 status is used to prevent CDN-level caching. The default 200
69
- // status code is typically treated as non-changing and cacheable.
70
- config.res.json({ success: true });
66
+ // `Cache-Control` header is used to prevent CDN-level caching.
71
67
  config.res.setHeader("Cache-Control", "no-store");
72
68
 
69
+ config.res.json({ success: true });
70
+
73
71
  return;
74
72
  } else {
75
73
  // Assume Draft Mode is being used.
76
74
 
77
75
  draftMode().disable();
78
76
 
79
- // 205 status is used to prevent CDN-level caching. The default 200
80
- // status code is typically treated as non-changing and cacheable.
77
+ // `Cache-Control` header is used to prevent CDN-level caching.
81
78
  return new Response(JSON.stringify({ success: true }), {
82
79
  headers: {
83
80
  "Cache-Control": "no-store",