@prismicio/next 2.2.3 → 2.2.4-pr.114.39a5b55

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 (199) hide show
  1. package/dist/PrismicNextImage.cjs +9 -11
  2. package/dist/PrismicNextImage.cjs.map +1 -1
  3. package/dist/PrismicNextImage.d.cts +13 -21
  4. package/dist/PrismicNextImage.d.cts.map +1 -0
  5. package/dist/PrismicNextImage.d.ts +13 -21
  6. package/dist/PrismicNextImage.d.ts.map +1 -0
  7. package/dist/PrismicNextImage.js +9 -11
  8. package/dist/PrismicNextImage.js.map +1 -1
  9. package/dist/PrismicNextLink.cjs.map +1 -1
  10. package/dist/PrismicNextLink.d.cts.map +1 -0
  11. package/dist/PrismicNextLink.d.ts.map +1 -0
  12. package/dist/PrismicNextLink.js.map +1 -1
  13. package/dist/PrismicPreview.cjs +6 -7
  14. package/dist/PrismicPreview.cjs.map +1 -1
  15. package/dist/PrismicPreview.d.cts +11 -12
  16. package/dist/PrismicPreview.d.cts.map +1 -0
  17. package/dist/PrismicPreview.d.ts +11 -12
  18. package/dist/PrismicPreview.d.ts.map +1 -0
  19. package/dist/PrismicPreview.js +6 -7
  20. package/dist/PrismicPreview.js.map +1 -1
  21. package/dist/PrismicPreviewClient.cjs +1 -3
  22. package/dist/PrismicPreviewClient.cjs.map +1 -1
  23. package/dist/PrismicPreviewClient.js +1 -3
  24. package/dist/PrismicPreviewClient.js.map +1 -1
  25. package/dist/SliceSimulator.cjs +3 -3
  26. package/dist/SliceSimulator.cjs.map +1 -1
  27. package/dist/SliceSimulator.d.cts +3 -5
  28. package/dist/SliceSimulator.d.cts.map +1 -0
  29. package/dist/SliceSimulator.d.ts +3 -5
  30. package/dist/SliceSimulator.d.ts.map +1 -0
  31. package/dist/SliceSimulator.js +3 -3
  32. package/dist/SliceSimulator.js.map +1 -1
  33. package/dist/SliceSimulatorWrapper.cjs +1 -4
  34. package/dist/SliceSimulatorWrapper.cjs.map +1 -1
  35. package/dist/SliceSimulatorWrapper.js +1 -4
  36. package/dist/SliceSimulatorWrapper.js.map +1 -1
  37. package/dist/cacheTagPrismicPages.cjs +11 -0
  38. package/dist/cacheTagPrismicPages.cjs.map +1 -0
  39. package/dist/cacheTagPrismicPages.d.cts +7 -0
  40. package/dist/cacheTagPrismicPages.d.cts.map +1 -0
  41. package/dist/cacheTagPrismicPages.d.ts +7 -0
  42. package/dist/cacheTagPrismicPages.d.ts.map +1 -0
  43. package/dist/cacheTagPrismicPages.js +10 -0
  44. package/dist/cacheTagPrismicPages.js.map +1 -0
  45. package/dist/createLocaleRedirect.cjs.map +1 -1
  46. package/dist/createLocaleRedirect.d.cts.map +1 -0
  47. package/dist/createLocaleRedirect.d.ts.map +1 -0
  48. package/dist/createLocaleRedirect.js.map +1 -1
  49. package/dist/enableAutoPreviews.cjs +3 -18
  50. package/dist/enableAutoPreviews.cjs.map +1 -1
  51. package/dist/enableAutoPreviews.d.cts +1 -2
  52. package/dist/enableAutoPreviews.d.cts.map +1 -0
  53. package/dist/enableAutoPreviews.d.ts +1 -2
  54. package/dist/enableAutoPreviews.d.ts.map +1 -0
  55. package/dist/enableAutoPreviews.js +3 -17
  56. package/dist/enableAutoPreviews.js.map +1 -1
  57. package/dist/exitPreview.cjs +13 -11
  58. package/dist/exitPreview.cjs.map +1 -1
  59. package/dist/exitPreview.d.cts +9 -10
  60. package/dist/exitPreview.d.cts.map +1 -0
  61. package/dist/exitPreview.d.ts +9 -10
  62. package/dist/exitPreview.d.ts.map +1 -0
  63. package/dist/exitPreview.js +12 -11
  64. package/dist/exitPreview.js.map +1 -1
  65. package/dist/getPreviewRef.cjs +39 -0
  66. package/dist/getPreviewRef.cjs.map +1 -0
  67. package/dist/getPreviewRef.d.cts +23 -0
  68. package/dist/getPreviewRef.d.cts.map +1 -0
  69. package/dist/getPreviewRef.d.ts +23 -0
  70. package/dist/getPreviewRef.d.ts.map +1 -0
  71. package/dist/getPreviewRef.js +38 -0
  72. package/dist/getPreviewRef.js.map +1 -0
  73. package/dist/getPrismicCacheTags.cjs +29 -0
  74. package/dist/getPrismicCacheTags.cjs.map +1 -0
  75. package/dist/getPrismicCacheTags.d.cts +14 -0
  76. package/dist/getPrismicCacheTags.d.cts.map +1 -0
  77. package/dist/getPrismicCacheTags.d.ts +14 -0
  78. package/dist/getPrismicCacheTags.d.ts.map +1 -0
  79. package/dist/getPrismicCacheTags.js +28 -0
  80. package/dist/getPrismicCacheTags.js.map +1 -0
  81. package/dist/getSlices.cjs.map +1 -1
  82. package/dist/getSlices.d.cts.map +1 -0
  83. package/dist/getSlices.d.ts.map +1 -0
  84. package/dist/getSlices.js.map +1 -1
  85. package/dist/imgixLoader.cjs +2 -2
  86. package/dist/imgixLoader.cjs.map +1 -1
  87. package/dist/imgixLoader.d.cts +2 -2
  88. package/dist/imgixLoader.d.cts.map +1 -0
  89. package/dist/imgixLoader.d.ts +2 -2
  90. package/dist/imgixLoader.d.ts.map +1 -0
  91. package/dist/imgixLoader.js +2 -2
  92. package/dist/imgixLoader.js.map +1 -1
  93. package/dist/index.cjs +8 -0
  94. package/dist/index.d.cts +5 -1
  95. package/dist/index.d.ts +5 -1
  96. package/dist/index.js +5 -1
  97. package/dist/lib/devMsg.cjs +6 -8
  98. package/dist/lib/devMsg.cjs.map +1 -1
  99. package/dist/lib/devMsg.js +6 -8
  100. package/dist/lib/devMsg.js.map +1 -1
  101. package/dist/lib/resolveDefaultExport.cjs +8 -12
  102. package/dist/lib/resolveDefaultExport.cjs.map +1 -1
  103. package/dist/lib/resolveDefaultExport.js +8 -12
  104. package/dist/lib/resolveDefaultExport.js.map +1 -1
  105. package/dist/package.cjs +1 -1
  106. package/dist/package.js +1 -1
  107. package/dist/pages/PrismicPreview.cjs +6 -7
  108. package/dist/pages/PrismicPreview.cjs.map +1 -1
  109. package/dist/pages/PrismicPreview.d.cts +11 -12
  110. package/dist/pages/PrismicPreview.d.cts.map +1 -0
  111. package/dist/pages/PrismicPreview.d.ts +11 -12
  112. package/dist/pages/PrismicPreview.d.ts.map +1 -0
  113. package/dist/pages/PrismicPreview.js +6 -7
  114. package/dist/pages/PrismicPreview.js.map +1 -1
  115. package/dist/pages/SliceSimulator.cjs +2 -2
  116. package/dist/pages/SliceSimulator.cjs.map +1 -1
  117. package/dist/pages/SliceSimulator.d.cts +10 -13
  118. package/dist/pages/SliceSimulator.d.cts.map +1 -0
  119. package/dist/pages/SliceSimulator.d.ts +10 -13
  120. package/dist/pages/SliceSimulator.d.ts.map +1 -0
  121. package/dist/pages/SliceSimulator.js +2 -2
  122. package/dist/pages/SliceSimulator.js.map +1 -1
  123. package/dist/pages/enableAutoPreviews.cjs +3 -3
  124. package/dist/pages/enableAutoPreviews.cjs.map +1 -1
  125. package/dist/pages/enableAutoPreviews.d.cts +5 -5
  126. package/dist/pages/enableAutoPreviews.d.cts.map +1 -0
  127. package/dist/pages/enableAutoPreviews.d.ts +5 -5
  128. package/dist/pages/enableAutoPreviews.d.ts.map +1 -0
  129. package/dist/pages/enableAutoPreviews.js +3 -3
  130. package/dist/pages/enableAutoPreviews.js.map +1 -1
  131. package/dist/pages/exitPreview.cjs +9 -10
  132. package/dist/pages/exitPreview.cjs.map +1 -1
  133. package/dist/pages/exitPreview.d.cts +9 -10
  134. package/dist/pages/exitPreview.d.cts.map +1 -0
  135. package/dist/pages/exitPreview.d.ts +9 -10
  136. package/dist/pages/exitPreview.d.ts.map +1 -0
  137. package/dist/pages/exitPreview.js +9 -10
  138. package/dist/pages/exitPreview.js.map +1 -1
  139. package/dist/pages/redirectToPreviewURL.cjs.map +1 -1
  140. package/dist/pages/redirectToPreviewURL.d.cts +7 -9
  141. package/dist/pages/redirectToPreviewURL.d.cts.map +1 -0
  142. package/dist/pages/redirectToPreviewURL.d.ts +7 -9
  143. package/dist/pages/redirectToPreviewURL.d.ts.map +1 -0
  144. package/dist/pages/redirectToPreviewURL.js.map +1 -1
  145. package/dist/pages/setPreviewData.cjs.map +1 -1
  146. package/dist/pages/setPreviewData.d.cts.map +1 -0
  147. package/dist/pages/setPreviewData.d.ts.map +1 -0
  148. package/dist/pages/setPreviewData.js.map +1 -1
  149. package/dist/pages/types.d.cts +3 -8
  150. package/dist/pages/types.d.cts.map +1 -0
  151. package/dist/pages/types.d.ts +3 -8
  152. package/dist/pages/types.d.ts.map +1 -0
  153. package/dist/redirectToPreviewURL.cjs.map +1 -1
  154. package/dist/redirectToPreviewURL.d.cts +3 -5
  155. package/dist/redirectToPreviewURL.d.cts.map +1 -0
  156. package/dist/redirectToPreviewURL.d.ts +3 -5
  157. package/dist/redirectToPreviewURL.d.ts.map +1 -0
  158. package/dist/redirectToPreviewURL.js.map +1 -1
  159. package/dist/revalidatePrismicPages.cjs +18 -0
  160. package/dist/revalidatePrismicPages.cjs.map +1 -0
  161. package/dist/revalidatePrismicPages.d.cts +12 -0
  162. package/dist/revalidatePrismicPages.d.cts.map +1 -0
  163. package/dist/revalidatePrismicPages.d.ts +12 -0
  164. package/dist/revalidatePrismicPages.d.ts.map +1 -0
  165. package/dist/revalidatePrismicPages.js +17 -0
  166. package/dist/revalidatePrismicPages.js.map +1 -0
  167. package/dist/types.d.cts +1 -2
  168. package/dist/types.d.cts.map +1 -0
  169. package/dist/types.d.ts +1 -2
  170. package/dist/types.d.ts.map +1 -0
  171. package/package.json +32 -34
  172. package/src/PrismicNextImage.tsx +113 -134
  173. package/src/PrismicNextLink.tsx +43 -45
  174. package/src/PrismicPreview.tsx +28 -29
  175. package/src/PrismicPreviewClient.tsx +42 -44
  176. package/src/SliceSimulator.tsx +38 -43
  177. package/src/SliceSimulatorWrapper.tsx +15 -25
  178. package/src/cacheTagPrismicPages.ts +8 -0
  179. package/src/createLocaleRedirect.ts +3 -3
  180. package/src/enableAutoPreviews.ts +7 -37
  181. package/src/exitPreview.ts +18 -13
  182. package/src/getPreviewRef.ts +52 -0
  183. package/src/getPrismicCacheTags.ts +44 -0
  184. package/src/getSlices.ts +6 -10
  185. package/src/imgixLoader.ts +10 -10
  186. package/src/index.ts +26 -21
  187. package/src/lib/devMsg.ts +9 -11
  188. package/src/lib/resolveDefaultExport.ts +9 -13
  189. package/src/pages/PrismicPreview.tsx +47 -51
  190. package/src/pages/SliceSimulator.tsx +35 -37
  191. package/src/pages/enableAutoPreviews.ts +17 -17
  192. package/src/pages/exitPreview.ts +17 -18
  193. package/src/pages/index.ts +20 -23
  194. package/src/pages/redirectToPreviewURL.ts +22 -33
  195. package/src/pages/setPreviewData.ts +7 -7
  196. package/src/pages/types.ts +16 -20
  197. package/src/redirectToPreviewURL.ts +21 -30
  198. package/src/revalidatePrismicPages.ts +17 -0
  199. package/src/types.ts +12 -13
@@ -1,39 +1,34 @@
1
- "use client";
1
+ "use client"
2
2
 
3
- import { SliceSimulatorWrapper } from "./SliceSimulatorWrapper";
4
- import { getSlices } from "./getSlices";
5
- import {
6
- SimulatorManager,
7
- StateEventType,
8
- getDefaultMessage,
9
- } from "@prismicio/simulator/kit";
10
- import type { SliceSimulatorProps as BaseSliceSimulatorProps } from "@prismicio/simulator/kit";
11
- import { compressToEncodedURIComponent } from "lz-string";
12
- import { useRouter } from "next/navigation";
13
- import { useEffect, useState } from "react";
14
- import type { FC, ReactNode } from "react";
3
+ import { SimulatorManager, StateEventType, getDefaultMessage } from "@prismicio/simulator/kit"
4
+ import type { SliceSimulatorProps as BaseSliceSimulatorProps } from "@prismicio/simulator/kit"
5
+ import { compressToEncodedURIComponent } from "lz-string"
6
+ import { useRouter } from "next/navigation"
7
+ import { useEffect, useState } from "react"
8
+ import type { FC, ReactNode } from "react"
15
9
 
16
- const STATE_PARAMS_KEY = "state";
10
+ import { getSlices } from "./getSlices"
11
+ import { SliceSimulatorWrapper } from "./SliceSimulatorWrapper"
17
12
 
18
- const simulatorManager = new SimulatorManager();
13
+ const STATE_PARAMS_KEY = "state"
19
14
 
20
- /**
21
- * Parameters provided to the Slice Simulator page.
22
- */
15
+ const simulatorManager = new SimulatorManager()
16
+
17
+ /** Parameters provided to the Slice Simulator page. */
23
18
  export type SliceSimulatorParams = {
24
19
  searchParams: Promise<{
25
- state?: string;
26
- }>;
27
- };
20
+ state?: string
21
+ }>
22
+ }
28
23
 
29
24
  export type SliceSimulatorProps = BaseSliceSimulatorProps & {
30
- children: ReactNode;
31
- className?: string;
32
- };
25
+ children: ReactNode
26
+ className?: string
27
+ }
33
28
 
34
29
  /**
35
- * Simulate slices in isolation. The slice simulator enables live slice
36
- * development in Slice Machine and live previews in the Page Builder.
30
+ * Simulate slices in isolation. The slice simulator enables live slice development in Slice Machine
31
+ * and live previews in the Page Builder.
37
32
  */
38
33
  export const SliceSimulator: FC<SliceSimulatorProps> = ({
39
34
  children,
@@ -41,45 +36,45 @@ export const SliceSimulator: FC<SliceSimulatorProps> = ({
41
36
  zIndex,
42
37
  className,
43
38
  }) => {
44
- const [message, setMessage] = useState(() => getDefaultMessage());
45
- const router = useRouter();
39
+ const [message, setMessage] = useState(() => getDefaultMessage())
40
+ const router = useRouter()
46
41
 
47
42
  const state =
48
43
  typeof window !== "undefined"
49
44
  ? new URL(window.location.href).searchParams.get(STATE_PARAMS_KEY)
50
- : undefined;
51
- const hasSlices = getSlices(state).length > 0;
45
+ : undefined
46
+ const hasSlices = getSlices(state).length > 0
52
47
 
53
48
  useEffect(() => {
54
49
  simulatorManager.state.on(
55
50
  StateEventType.Slices,
56
51
  (newSlices) => {
57
- const url = new URL(window.location.href);
52
+ const url = new URL(window.location.href)
58
53
  url.searchParams.set(
59
54
  STATE_PARAMS_KEY,
60
55
  compressToEncodedURIComponent(JSON.stringify(newSlices)),
61
- );
56
+ )
62
57
 
63
- window.history.replaceState(null, "", url);
58
+ window.history.replaceState(null, "", url)
64
59
  // Wait until the next tick to prevent URL state race conditions.
65
- setTimeout(() => router.refresh(), 0);
60
+ setTimeout(() => router.refresh(), 0)
66
61
  },
67
62
  "simulator-slices",
68
- );
63
+ )
69
64
  simulatorManager.state.on(
70
65
  StateEventType.Message,
71
66
  (newMessage) => setMessage(newMessage),
72
67
  "simulator-message",
73
- );
68
+ )
74
69
 
75
- simulatorManager.init();
70
+ simulatorManager.init()
76
71
 
77
72
  return () => {
78
- simulatorManager.state.off(StateEventType.Slices, "simulator-slices");
73
+ simulatorManager.state.off(StateEventType.Slices, "simulator-slices")
79
74
 
80
- simulatorManager.state.off(StateEventType.Message, "simulator-message");
81
- };
82
- }, [router]);
75
+ simulatorManager.state.off(StateEventType.Message, "simulator-message")
76
+ }
77
+ }, [router])
83
78
 
84
79
  return (
85
80
  <SliceSimulatorWrapper
@@ -91,5 +86,5 @@ export const SliceSimulator: FC<SliceSimulatorProps> = ({
91
86
  >
92
87
  {children}
93
88
  </SliceSimulatorWrapper>
94
- );
95
- };
89
+ )
90
+ }
@@ -4,21 +4,18 @@ import {
4
4
  onClickHandler,
5
5
  simulatorClass,
6
6
  simulatorRootClass,
7
- } from "@prismicio/simulator/kit";
8
- import type { SliceSimulatorProps } from "@prismicio/simulator/kit";
9
- import type { FC, ReactNode } from "react";
7
+ } from "@prismicio/simulator/kit"
8
+ import type { SliceSimulatorProps } from "@prismicio/simulator/kit"
9
+ import type { FC, ReactNode } from "react"
10
10
 
11
11
  type SliceSimulatorWrapperProps = SliceSimulatorProps & {
12
- children: ReactNode;
13
- className?: string;
14
- message?: string;
15
- hasSlices: boolean;
16
- };
12
+ children: ReactNode
13
+ className?: string
14
+ message?: string
15
+ hasSlices: boolean
16
+ }
17
17
 
18
- /**
19
- * A wrapper for the slice simulator that isolates the given children from the
20
- * page's layout.
21
- */
18
+ /** A wrapper for the slice simulator that isolates the given children from the page's layout. */
22
19
  export const SliceSimulatorWrapper: FC<SliceSimulatorWrapperProps> = ({
23
20
  className,
24
21
  children,
@@ -27,16 +24,13 @@ export const SliceSimulatorWrapper: FC<SliceSimulatorWrapperProps> = ({
27
24
  message,
28
25
  hasSlices,
29
26
  }) => {
30
- const defaultProps = getDefaultProps();
27
+ const defaultProps = getDefaultProps()
31
28
 
32
29
  return (
33
30
  <div
34
31
  className={[simulatorClass, className].filter(Boolean).join(" ")}
35
32
  style={{
36
- zIndex:
37
- typeof zIndex === "undefined"
38
- ? defaultProps.zIndex
39
- : (zIndex ?? undefined),
33
+ zIndex: typeof zIndex === "undefined" ? defaultProps.zIndex : (zIndex ?? undefined),
40
34
  position: "fixed",
41
35
  top: 0,
42
36
  left: 0,
@@ -44,9 +38,7 @@ export const SliceSimulatorWrapper: FC<SliceSimulatorWrapperProps> = ({
44
38
  height: "100vh",
45
39
  overflow: "auto",
46
40
  background:
47
- typeof background === "undefined"
48
- ? defaultProps.background
49
- : (background ?? undefined),
41
+ typeof background === "undefined" ? defaultProps.background : (background ?? undefined),
50
42
  }}
51
43
  >
52
44
  {message ? (
@@ -56,13 +48,11 @@ export const SliceSimulatorWrapper: FC<SliceSimulatorWrapperProps> = ({
56
48
  id="root"
57
49
  className={simulatorRootClass}
58
50
  onClickCapture={onClickHandler as unknown as React.MouseEventHandler}
59
- onSubmitCapture={
60
- disableEventHandler as unknown as React.FormEventHandler
61
- }
51
+ onSubmitCapture={disableEventHandler as unknown as React.FormEventHandler}
62
52
  >
63
53
  {children}
64
54
  </div>
65
55
  ) : null}
66
56
  </div>
67
- );
68
- };
57
+ )
58
+ }
@@ -0,0 +1,8 @@
1
+ import type { PrismicDocument } from "@prismicio/client"
2
+ import { cacheTag } from "next/cache"
3
+
4
+ import { getPrismicCacheTags } from "./getPrismicCacheTags"
5
+
6
+ export function cacheTagPrismicPages(pages: PrismicDocument[]): void {
7
+ cacheTag(...getPrismicCacheTags(pages))
8
+ }
@@ -1,4 +1,4 @@
1
- import { devMsg } from "./lib/devMsg";
1
+ import { devMsg } from "./lib/devMsg"
2
2
 
3
3
  /**
4
4
  * @deprecated `createLocaleRedirect()` has been removed due to performance
@@ -9,10 +9,10 @@ export function createLocaleRedirect(): never {
9
9
  `createLocaleRedirect() has been removed due to performance issues. See ${devMsg(
10
10
  "replace-createLocaleRedirect",
11
11
  )} for more details.`,
12
- );
12
+ )
13
13
  }
14
14
  /**
15
15
  * @deprecated `createLocaleRedirect()` has been removed due to performance
16
16
  * issues.
17
17
  */
18
- export type CreateLocaleRedirectConfig = never;
18
+ export type CreateLocaleRedirectConfig = never
@@ -1,4 +1,6 @@
1
- import { type Client, cookie as prismicCookie } from "@prismicio/client";
1
+ import type { Client } from "@prismicio/client"
2
+
3
+ import { getPreviewRef } from "./getPreviewRef"
2
4
 
3
5
  /** Configuration for `enableAutoPreviews`. */
4
6
  export type EnableAutoPreviewsConfig = {
@@ -6,48 +8,16 @@ export type EnableAutoPreviewsConfig = {
6
8
  // `Pick` is used to use the smallest possible subset of
7
9
  // `prismic.Client`. Doing this reduces the surface area for breaking
8
10
  // type changes.
9
- client: Pick<Client, "queryContentFromRef" | "enableAutoPreviewsFromReq">;
10
- };
11
+ client: Pick<Client, "queryContentFromRef" | "enableAutoPreviewsFromReq">
12
+ }
11
13
 
12
14
  /**
13
- * Configures a Prismic client to automatically query draft content during a
14
- * preview session.
15
+ * Configures a Prismic client to automatically query draft content during a preview session.
15
16
  *
16
17
  * @param config - Configuration for the function.
17
18
  */
18
19
  export function enableAutoPreviews(config: EnableAutoPreviewsConfig): void {
19
20
  // We use a function value so the cookie is checked on every
20
21
  // request. We don't have a static value to read from.
21
- config.client.queryContentFromRef(async () => {
22
- // Need this to avoid the following Next.js build-time error:
23
- // You're importing a component that needs next/headers. That only works
24
- // in a Server Component which is not supported in the pages/ directory.
25
- const { cookies, draftMode } = await import("next/headers");
26
-
27
- let isDraftModeEnabled = false;
28
- try {
29
- isDraftModeEnabled = (await draftMode()).isEnabled;
30
- } catch {
31
- // `draftMode()` may have been called in a palce that
32
- // does not have access to its async storage. This
33
- // occurs in places like `generateStaticParams()`. We
34
- // can ignore this case.
35
- return;
36
- }
37
- if (!isDraftModeEnabled) {
38
- return;
39
- }
40
-
41
- const cookie = (await cookies()).get(prismicCookie.preview)?.value;
42
- if (!cookie) {
43
- return;
44
- }
45
-
46
- const isActiveCookie = cookie.includes("websitePreviewId=");
47
- if (!isActiveCookie) {
48
- return;
49
- }
50
-
51
- return cookie;
52
- });
22
+ config.client.queryContentFromRef(() => getPreviewRef())
53
23
  }
@@ -1,31 +1,36 @@
1
+ import { cookie as prismicCookie } from "@prismicio/client"
2
+
1
3
  /**
2
- * Ends a Prismic preview session within a Next.js app. This function should be
3
- * used in a Router Handler.
4
+ * Ends a Prismic preview session within a Next.js app. This function should be used in a Router
5
+ * Handler.
4
6
  *
5
7
  * @example
8
+ * ;```typescript
9
+ * // src/app/api/exit-preview/route.js
6
10
  *
7
- * ```typescript
8
- * // src/app/api/exit-preview/route.js
9
- *
10
- * import { exitPreview } from "@prismicio/next";
11
+ * import { exitPreview } from "@prismicio/next"
11
12
  *
12
- * export async function GET() {
13
- * return await exitPreview();
14
- * }
15
- * ```
13
+ * export async function GET() {
14
+ * return await exitPreview()
15
+ * }
16
+ * ```
16
17
  */
17
18
  export async function exitPreview(): Promise<Response> {
18
19
  // Need this to avoid the following Next.js build-time error:
19
20
  // You're importing a component that needs next/headers. That only works
20
21
  // in a Server Component which is not supported in the pages/ directory.
21
- const { draftMode } = await import("next/headers");
22
+ const { cookies, draftMode } = await import("next/headers")
23
+
24
+ ;(await draftMode()).disable()
22
25
 
23
- (await draftMode()).disable();
26
+ // `redirectToPreviewURL` writes the preview cookie, so `exitPreview`
27
+ // clears it to close the preview-cookie loop.
28
+ ;(await cookies()).delete(prismicCookie.preview)
24
29
 
25
30
  // `Cache-Control` header is used to prevent CDN-level caching.
26
31
  return new Response(JSON.stringify({ success: true }), {
27
32
  headers: {
28
33
  "Cache-Control": "no-store",
29
34
  },
30
- });
35
+ })
31
36
  }
@@ -0,0 +1,52 @@
1
+ import { cookie as prismicCookie } from "@prismicio/client"
2
+
3
+ /**
4
+ * Reads the Prismic preview ref for the current request when an active preview
5
+ * session exists.
6
+ *
7
+ * This is the read-side counterpart to `redirectToPreviewURL`, which writes the
8
+ * preview cookie. Use it with Next.js Cache Components to read the ref _outside_
9
+ * a cached function and pass it _in_ as an argument so it becomes part of the
10
+ * cache key:
11
+ *
12
+ * @example
13
+ * ;```typescript
14
+ * import { getPreviewRef } from "@prismicio/next"
15
+ *
16
+ * const page = await fetchPage(uid, await getPreviewRef())
17
+ * ```
18
+ *
19
+ * @returns The active preview ref, or `undefined` if no preview session is active.
20
+ */
21
+ export async function getPreviewRef(): Promise<string | undefined> {
22
+ // Need this to avoid the following Next.js build-time error:
23
+ // You're importing a component that needs next/headers. That only works
24
+ // in a Server Component which is not supported in the pages/ directory.
25
+ const { cookies, draftMode } = await import("next/headers")
26
+
27
+ let isDraftModeEnabled = false
28
+ try {
29
+ isDraftModeEnabled = (await draftMode()).isEnabled
30
+ } catch {
31
+ // `draftMode()` may have been called in a place that
32
+ // does not have access to its async storage. This
33
+ // occurs in places like `generateStaticParams()`. We
34
+ // can ignore this case.
35
+ return
36
+ }
37
+ if (!isDraftModeEnabled) {
38
+ return
39
+ }
40
+
41
+ const cookie = (await cookies()).get(prismicCookie.preview)?.value
42
+ if (!cookie) {
43
+ return
44
+ }
45
+
46
+ const isActiveCookie = cookie.includes("websitePreviewId=")
47
+ if (!isActiveCookie) {
48
+ return
49
+ }
50
+
51
+ return cookie
52
+ }
@@ -0,0 +1,44 @@
1
+ import type { PrismicDocument } from "@prismicio/client"
2
+
3
+ const CACHE_TAG_PREFIX = "prismic/"
4
+
5
+ /**
6
+ * Generates cache tags for a set of Prismic pages. Tags for linked pages (e.g. a content
7
+ * relationship) are included to ensure all related content can be revalidated.
8
+ *
9
+ * @param pages - A set of Prismic pages used to tag the function.
10
+ * @see {@link https://nextjs.org/docs/app/api-reference/functions/cacheTag}
11
+ */
12
+ export function getPrismicCacheTags(pages: PrismicDocument[]): string[] {
13
+ const ids = [...new Set(getPrismicDocumentIDs(pages))]
14
+ return ids.map((id) => buildPrismicCacheTag(id))
15
+ }
16
+
17
+ export function buildPrismicCacheTag(id: string): string {
18
+ return `${CACHE_TAG_PREFIX}${id}`
19
+ }
20
+
21
+ function getPrismicDocumentIDs(input: unknown): string[] {
22
+ if (typeof input !== "object" || input == null) return []
23
+
24
+ if (Array.isArray(input)) return input.flatMap((item) => getPrismicDocumentIDs(item))
25
+
26
+ if ("id" in input && typeof input.id === "string") {
27
+ // Document
28
+ // Content relationship or link
29
+ if (
30
+ ("href" in input &&
31
+ typeof input.href === "string" &&
32
+ input.href.includes("/api/v2/documents/search?")) ||
33
+ ("link_type" in input && input.link_type === "Document")
34
+ ) {
35
+ if ("data" in input && typeof input.data === "object" && input.data !== null) {
36
+ return [input.id, ...getPrismicDocumentIDs(input.data)]
37
+ } else {
38
+ return [input.id]
39
+ }
40
+ }
41
+ }
42
+
43
+ return Object.values(input).flatMap((input) => getPrismicDocumentIDs(input))
44
+ }
package/src/getSlices.ts CHANGED
@@ -1,11 +1,7 @@
1
- import { getDefaultSlices } from "@prismicio/simulator/kit";
2
- import type { StateEvents, StateEventType } from "@prismicio/simulator/kit";
3
- import { decompressFromEncodedURIComponent } from "lz-string";
1
+ import { getDefaultSlices } from "@prismicio/simulator/kit"
2
+ import type { StateEvents, StateEventType } from "@prismicio/simulator/kit"
3
+ import { decompressFromEncodedURIComponent } from "lz-string"
4
4
 
5
- export const getSlices = (
6
- state: string | null | undefined,
7
- ): StateEvents[StateEventType.Slices] => {
8
- return state
9
- ? JSON.parse(decompressFromEncodedURIComponent(state))
10
- : getDefaultSlices();
11
- };
5
+ export const getSlices = (state: string | null | undefined): StateEvents[StateEventType.Slices] => {
6
+ return state ? JSON.parse(decompressFromEncodedURIComponent(state)) : getDefaultSlices()
7
+ }
@@ -1,26 +1,26 @@
1
- import type { ImageLoaderProps } from "next/image";
2
- import type { ImgixURLParams } from "imgix-url-builder";
3
- import { buildURL } from "imgix-url-builder";
1
+ import type { ImgixURLParams } from "imgix-url-builder"
2
+ import { buildURL } from "imgix-url-builder"
3
+ import type { ImageLoaderProps } from "next/image"
4
4
 
5
5
  /**
6
- * A `next/image` loader for Imgix, which Prismic uses, with an optional
7
- * collection of default Imgix parameters.
6
+ * A `next/image` loader for Imgix, which Prismic uses, with an optional collection of default Imgix
7
+ * parameters.
8
8
  *
9
9
  * @see To learn about `next/image` loaders: https://nextjs.org/docs/api-reference/next/image#loader
10
10
  * @see To learn about Imgix's URL API: https://docs.imgix.com/apis/rendering
11
11
  */
12
12
  export const imgixLoader = (args: ImageLoaderProps): string => {
13
- const url = new URL(args.src);
13
+ const url = new URL(args.src)
14
14
 
15
15
  const params: ImgixURLParams = {
16
16
  fit: (url.searchParams.get("fit") as ImgixURLParams["fit"]) || "max",
17
17
  w: args.width,
18
18
  h: undefined,
19
- };
19
+ }
20
20
 
21
21
  if (args.quality) {
22
- params.q = args.quality;
22
+ params.q = args.quality
23
23
  }
24
24
 
25
- return buildURL(args.src, params);
26
- };
25
+ return buildURL(args.src, params)
26
+ }
package/src/index.ts CHANGED
@@ -1,31 +1,36 @@
1
- export { exitPreview } from "./exitPreview";
1
+ export { exitPreview } from "./exitPreview"
2
2
 
3
- export { PrismicPreview } from "./PrismicPreview";
4
- export type { PrismicPreviewProps } from "./PrismicPreview";
3
+ export { PrismicPreview } from "./PrismicPreview"
4
+ export type { PrismicPreviewProps } from "./PrismicPreview"
5
5
 
6
- export { PrismicNextLink } from "./PrismicNextLink";
7
- export type { PrismicNextLinkProps } from "./PrismicNextLink";
6
+ export { PrismicNextLink } from "./PrismicNextLink"
7
+ export type { PrismicNextLinkProps } from "./PrismicNextLink"
8
8
 
9
- export { enableAutoPreviews } from "./enableAutoPreviews";
10
- export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews";
9
+ export { enableAutoPreviews } from "./enableAutoPreviews"
10
+ export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews"
11
11
 
12
- export { redirectToPreviewURL } from "./redirectToPreviewURL";
13
- export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL";
12
+ export { getPreviewRef } from "./getPreviewRef"
14
13
 
15
- export { PrismicNextImage } from "./PrismicNextImage";
16
- export type { PrismicNextImageProps } from "./PrismicNextImage";
14
+ export { redirectToPreviewURL } from "./redirectToPreviewURL"
15
+ export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL"
17
16
 
18
- export { SliceSimulator } from "./SliceSimulator";
19
- export type {
20
- SliceSimulatorProps,
21
- SliceSimulatorParams,
22
- } from "./SliceSimulator";
17
+ export { PrismicNextImage } from "./PrismicNextImage"
18
+ export type { PrismicNextImageProps } from "./PrismicNextImage"
23
19
 
24
- export { getSlices } from "./getSlices";
20
+ export { SliceSimulator } from "./SliceSimulator"
21
+ export type { SliceSimulatorProps, SliceSimulatorParams } from "./SliceSimulator"
25
22
 
26
- export { imgixLoader } from "./imgixLoader";
23
+ export { getSlices } from "./getSlices"
27
24
 
28
- export type { CreateClientConfig } from "./types";
25
+ export { imgixLoader } from "./imgixLoader"
29
26
 
30
- export { createLocaleRedirect } from "./createLocaleRedirect";
31
- export type { CreateLocaleRedirectConfig } from "./createLocaleRedirect";
27
+ export type { CreateClientConfig } from "./types"
28
+
29
+ export { createLocaleRedirect } from "./createLocaleRedirect"
30
+ export type { CreateLocaleRedirectConfig } from "./createLocaleRedirect"
31
+
32
+ export { getPrismicCacheTags } from "./getPrismicCacheTags"
33
+
34
+ export { cacheTagPrismicPages } from "./cacheTagPrismicPages"
35
+
36
+ export { revalidatePrismicPages } from "./revalidatePrismicPages"
package/src/lib/devMsg.ts CHANGED
@@ -1,20 +1,18 @@
1
- import { version } from "../../package.json";
1
+ import { version } from "../../package.json"
2
2
 
3
3
  /**
4
4
  * Returns a `prismic.dev/msg` URL for a given message slug.
5
5
  *
6
6
  * @example
7
+ * ;```ts
8
+ * devMsg("missing-param")
9
+ * // => "https://prismic.dev/msg/next/v1.2.3/missing-param"
10
+ * ```
7
11
  *
8
- * ```ts
9
- * devMsg("missing-param");
10
- * // => "https://prismic.dev/msg/next/v1.2.3/missing-param"
11
- * ```
12
- *
13
- * @param slug - Slug for the message. This corresponds to a Markdown file in
14
- * the Git repository's `/messages` directory.
15
- *
12
+ * @param slug - Slug for the message. This corresponds to a Markdown file in the Git repository's
13
+ * `/messages` directory.
16
14
  * @returns The `prismic.dev/msg` URL for the given slug.
17
15
  */
18
16
  export const devMsg = (slug: string): string => {
19
- return `https://prismic.dev/msg/next/v${version}/${slug}`;
20
- };
17
+ return `https://prismic.dev/msg/next/v${version}/${slug}`
18
+ }
@@ -1,21 +1,17 @@
1
1
  /**
2
- * Resolves a module's default export. The module may provide its default export
3
- * as a `default` property on an object. This happens when named and default
4
- * exports are mixed in modules.
2
+ * Resolves a module's default export. The module may provide its default export as a `default`
3
+ * property on an object. This happens when named and default exports are mixed in modules.
5
4
  *
6
- * In ES Modules, mixing is fine since the module resolver can distinguish
7
- * default and named exports. In transpiled modules, however, all exports are
8
- * put into a single object, and the default export is provided at a property
9
- * called `default`.
5
+ * In ES Modules, mixing is fine since the module resolver can distinguish default and named
6
+ * exports. In transpiled modules, however, all exports are put into a single object, and the
7
+ * default export is provided at a property called `default`.
10
8
  *
11
- * This helper is needed by Next.js uses CJS files with named and default
12
- * exports.
9
+ * This helper is needed by Next.js uses CJS files with named and default exports.
13
10
  *
14
- * This helper _should_ be a temporary hack until Next.js and Node.js resolve
15
- * their ESM difficulties. This will likely not be removed any time soon,
16
- * unfortunately.
11
+ * This helper _should_ be a temporary hack until Next.js and Node.js resolve their ESM
12
+ * difficulties. This will likely not be removed any time soon, unfortunately.
17
13
  */
18
14
  export function resolveDefaultExport<T>(mod: T): T {
19
15
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
20
- return "default" in (mod as any) ? (mod as any).default : mod;
16
+ return "default" in (mod as any) ? (mod as any).default : mod
21
17
  }