@prismicio/next 2.2.0 → 2.2.1-canary.0a22932

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.
Files changed (67) hide show
  1. package/dist/PrismicNextImage.d.ts +57 -0
  2. package/dist/PrismicNextImage.js +69 -0
  3. package/dist/PrismicNextImage.js.map +1 -0
  4. package/dist/PrismicNextLink.d.cts +3 -3
  5. package/dist/PrismicNextLink.d.ts +54 -0
  6. package/dist/PrismicNextLink.js +29 -0
  7. package/dist/PrismicNextLink.js.map +1 -0
  8. package/dist/PrismicPreview.d.ts +36 -0
  9. package/dist/PrismicPreview.js +39 -0
  10. package/dist/PrismicPreview.js.map +1 -0
  11. package/dist/PrismicPreviewClient.js +76 -0
  12. package/dist/PrismicPreviewClient.js.map +1 -0
  13. package/dist/SliceSimulator.d.ts +25 -0
  14. package/dist/SliceSimulator.js +48 -0
  15. package/dist/SliceSimulator.js.map +1 -0
  16. package/dist/SliceSimulatorWrapper.js +35 -0
  17. package/dist/SliceSimulatorWrapper.js.map +1 -0
  18. package/dist/createLocaleRedirect.d.ts +14 -0
  19. package/dist/createLocaleRedirect.js +14 -0
  20. package/dist/createLocaleRedirect.js.map +1 -0
  21. package/dist/enableAutoPreviews.d.ts +18 -0
  22. package/dist/enableAutoPreviews.js +29 -0
  23. package/dist/enableAutoPreviews.js.map +1 -0
  24. package/dist/exitPreview.d.ts +21 -0
  25. package/dist/exitPreview.js +26 -0
  26. package/dist/exitPreview.js.map +1 -0
  27. package/dist/getSlices.d.ts +7 -0
  28. package/dist/getSlices.js +11 -0
  29. package/dist/getSlices.js.map +1 -0
  30. package/dist/imgixLoader.d.ts +15 -0
  31. package/dist/imgixLoader.js +23 -0
  32. package/dist/imgixLoader.js.map +1 -0
  33. package/dist/index.d.ts +12 -0
  34. package/dist/index.js +12 -0
  35. package/dist/lib/devMsg.js +25 -0
  36. package/dist/lib/devMsg.js.map +1 -0
  37. package/dist/lib/resolveDefaultExport.js +25 -0
  38. package/dist/lib/resolveDefaultExport.js.map +1 -0
  39. package/dist/package.cjs +1 -1
  40. package/dist/package.js +6 -0
  41. package/dist/package.js.map +1 -0
  42. package/dist/pages/PrismicPreview.d.ts +36 -0
  43. package/dist/pages/PrismicPreview.js +74 -0
  44. package/dist/pages/PrismicPreview.js.map +1 -0
  45. package/dist/pages/SliceSimulator.d.ts +37 -0
  46. package/dist/pages/SliceSimulator.js +40 -0
  47. package/dist/pages/SliceSimulator.js.map +1 -0
  48. package/dist/pages/enableAutoPreviews.d.ts +37 -0
  49. package/dist/pages/enableAutoPreviews.js +25 -0
  50. package/dist/pages/enableAutoPreviews.js.map +1 -0
  51. package/dist/pages/exitPreview.d.ts +38 -0
  52. package/dist/pages/exitPreview.js +26 -0
  53. package/dist/pages/exitPreview.js.map +1 -0
  54. package/dist/pages/redirectToPreviewURL.d.ts +48 -0
  55. package/dist/pages/redirectToPreviewURL.js +15 -0
  56. package/dist/pages/redirectToPreviewURL.js.map +1 -0
  57. package/dist/pages/setPreviewData.d.ts +26 -0
  58. package/dist/pages/setPreviewData.js +12 -0
  59. package/dist/pages/setPreviewData.js.map +1 -0
  60. package/dist/pages/types.d.ts +36 -0
  61. package/dist/pages.d.ts +12 -0
  62. package/dist/pages.js +12 -0
  63. package/dist/redirectToPreviewURL.d.ts +33 -0
  64. package/dist/redirectToPreviewURL.js +23 -0
  65. package/dist/redirectToPreviewURL.js.map +1 -0
  66. package/dist/types.d.ts +31 -0
  67. package/package.json +9 -13
@@ -0,0 +1,29 @@
1
+ import { cookie } from "@prismicio/client";
2
+
3
+ //#region src/enableAutoPreviews.ts
4
+ /**
5
+ * Configures a Prismic client to automatically query draft content during a
6
+ * preview session.
7
+ *
8
+ * @param config - Configuration for the function.
9
+ */
10
+ function enableAutoPreviews(config) {
11
+ config.client.queryContentFromRef(async () => {
12
+ const { cookies, draftMode } = await import("next/headers");
13
+ let isDraftModeEnabled = false;
14
+ try {
15
+ isDraftModeEnabled = (await draftMode()).isEnabled;
16
+ } catch {
17
+ return;
18
+ }
19
+ if (!isDraftModeEnabled) return;
20
+ const cookie$1 = (await cookies()).get(cookie.preview)?.value;
21
+ if (!cookie$1) return;
22
+ if (!cookie$1.includes("websitePreviewId=")) return;
23
+ return cookie$1;
24
+ });
25
+ }
26
+
27
+ //#endregion
28
+ export { enableAutoPreviews };
29
+ //# sourceMappingURL=enableAutoPreviews.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enableAutoPreviews.js","names":["cookie","prismicCookie"],"sources":["../src/enableAutoPreviews.ts"],"sourcesContent":["import { type Client, cookie as prismicCookie } from \"@prismicio/client\";\n\n/** Configuration for `enableAutoPreviews`. */\nexport type EnableAutoPreviewsConfig = {\n\t/** Prismic client with which automatic previews will be enabled. */\n\t// `Pick` is used to use the smallest possible subset of\n\t// `prismic.Client`. Doing this reduces the surface area for breaking\n\t// type changes.\n\tclient: Pick<Client, \"queryContentFromRef\" | \"enableAutoPreviewsFromReq\">;\n};\n\n/**\n * Configures a Prismic client to automatically query draft content during a\n * preview session.\n *\n * @param config - Configuration for the function.\n */\nexport function enableAutoPreviews(config: EnableAutoPreviewsConfig): void {\n\t// We use a function value so the cookie is checked on every\n\t// request. We don't have a static value to read from.\n\tconfig.client.queryContentFromRef(async () => {\n\t\t// Need this to avoid the following Next.js build-time error:\n\t\t// You're importing a component that needs next/headers. That only works\n\t\t// in a Server Component which is not supported in the pages/ directory.\n\t\tconst { cookies, draftMode } = await import(\"next/headers\");\n\n\t\tlet isDraftModeEnabled = false;\n\t\ttry {\n\t\t\tisDraftModeEnabled = (await draftMode()).isEnabled;\n\t\t} catch {\n\t\t\t// `draftMode()` may have been called in a palce that\n\t\t\t// does not have access to its async storage. This\n\t\t\t// occurs in places like `generateStaticParams()`. We\n\t\t\t// can ignore this case.\n\t\t\treturn;\n\t\t}\n\t\tif (!isDraftModeEnabled) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst cookie = (await cookies()).get(prismicCookie.preview)?.value;\n\t\tif (!cookie) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst isActiveCookie = cookie.includes(\"websitePreviewId=\");\n\t\tif (!isActiveCookie) {\n\t\t\treturn;\n\t\t}\n\n\t\treturn cookie;\n\t});\n}\n"],"mappings":";;;;;;;;;AAiBA,SAAgB,mBAAmB,QAAwC;AAG1E,QAAO,OAAO,oBAAoB,YAAY;EAI7C,MAAM,EAAE,SAAS,cAAc,MAAM,OAAO;EAE5C,IAAI,qBAAqB;AACzB,MAAI;AACH,yBAAsB,MAAM,WAAW,EAAE;UAClC;AAKP;;AAED,MAAI,CAAC,mBACJ;EAGD,MAAMA,YAAU,MAAM,SAAS,EAAE,IAAIC,OAAc,QAAQ,EAAE;AAC7D,MAAI,CAACD,SACJ;AAID,MAAI,CADmBA,SAAO,SAAS,oBAAoB,CAE1D;AAGD,SAAOA;GACN"}
@@ -0,0 +1,21 @@
1
+ //#region src/exitPreview.d.ts
2
+ /**
3
+ * Ends a Prismic preview session within a Next.js app. This function should be
4
+ * used in a Router Handler.
5
+ *
6
+ * @example
7
+ *
8
+ * ```typescript
9
+ * // src/app/api/exit-preview/route.js
10
+ *
11
+ * import { exitPreview } from "@prismicio/next";
12
+ *
13
+ * export async function GET() {
14
+ * return await exitPreview();
15
+ * }
16
+ * ```
17
+ */
18
+ declare function exitPreview(): Promise<Response>;
19
+ //#endregion
20
+ export { exitPreview };
21
+ //# sourceMappingURL=exitPreview.d.ts.map
@@ -0,0 +1,26 @@
1
+ //#region src/exitPreview.ts
2
+ /**
3
+ * Ends a Prismic preview session within a Next.js app. This function should be
4
+ * used in a Router Handler.
5
+ *
6
+ * @example
7
+ *
8
+ * ```typescript
9
+ * // src/app/api/exit-preview/route.js
10
+ *
11
+ * import { exitPreview } from "@prismicio/next";
12
+ *
13
+ * export async function GET() {
14
+ * return await exitPreview();
15
+ * }
16
+ * ```
17
+ */
18
+ async function exitPreview() {
19
+ const { draftMode } = await import("next/headers");
20
+ (await draftMode()).disable();
21
+ return new Response(JSON.stringify({ success: true }), { headers: { "Cache-Control": "no-store" } });
22
+ }
23
+
24
+ //#endregion
25
+ export { exitPreview };
26
+ //# sourceMappingURL=exitPreview.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exitPreview.js","names":[],"sources":["../src/exitPreview.ts"],"sourcesContent":["/**\n * Ends a Prismic preview session within a Next.js app. This function should be\n * used in a Router Handler.\n *\n * @example\n *\n * ```typescript\n * // src/app/api/exit-preview/route.js\n *\n * import { exitPreview } from \"@prismicio/next\";\n *\n * export async function GET() {\n * \treturn await exitPreview();\n * }\n * ```\n */\nexport async function exitPreview(): Promise<Response> {\n\t// Need this to avoid the following Next.js build-time error:\n\t// You're importing a component that needs next/headers. That only works\n\t// in a Server Component which is not supported in the pages/ directory.\n\tconst { draftMode } = await import(\"next/headers\");\n\n\t(await draftMode()).disable();\n\n\t// `Cache-Control` header is used to prevent CDN-level caching.\n\treturn new Response(JSON.stringify({ success: true }), {\n\t\theaders: {\n\t\t\t\"Cache-Control\": \"no-store\",\n\t\t},\n\t});\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAgBA,eAAsB,cAAiC;CAItD,MAAM,EAAE,cAAc,MAAM,OAAO;AAEnC,EAAC,MAAM,WAAW,EAAE,SAAS;AAG7B,QAAO,IAAI,SAAS,KAAK,UAAU,EAAE,SAAS,MAAM,CAAC,EAAE,EACtD,SAAS,EACR,iBAAiB,YACjB,EACD,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { StateEventType, StateEvents } from "@prismicio/simulator/kit";
2
+
3
+ //#region src/getSlices.d.ts
4
+ declare const getSlices: (state: string | null | undefined) => StateEvents[StateEventType.Slices];
5
+ //#endregion
6
+ export { getSlices };
7
+ //# sourceMappingURL=getSlices.d.ts.map
@@ -0,0 +1,11 @@
1
+ import { getDefaultSlices } from "@prismicio/simulator/kit";
2
+ import { decompressFromEncodedURIComponent } from "lz-string";
3
+
4
+ //#region src/getSlices.ts
5
+ const getSlices = (state) => {
6
+ return state ? JSON.parse(decompressFromEncodedURIComponent(state)) : getDefaultSlices();
7
+ };
8
+
9
+ //#endregion
10
+ export { getSlices };
11
+ //# sourceMappingURL=getSlices.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"getSlices.js","names":[],"sources":["../src/getSlices.ts"],"sourcesContent":["import { getDefaultSlices } from \"@prismicio/simulator/kit\";\nimport type { StateEvents, StateEventType } from \"@prismicio/simulator/kit\";\nimport { decompressFromEncodedURIComponent } from \"lz-string\";\n\nexport const getSlices = (\n\tstate: string | null | undefined,\n): StateEvents[StateEventType.Slices] => {\n\treturn state\n\t\t? JSON.parse(decompressFromEncodedURIComponent(state))\n\t\t: getDefaultSlices();\n};\n"],"mappings":";;;;AAIA,MAAa,aACZ,UACwC;AACxC,QAAO,QACJ,KAAK,MAAM,kCAAkC,MAAM,CAAC,GACpD,kBAAkB"}
@@ -0,0 +1,15 @@
1
+ import { ImageLoaderProps } from "next/image";
2
+
3
+ //#region src/imgixLoader.d.ts
4
+
5
+ /**
6
+ * A `next/image` loader for Imgix, which Prismic uses, with an optional
7
+ * collection of default Imgix parameters.
8
+ *
9
+ * @see To learn about `next/image` loaders: https://nextjs.org/docs/api-reference/next/image#loader
10
+ * @see To learn about Imgix's URL API: https://docs.imgix.com/apis/rendering
11
+ */
12
+ declare const imgixLoader: (args: ImageLoaderProps) => string;
13
+ //#endregion
14
+ export { imgixLoader };
15
+ //# sourceMappingURL=imgixLoader.d.ts.map
@@ -0,0 +1,23 @@
1
+ import { buildURL } from "imgix-url-builder";
2
+
3
+ //#region src/imgixLoader.ts
4
+ /**
5
+ * A `next/image` loader for Imgix, which Prismic uses, with an optional
6
+ * collection of default Imgix parameters.
7
+ *
8
+ * @see To learn about `next/image` loaders: https://nextjs.org/docs/api-reference/next/image#loader
9
+ * @see To learn about Imgix's URL API: https://docs.imgix.com/apis/rendering
10
+ */
11
+ const imgixLoader = (args) => {
12
+ const params = {
13
+ fit: new URL(args.src).searchParams.get("fit") || "max",
14
+ w: args.width,
15
+ h: void 0
16
+ };
17
+ if (args.quality) params.q = args.quality;
18
+ return buildURL(args.src, params);
19
+ };
20
+
21
+ //#endregion
22
+ export { imgixLoader };
23
+ //# sourceMappingURL=imgixLoader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"imgixLoader.js","names":[],"sources":["../src/imgixLoader.ts"],"sourcesContent":["import type { ImageLoaderProps } from \"next/image\";\nimport type { ImgixURLParams } from \"imgix-url-builder\";\nimport { buildURL } from \"imgix-url-builder\";\n\n/**\n * A `next/image` loader for Imgix, which Prismic uses, with an optional\n * collection of default Imgix parameters.\n *\n * @see To learn about `next/image` loaders: https://nextjs.org/docs/api-reference/next/image#loader\n * @see To learn about Imgix's URL API: https://docs.imgix.com/apis/rendering\n */\nexport const imgixLoader = (args: ImageLoaderProps): string => {\n\tconst url = new URL(args.src);\n\n\tconst params: ImgixURLParams = {\n\t\tfit: (url.searchParams.get(\"fit\") as ImgixURLParams[\"fit\"]) || \"max\",\n\t\tw: args.width,\n\t\th: undefined,\n\t};\n\n\tif (args.quality) {\n\t\tparams.q = args.quality;\n\t}\n\n\treturn buildURL(args.src, params);\n};\n"],"mappings":";;;;;;;;;;AAWA,MAAa,eAAe,SAAmC;CAG9D,MAAM,SAAyB;EAC9B,KAHW,IAAI,IAAI,KAAK,IAAI,CAGlB,aAAa,IAAI,MAAM,IAA8B;EAC/D,GAAG,KAAK;EACR,GAAG;EACH;AAED,KAAI,KAAK,QACR,QAAO,IAAI,KAAK;AAGjB,QAAO,SAAS,KAAK,KAAK,OAAO"}
@@ -0,0 +1,12 @@
1
+ import { exitPreview } from "./exitPreview.js";
2
+ import { PrismicPreview, PrismicPreviewProps } from "./PrismicPreview.js";
3
+ import { PrismicNextLink, PrismicNextLinkProps } from "./PrismicNextLink.js";
4
+ import { EnableAutoPreviewsConfig, enableAutoPreviews } from "./enableAutoPreviews.js";
5
+ import { CreateClientConfig } from "./types.js";
6
+ import { RedirectToPreviewURLConfig, redirectToPreviewURL } from "./redirectToPreviewURL.js";
7
+ import { PrismicNextImage, PrismicNextImageProps } from "./PrismicNextImage.js";
8
+ import { SliceSimulator, SliceSimulatorParams, SliceSimulatorProps } from "./SliceSimulator.js";
9
+ import { getSlices } from "./getSlices.js";
10
+ import { imgixLoader } from "./imgixLoader.js";
11
+ import { CreateLocaleRedirectConfig, createLocaleRedirect } from "./createLocaleRedirect.js";
12
+ export { type CreateClientConfig, type CreateLocaleRedirectConfig, type EnableAutoPreviewsConfig, PrismicNextImage, type PrismicNextImageProps, PrismicNextLink, type PrismicNextLinkProps, PrismicPreview, type PrismicPreviewProps, type RedirectToPreviewURLConfig, SliceSimulator, type SliceSimulatorParams, type SliceSimulatorProps, createLocaleRedirect, enableAutoPreviews, exitPreview, getSlices, imgixLoader, redirectToPreviewURL };
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ import { exitPreview } from "./exitPreview.js";
2
+ import { PrismicPreview } from "./PrismicPreview.js";
3
+ import { PrismicNextLink } from "./PrismicNextLink.js";
4
+ import { enableAutoPreviews } from "./enableAutoPreviews.js";
5
+ import { redirectToPreviewURL } from "./redirectToPreviewURL.js";
6
+ import { imgixLoader } from "./imgixLoader.js";
7
+ import { PrismicNextImage } from "./PrismicNextImage.js";
8
+ import { getSlices } from "./getSlices.js";
9
+ import { SliceSimulator } from "./SliceSimulator.js";
10
+ import { createLocaleRedirect } from "./createLocaleRedirect.js";
11
+
12
+ export { PrismicNextImage, PrismicNextLink, PrismicPreview, SliceSimulator, createLocaleRedirect, enableAutoPreviews, exitPreview, getSlices, imgixLoader, redirectToPreviewURL };
@@ -0,0 +1,25 @@
1
+ import { version } from "../package.js";
2
+
3
+ //#region src/lib/devMsg.ts
4
+ /**
5
+ * Returns a `prismic.dev/msg` URL for a given message slug.
6
+ *
7
+ * @example
8
+ *
9
+ * ```ts
10
+ * devMsg("missing-param");
11
+ * // => "https://prismic.dev/msg/next/v1.2.3/missing-param"
12
+ * ```
13
+ *
14
+ * @param slug - Slug for the message. This corresponds to a Markdown file in
15
+ * the Git repository's `/messages` directory.
16
+ *
17
+ * @returns The `prismic.dev/msg` URL for the given slug.
18
+ */
19
+ const devMsg = (slug) => {
20
+ return `https://prismic.dev/msg/next/v${version}/${slug}`;
21
+ };
22
+
23
+ //#endregion
24
+ export { devMsg };
25
+ //# sourceMappingURL=devMsg.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"devMsg.js","names":[],"sources":["../../src/lib/devMsg.ts"],"sourcesContent":["import { version } from \"../../package.json\";\n\n/**\n * Returns a `prismic.dev/msg` URL for a given message slug.\n *\n * @example\n *\n * ```ts\n * devMsg(\"missing-param\");\n * // => \"https://prismic.dev/msg/next/v1.2.3/missing-param\"\n * ```\n *\n * @param slug - Slug for the message. This corresponds to a Markdown file in\n * the Git repository's `/messages` directory.\n *\n * @returns The `prismic.dev/msg` URL for the given slug.\n */\nexport const devMsg = (slug: string): string => {\n\treturn `https://prismic.dev/msg/next/v${version}/${slug}`;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiBA,MAAa,UAAU,SAAyB;AAC/C,QAAO,iCAAiC,QAAQ,GAAG"}
@@ -0,0 +1,25 @@
1
+ //#region src/lib/resolveDefaultExport.ts
2
+ /**
3
+ * Resolves a module's default export. The module may provide its default export
4
+ * as a `default` property on an object. This happens when named and default
5
+ * exports are mixed in modules.
6
+ *
7
+ * In ES Modules, mixing is fine since the module resolver can distinguish
8
+ * default and named exports. In transpiled modules, however, all exports are
9
+ * put into a single object, and the default export is provided at a property
10
+ * called `default`.
11
+ *
12
+ * This helper is needed by Next.js uses CJS files with named and default
13
+ * exports.
14
+ *
15
+ * This helper _should_ be a temporary hack until Next.js and Node.js resolve
16
+ * their ESM difficulties. This will likely not be removed any time soon,
17
+ * unfortunately.
18
+ */
19
+ function resolveDefaultExport(mod) {
20
+ return "default" in mod ? mod.default : mod;
21
+ }
22
+
23
+ //#endregion
24
+ export { resolveDefaultExport };
25
+ //# sourceMappingURL=resolveDefaultExport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolveDefaultExport.js","names":[],"sources":["../../src/lib/resolveDefaultExport.ts"],"sourcesContent":["/**\n * Resolves a module's default export. The module may provide its default export\n * as a `default` property on an object. This happens when named and default\n * exports are mixed in modules.\n *\n * In ES Modules, mixing is fine since the module resolver can distinguish\n * default and named exports. In transpiled modules, however, all exports are\n * put into a single object, and the default export is provided at a property\n * called `default`.\n *\n * This helper is needed by Next.js uses CJS files with named and default\n * exports.\n *\n * This helper _should_ be a temporary hack until Next.js and Node.js resolve\n * their ESM difficulties. This will likely not be removed any time soon,\n * unfortunately.\n */\nexport function resolveDefaultExport<T>(mod: T): T {\n\t// eslint-disable-next-line @typescript-eslint/no-explicit-any\n\treturn \"default\" in (mod as any) ? (mod as any).default : mod;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAiBA,SAAgB,qBAAwB,KAAW;AAElD,QAAO,aAAc,MAAe,IAAY,UAAU"}
package/dist/package.cjs CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
  //#region package.json
3
- var version = "2.2.0";
3
+ var version = "2.2.1-canary.0a22932";
4
4
 
5
5
  //#endregion
6
6
  Object.defineProperty(exports, 'version', {
@@ -0,0 +1,6 @@
1
+ //#region package.json
2
+ var version = "2.2.1-canary.0a22932";
3
+
4
+ //#endregion
5
+ export { version };
6
+ //# sourceMappingURL=package.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"package.js","names":[],"sources":["../package.json"],"sourcesContent":[""],"mappings":""}
@@ -0,0 +1,36 @@
1
+ import { FC, ReactNode } from "react";
2
+
3
+ //#region src/pages/PrismicPreview.d.ts
4
+ /** Props for `<PrismicPreview>`. */
5
+ type PrismicPreviewProps = {
6
+ /**
7
+ * The name of your Prismic repository. A Prismic Toolbar will be registered
8
+ * using this repository.
9
+ */
10
+ repositoryName: string;
11
+ /**
12
+ * The URL of your app's Prismic preview endpoint (default: `/api/preview`).
13
+ * This URL will be fetched on preview update events.
14
+ */
15
+ updatePreviewURL?: string;
16
+ /**
17
+ * The URL of your app's exit preview endpoint (default: `/api/exit-preview`).
18
+ * This URL will be fetched on preview exit events.
19
+ */
20
+ exitPreviewURL?: string;
21
+ /** Children to render adjacent to the Prismic Toolbar. */
22
+ children?: ReactNode;
23
+ };
24
+ /**
25
+ * React component that sets up Prismic Previews using the Prismic Toolbar. When
26
+ * the Prismic Toolbar send events to the browser, such as on preview updates
27
+ * and exiting, this component will automatically refresh the page with the
28
+ * changes.
29
+ *
30
+ * This component can be wrapped around your app or added anywhere in your app's
31
+ * tree. It must be rendered on every page.
32
+ */
33
+ declare const PrismicPreview: FC<PrismicPreviewProps>;
34
+ //#endregion
35
+ export { PrismicPreview, PrismicPreviewProps };
36
+ //# sourceMappingURL=PrismicPreview.d.ts.map
@@ -0,0 +1,74 @@
1
+ import Script from "next/script";
2
+ import { cookie, getToolbarSrc } from "@prismicio/client";
3
+ import { useEffect } from "react";
4
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
5
+ import { useRouter } from "next/router";
6
+
7
+ //#region src/pages/PrismicPreview.tsx
8
+ /**
9
+ * React component that sets up Prismic Previews using the Prismic Toolbar. When
10
+ * the Prismic Toolbar send events to the browser, such as on preview updates
11
+ * and exiting, this component will automatically refresh the page with the
12
+ * changes.
13
+ *
14
+ * This component can be wrapped around your app or added anywhere in your app's
15
+ * tree. It must be rendered on every page.
16
+ */
17
+ const PrismicPreview = (props) => {
18
+ const { repositoryName, updatePreviewURL = "/api/preview", exitPreviewURL = "/api/exit-preview", children } = props;
19
+ const router = useRouter();
20
+ const toolbarSrc = getToolbarSrc(repositoryName);
21
+ useEffect(() => {
22
+ const controller = new AbortController();
23
+ window.addEventListener("prismicPreviewUpdate", onUpdate, { signal: controller.signal });
24
+ window.addEventListener("prismicPreviewEnd", onEnd, { signal: controller.signal });
25
+ if (window.location.href.startsWith(window.location.origin + router.basePath) && getPreviewCookieRepositoryName() === repositoryName && !router.isPreview) start();
26
+ function onEnd(event) {
27
+ event.preventDefault();
28
+ fetch(router.basePath + exitPreviewURL, { signal: controller.signal }).then((res) => {
29
+ if (!res.ok) {
30
+ console.error(`[<PrismicPreview>] Failed to exit Preview Mode using the "${exitPreviewURL}" API endpoint. Does it exist?`);
31
+ return;
32
+ }
33
+ refresh();
34
+ }).catch(() => {});
35
+ }
36
+ function onUpdate(event) {
37
+ event.preventDefault();
38
+ start();
39
+ }
40
+ function start() {
41
+ fetch(router.basePath + updatePreviewURL, {
42
+ redirect: "manual",
43
+ signal: controller.signal
44
+ }).then((res) => {
45
+ if (res.type !== "opaqueredirect") {
46
+ console.error(`[<PrismicPreview>] Failed to start or update the preview using "${updatePreviewURL}". Does it exist?`);
47
+ return;
48
+ }
49
+ refresh();
50
+ }).catch(() => {});
51
+ }
52
+ function refresh() {
53
+ router.replace(router.asPath, void 0, { scroll: false });
54
+ }
55
+ return () => controller.abort();
56
+ }, [
57
+ exitPreviewURL,
58
+ updatePreviewURL,
59
+ repositoryName,
60
+ router
61
+ ]);
62
+ return /* @__PURE__ */ jsxs(Fragment, { children: [children, /* @__PURE__ */ jsx(Script, {
63
+ src: toolbarSrc,
64
+ strategy: "lazyOnload"
65
+ })] });
66
+ };
67
+ function getPreviewCookieRepositoryName() {
68
+ const cookie$1 = window.document.cookie.split("; ").find((row) => row.startsWith(`${cookie.preview}=`))?.split("=")[1];
69
+ return (decodeURIComponent(cookie$1 ?? "").match(/"([^"]+)\.prismic\.io"/) || [])[1];
70
+ }
71
+
72
+ //#endregion
73
+ export { PrismicPreview };
74
+ //# sourceMappingURL=PrismicPreview.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"PrismicPreview.js","names":["cookie","prismicCookie"],"sources":["../../src/pages/PrismicPreview.tsx"],"sourcesContent":["import { getToolbarSrc, cookie as prismicCookie } from \"@prismicio/client\";\nimport { useRouter } from \"next/router\";\nimport Script from \"next/script\";\nimport type { FC } from \"react\";\nimport { type ReactNode, useEffect } from \"react\";\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 const PrismicPreview: FC<PrismicPreviewProps> = (props) => {\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"],"mappings":";;;;;;;;;;;;;;;;AAuCA,MAAa,kBAA2C,UAAU;CACjE,MAAM,EACL,gBACA,mBAAmB,gBACnB,iBAAiB,qBACjB,aACG;CAEJ,MAAM,SAAS,WAAW;CAE1B,MAAM,aAAa,cAAc,eAAe;AAEhD,iBAAgB;EACf,MAAM,aAAa,IAAI,iBAAiB;AAExC,SAAO,iBAAiB,wBAAwB,UAAU,EACzD,QAAQ,WAAW,QACnB,CAAC;AACF,SAAO,iBAAiB,qBAAqB,OAAO,EACnD,QAAQ,WAAW,QACnB,CAAC;AAQF,MACC,OAAO,SAAS,KAAK,WACpB,OAAO,SAAS,SAAS,OAAO,SAChC,IACD,gCAAgC,KAAK,kBACrC,CAAC,OAAO,UAER,QAAO;EAGR,SAAS,MAAM,OAAc;AAC5B,SAAM,gBAAgB;AACtB,SAAM,OAAO,WAAW,gBAAgB,EAAE,QAAQ,WAAW,QAAQ,CAAC,CACpE,MAAM,QAAQ;AACd,QAAI,CAAC,IAAI,IAAI;AACZ,aAAQ,MACP,6DAA6D,eAAe,gCAC5E;AAED;;AAGD,aAAS;KACR,CACD,YAAY,GAAG;;EAGlB,SAAS,SAAS,OAAc;AAC/B,SAAM,gBAAgB;AACtB,UAAO;;EAGR,SAAS,QAAQ;AAKhB,SAAM,OAAO,WAAW,kBAAkB;IACzC,UAAU;IACV,QAAQ,WAAW;IACnB,CAAC,CACA,MAAM,QAAQ;AACd,QAAI,IAAI,SAAS,kBAAkB;AAClC,aAAQ,MACP,mEAAmE,iBAAiB,mBACpF;AAED;;AAGD,aAAS;KACR,CACD,YAAY,GAAG;;EAGlB,SAAS,UAAU;AAClB,UAAO,QAAQ,OAAO,QAAQ,QAAW,EAAE,QAAQ,OAAO,CAAC;;AAG5D,eAAa,WAAW,OAAO;IAC7B;EAAC;EAAgB;EAAkB;EAAgB;EAAO,CAAC;AAE9D,QACC,4CACE,UACD,oBAAC;EAAO,KAAK;EAAY,UAAS;GAAe,IAC/C;;AAIL,SAAS,iCAAiC;CACzC,MAAMA,WAAS,OAAO,SAAS,OAC7B,MAAM,KAAK,CACX,MAAM,QAAQ,IAAI,WAAW,GAAGC,OAAc,QAAQ,GAAG,CAAC,EACzD,MAAM,IAAI,CAAC;AAEd,SAAQ,mBAAmBD,YAAU,GAAG,CAAC,MAAM,yBAAyB,IACvE,EAAE,EAAE"}
@@ -0,0 +1,37 @@
1
+ import { ComponentType, FC } from "react";
2
+ import { SliceSimulatorProps, SliceSimulatorState } from "@prismicio/simulator/kit";
3
+
4
+ //#region src/pages/SliceSimulator.d.ts
5
+ type SliceSimulatorSliceZoneProps = {
6
+ slices: SliceSimulatorState["slices"];
7
+ };
8
+ type SliceSimulatorProps$1 = SliceSimulatorProps & {
9
+ /**
10
+ * React component to render simulated Slices.
11
+ *
12
+ * @example
13
+ *
14
+ * ```tsx
15
+ * import { SliceSimulator } from "@slicemachine/adapter-next/simulator";
16
+ * import { SliceZone } from "@prismicio/react";
17
+ *
18
+ * import { components } from "../slices";
19
+ *
20
+ * <SliceSimulator
21
+ * sliceZone={({ slices }) => (
22
+ * <SliceZone slices={slices} components={components} />
23
+ * )}
24
+ * />;
25
+ * ```
26
+ */
27
+ sliceZone: ComponentType<SliceSimulatorSliceZoneProps>;
28
+ className?: string;
29
+ };
30
+ /**
31
+ * Simulate slices in isolation. The slice simulator enables live slice
32
+ * development in Slice Machine and live previews in the Page Builder.
33
+ */
34
+ declare const SliceSimulator: FC<SliceSimulatorProps$1>;
35
+ //#endregion
36
+ export { SliceSimulator, SliceSimulatorProps$1 as SliceSimulatorProps, SliceSimulatorSliceZoneProps };
37
+ //# sourceMappingURL=SliceSimulator.d.ts.map
@@ -0,0 +1,40 @@
1
+ import { SliceSimulatorWrapper } from "../SliceSimulatorWrapper.js";
2
+ import { useEffect, useState } from "react";
3
+ import { jsx } from "react/jsx-runtime";
4
+ import { SimulatorManager, StateEventType, getDefaultMessage, getDefaultSlices } from "@prismicio/simulator/kit";
5
+
6
+ //#region src/pages/SliceSimulator.tsx
7
+ const simulatorManager = new SimulatorManager();
8
+ /**
9
+ * Simulate slices in isolation. The slice simulator enables live slice
10
+ * development in Slice Machine and live previews in the Page Builder.
11
+ */
12
+ const SliceSimulator = ({ background, zIndex, className, sliceZone: SliceZoneComp }) => {
13
+ const [slices, setSlices] = useState(() => getDefaultSlices());
14
+ const [message, setMessage] = useState(() => getDefaultMessage());
15
+ useEffect(() => {
16
+ simulatorManager.state.on(StateEventType.Slices, (_slices) => {
17
+ setSlices(_slices);
18
+ }, "simulator-slices");
19
+ simulatorManager.state.on(StateEventType.Message, (_message) => {
20
+ setMessage(_message);
21
+ }, "simulator-message");
22
+ simulatorManager.init();
23
+ return () => {
24
+ simulatorManager.state.off(StateEventType.Slices, "simulator-slices");
25
+ simulatorManager.state.off(StateEventType.Message, "simulator-message");
26
+ };
27
+ }, []);
28
+ return /* @__PURE__ */ jsx(SliceSimulatorWrapper, {
29
+ message,
30
+ hasSlices: slices.length > 0,
31
+ background,
32
+ zIndex,
33
+ className,
34
+ children: /* @__PURE__ */ jsx(SliceZoneComp, { slices })
35
+ });
36
+ };
37
+
38
+ //#endregion
39
+ export { SliceSimulator };
40
+ //# sourceMappingURL=SliceSimulator.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SliceSimulator.js","names":[],"sources":["../../src/pages/SliceSimulator.tsx"],"sourcesContent":["import { SliceSimulatorWrapper } from \"../SliceSimulatorWrapper\";\nimport {\n\tSimulatorManager,\n\tStateEventType,\n\tgetDefaultMessage,\n\tgetDefaultSlices,\n} from \"@prismicio/simulator/kit\";\nimport type {\n\tSliceSimulatorProps as BaseSliceSimulatorProps,\n\tSliceSimulatorState,\n} from \"@prismicio/simulator/kit\";\nimport { useEffect, useState } from \"react\";\nimport type { ComponentType, FC } from \"react\";\n\nconst simulatorManager = new SimulatorManager();\n\nexport type SliceSimulatorSliceZoneProps = {\n\tslices: SliceSimulatorState[\"slices\"];\n};\n\nexport type SliceSimulatorProps = BaseSliceSimulatorProps & {\n\t/**\n\t * React component to render simulated Slices.\n\t *\n\t * @example\n\t *\n\t * ```tsx\n\t * import { SliceSimulator } from \"@slicemachine/adapter-next/simulator\";\n\t * import { SliceZone } from \"@prismicio/react\";\n\t *\n\t * import { components } from \"../slices\";\n\t *\n\t * <SliceSimulator\n\t * \tsliceZone={({ slices }) => (\n\t * \t\t<SliceZone slices={slices} components={components} />\n\t * \t)}\n\t * />;\n\t * ```\n\t */\n\tsliceZone: ComponentType<SliceSimulatorSliceZoneProps>;\n\tclassName?: string;\n};\n\n/**\n * Simulate slices in isolation. The slice simulator enables live slice\n * development in Slice Machine and live previews in the Page Builder.\n */\nexport const SliceSimulator: FC<SliceSimulatorProps> = ({\n\tbackground,\n\tzIndex,\n\tclassName,\n\tsliceZone: SliceZoneComp,\n}) => {\n\tconst [slices, setSlices] = useState(() => getDefaultSlices());\n\tconst [message, setMessage] = useState(() => getDefaultMessage());\n\n\tuseEffect(() => {\n\t\tsimulatorManager.state.on(\n\t\t\tStateEventType.Slices,\n\t\t\t(_slices) => {\n\t\t\t\tsetSlices(_slices);\n\t\t\t},\n\t\t\t\"simulator-slices\",\n\t\t);\n\t\tsimulatorManager.state.on(\n\t\t\tStateEventType.Message,\n\t\t\t(_message) => {\n\t\t\t\tsetMessage(_message);\n\t\t\t},\n\t\t\t\"simulator-message\",\n\t\t);\n\n\t\tsimulatorManager.init();\n\n\t\treturn () => {\n\t\t\tsimulatorManager.state.off(StateEventType.Slices, \"simulator-slices\");\n\n\t\t\tsimulatorManager.state.off(StateEventType.Message, \"simulator-message\");\n\t\t};\n\t}, []);\n\n\treturn (\n\t\t<SliceSimulatorWrapper\n\t\t\tmessage={message}\n\t\t\thasSlices={slices.length > 0}\n\t\t\tbackground={background}\n\t\t\tzIndex={zIndex}\n\t\t\tclassName={className}\n\t\t>\n\t\t\t<SliceZoneComp slices={slices} />\n\t\t</SliceSimulatorWrapper>\n\t);\n};\n"],"mappings":";;;;;;AAcA,MAAM,mBAAmB,IAAI,kBAAkB;;;;;AAiC/C,MAAa,kBAA2C,EACvD,YACA,QACA,WACA,WAAW,oBACN;CACL,MAAM,CAAC,QAAQ,aAAa,eAAe,kBAAkB,CAAC;CAC9D,MAAM,CAAC,SAAS,cAAc,eAAe,mBAAmB,CAAC;AAEjE,iBAAgB;AACf,mBAAiB,MAAM,GACtB,eAAe,SACd,YAAY;AACZ,aAAU,QAAQ;KAEnB,mBACA;AACD,mBAAiB,MAAM,GACtB,eAAe,UACd,aAAa;AACb,cAAW,SAAS;KAErB,oBACA;AAED,mBAAiB,MAAM;AAEvB,eAAa;AACZ,oBAAiB,MAAM,IAAI,eAAe,QAAQ,mBAAmB;AAErE,oBAAiB,MAAM,IAAI,eAAe,SAAS,oBAAoB;;IAEtE,EAAE,CAAC;AAEN,QACC,oBAAC;EACS;EACT,WAAW,OAAO,SAAS;EACf;EACJ;EACG;YAEX,oBAAC,iBAAsB,SAAU;GACV"}
@@ -0,0 +1,37 @@
1
+ import { NextApiRequestLike } from "./types.js";
2
+ import { Client } from "@prismicio/client";
3
+ import { PreviewData } from "next";
4
+
5
+ //#region src/pages/enableAutoPreviews.d.ts
6
+
7
+ /**
8
+ * Configuration for `enableAutoPreviews`.
9
+ *
10
+ * @typeParam TPreviewData - Next.js preview data object.
11
+ */
12
+ type EnableAutoPreviewsConfig = {
13
+ /** Prismic client with which automatic previews will be enabled. */
14
+ client: Pick<Client, "queryContentFromRef" | "enableAutoPreviewsFromReq">;
15
+ /**
16
+ * The `previewData` object provided in the `getStaticProps()` or
17
+ * `getServerSideProps()` context object.
18
+ */
19
+ previewData?: PreviewData;
20
+ /**
21
+ * The `req` object from a Next.js API route.
22
+ *
23
+ * @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
24
+ */
25
+ req?: NextApiRequestLike;
26
+ };
27
+ /**
28
+ * Configures a Prismic client to automatically query draft content during a
29
+ * preview session. It either takes in a Next.js `getStaticProps` context object
30
+ * or a Next.js API endpoint request object.
31
+ *
32
+ * @param config - Configuration for the function.
33
+ */
34
+ declare function enableAutoPreviews(config: EnableAutoPreviewsConfig): void;
35
+ //#endregion
36
+ export { EnableAutoPreviewsConfig, enableAutoPreviews };
37
+ //# sourceMappingURL=enableAutoPreviews.d.ts.map
@@ -0,0 +1,25 @@
1
+ //#region src/pages/enableAutoPreviews.ts
2
+ /**
3
+ * Configures a Prismic client to automatically query draft content during a
4
+ * preview session. It either takes in a Next.js `getStaticProps` context object
5
+ * or a Next.js API endpoint request object.
6
+ *
7
+ * @param config - Configuration for the function.
8
+ */
9
+ function enableAutoPreviews(config) {
10
+ if ("previewData" in config && config.previewData) {
11
+ if (isPrismicPreviewData(config.previewData)) config.client.queryContentFromRef(config.previewData.ref);
12
+ return;
13
+ }
14
+ if ("req" in config && config.req) {
15
+ config.client.enableAutoPreviewsFromReq(config.req);
16
+ return;
17
+ }
18
+ }
19
+ function isPrismicPreviewData(input) {
20
+ return typeof input === "object" && input !== null && "ref" in input;
21
+ }
22
+
23
+ //#endregion
24
+ export { enableAutoPreviews };
25
+ //# sourceMappingURL=enableAutoPreviews.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"enableAutoPreviews.js","names":[],"sources":["../../src/pages/enableAutoPreviews.ts"],"sourcesContent":["import type { PreviewData } from \"next\";\nimport type { Client } from \"@prismicio/client\";\n\nimport type { NextApiRequestLike } from \"./types\";\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"],"mappings":";;;;;;;;AAsCA,SAAgB,mBAAmB,QAAwC;AAC1E,KAAI,iBAAiB,UAAU,OAAO,aAAa;AAIlD,MAAI,qBAAqB,OAAO,YAAY,CAC3C,QAAO,OAAO,oBAAoB,OAAO,YAAY,IAAI;AAG1D;;AAGD,KAAI,SAAS,UAAU,OAAO,KAAK;AAGlC,SAAO,OAAO,0BAA0B,OAAO,IAAI;AAEnD;;;AAIF,SAAS,qBAAqB,OAA0C;AACvE,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,SAAS"}
@@ -0,0 +1,38 @@
1
+ import { NextApiRequestLike, NextApiResponseLike } from "./types.js";
2
+
3
+ //#region src/pages/exitPreview.d.ts
4
+ /** Configuration for `exitPreview()`. */
5
+ type ExitPreviewAPIRouteConfig = {
6
+ /**
7
+ * The `req` object from a Next.js API route.
8
+ *
9
+ * @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
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/api-routes/introduction\>
16
+ */
17
+ res: NextApiResponseLike;
18
+ };
19
+ /**
20
+ * Ends a Prismic preview session within a Next.js app. This function should be
21
+ * used in an API route.
22
+ *
23
+ * @example
24
+ *
25
+ * ```typescript
26
+ * // src/pages/api/exit-preview.js
27
+ *
28
+ * import { exitPreview } from "@prismicio/next";
29
+ *
30
+ * export default function handler(_req, res) {
31
+ * return exitPreview({ res });
32
+ * }
33
+ * ```
34
+ */
35
+ declare function exitPreview(config: ExitPreviewAPIRouteConfig): void;
36
+ //#endregion
37
+ export { ExitPreviewAPIRouteConfig, exitPreview };
38
+ //# sourceMappingURL=exitPreview.d.ts.map
@@ -0,0 +1,26 @@
1
+ //#region src/pages/exitPreview.ts
2
+ /**
3
+ * Ends a Prismic preview session within a Next.js app. This function should be
4
+ * used in an API route.
5
+ *
6
+ * @example
7
+ *
8
+ * ```typescript
9
+ * // src/pages/api/exit-preview.js
10
+ *
11
+ * import { exitPreview } from "@prismicio/next";
12
+ *
13
+ * export default function handler(_req, res) {
14
+ * return exitPreview({ res });
15
+ * }
16
+ * ```
17
+ */
18
+ function exitPreview(config) {
19
+ config.res.clearPreviewData();
20
+ config.res.setHeader("Cache-Control", "no-store");
21
+ config.res.json({ success: true });
22
+ }
23
+
24
+ //#endregion
25
+ export { exitPreview };
26
+ //# sourceMappingURL=exitPreview.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exitPreview.js","names":[],"sources":["../../src/pages/exitPreview.ts"],"sourcesContent":["import type { NextApiRequestLike, NextApiResponseLike } from \"./types\";\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"],"mappings":";;;;;;;;;;;;;;;;;AAmCA,SAAgB,YAAY,QAAyC;AACpE,QAAO,IAAI,kBAAkB;AAG7B,QAAO,IAAI,UAAU,iBAAiB,WAAW;AAEjD,QAAO,IAAI,KAAK,EAAE,SAAS,MAAM,CAAC"}