@prismicio/next 2.2.3 → 2.2.4-pr.114.37ca25f

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 (192) 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 +3 -3
  11. package/dist/PrismicNextLink.d.cts.map +1 -0
  12. package/dist/PrismicNextLink.d.ts +3 -3
  13. package/dist/PrismicNextLink.d.ts.map +1 -0
  14. package/dist/PrismicNextLink.js.map +1 -1
  15. package/dist/PrismicPreview.cjs +6 -7
  16. package/dist/PrismicPreview.cjs.map +1 -1
  17. package/dist/PrismicPreview.d.cts +11 -12
  18. package/dist/PrismicPreview.d.cts.map +1 -0
  19. package/dist/PrismicPreview.d.ts +11 -12
  20. package/dist/PrismicPreview.d.ts.map +1 -0
  21. package/dist/PrismicPreview.js +6 -7
  22. package/dist/PrismicPreview.js.map +1 -1
  23. package/dist/PrismicPreviewClient.cjs +1 -3
  24. package/dist/PrismicPreviewClient.cjs.map +1 -1
  25. package/dist/PrismicPreviewClient.js +1 -3
  26. package/dist/PrismicPreviewClient.js.map +1 -1
  27. package/dist/SliceSimulator.cjs +3 -3
  28. package/dist/SliceSimulator.cjs.map +1 -1
  29. package/dist/SliceSimulator.d.cts +3 -5
  30. package/dist/SliceSimulator.d.cts.map +1 -0
  31. package/dist/SliceSimulator.d.ts +3 -5
  32. package/dist/SliceSimulator.d.ts.map +1 -0
  33. package/dist/SliceSimulator.js +3 -3
  34. package/dist/SliceSimulator.js.map +1 -1
  35. package/dist/SliceSimulatorWrapper.cjs +1 -4
  36. package/dist/SliceSimulatorWrapper.cjs.map +1 -1
  37. package/dist/SliceSimulatorWrapper.js +1 -4
  38. package/dist/SliceSimulatorWrapper.js.map +1 -1
  39. package/dist/cacheTagPrismicPages.cjs +11 -0
  40. package/dist/cacheTagPrismicPages.cjs.map +1 -0
  41. package/dist/cacheTagPrismicPages.d.cts +7 -0
  42. package/dist/cacheTagPrismicPages.d.cts.map +1 -0
  43. package/dist/cacheTagPrismicPages.d.ts +7 -0
  44. package/dist/cacheTagPrismicPages.d.ts.map +1 -0
  45. package/dist/cacheTagPrismicPages.js +10 -0
  46. package/dist/cacheTagPrismicPages.js.map +1 -0
  47. package/dist/createLocaleRedirect.cjs.map +1 -1
  48. package/dist/createLocaleRedirect.d.cts.map +1 -0
  49. package/dist/createLocaleRedirect.d.ts.map +1 -0
  50. package/dist/createLocaleRedirect.js.map +1 -1
  51. package/dist/enableAutoPreviews.cjs +1 -2
  52. package/dist/enableAutoPreviews.cjs.map +1 -1
  53. package/dist/enableAutoPreviews.d.cts +1 -2
  54. package/dist/enableAutoPreviews.d.cts.map +1 -0
  55. package/dist/enableAutoPreviews.d.ts +1 -2
  56. package/dist/enableAutoPreviews.d.ts.map +1 -0
  57. package/dist/enableAutoPreviews.js +1 -2
  58. package/dist/enableAutoPreviews.js.map +1 -1
  59. package/dist/exitPreview.cjs +9 -10
  60. package/dist/exitPreview.cjs.map +1 -1
  61. package/dist/exitPreview.d.cts +9 -10
  62. package/dist/exitPreview.d.cts.map +1 -0
  63. package/dist/exitPreview.d.ts +9 -10
  64. package/dist/exitPreview.d.ts.map +1 -0
  65. package/dist/exitPreview.js +9 -10
  66. package/dist/exitPreview.js.map +1 -1
  67. package/dist/getPrismicCacheTags.cjs +29 -0
  68. package/dist/getPrismicCacheTags.cjs.map +1 -0
  69. package/dist/getPrismicCacheTags.d.cts +14 -0
  70. package/dist/getPrismicCacheTags.d.cts.map +1 -0
  71. package/dist/getPrismicCacheTags.d.ts +14 -0
  72. package/dist/getPrismicCacheTags.d.ts.map +1 -0
  73. package/dist/getPrismicCacheTags.js +28 -0
  74. package/dist/getPrismicCacheTags.js.map +1 -0
  75. package/dist/getSlices.cjs.map +1 -1
  76. package/dist/getSlices.d.cts.map +1 -0
  77. package/dist/getSlices.d.ts.map +1 -0
  78. package/dist/getSlices.js.map +1 -1
  79. package/dist/imgixLoader.cjs +2 -2
  80. package/dist/imgixLoader.cjs.map +1 -1
  81. package/dist/imgixLoader.d.cts +2 -2
  82. package/dist/imgixLoader.d.cts.map +1 -0
  83. package/dist/imgixLoader.d.ts +2 -2
  84. package/dist/imgixLoader.d.ts.map +1 -0
  85. package/dist/imgixLoader.js +2 -2
  86. package/dist/imgixLoader.js.map +1 -1
  87. package/dist/index.cjs +6 -0
  88. package/dist/index.d.cts +4 -1
  89. package/dist/index.d.ts +4 -1
  90. package/dist/index.js +4 -1
  91. package/dist/lib/devMsg.cjs +6 -8
  92. package/dist/lib/devMsg.cjs.map +1 -1
  93. package/dist/lib/devMsg.js +6 -8
  94. package/dist/lib/devMsg.js.map +1 -1
  95. package/dist/lib/resolveDefaultExport.cjs +8 -12
  96. package/dist/lib/resolveDefaultExport.cjs.map +1 -1
  97. package/dist/lib/resolveDefaultExport.js +8 -12
  98. package/dist/lib/resolveDefaultExport.js.map +1 -1
  99. package/dist/package.cjs +1 -1
  100. package/dist/package.js +1 -1
  101. package/dist/pages/PrismicPreview.cjs +6 -7
  102. package/dist/pages/PrismicPreview.cjs.map +1 -1
  103. package/dist/pages/PrismicPreview.d.cts +11 -12
  104. package/dist/pages/PrismicPreview.d.cts.map +1 -0
  105. package/dist/pages/PrismicPreview.d.ts +11 -12
  106. package/dist/pages/PrismicPreview.d.ts.map +1 -0
  107. package/dist/pages/PrismicPreview.js +6 -7
  108. package/dist/pages/PrismicPreview.js.map +1 -1
  109. package/dist/pages/SliceSimulator.cjs +2 -2
  110. package/dist/pages/SliceSimulator.cjs.map +1 -1
  111. package/dist/pages/SliceSimulator.d.cts +10 -13
  112. package/dist/pages/SliceSimulator.d.cts.map +1 -0
  113. package/dist/pages/SliceSimulator.d.ts +10 -13
  114. package/dist/pages/SliceSimulator.d.ts.map +1 -0
  115. package/dist/pages/SliceSimulator.js +2 -2
  116. package/dist/pages/SliceSimulator.js.map +1 -1
  117. package/dist/pages/enableAutoPreviews.cjs +3 -3
  118. package/dist/pages/enableAutoPreviews.cjs.map +1 -1
  119. package/dist/pages/enableAutoPreviews.d.cts +5 -5
  120. package/dist/pages/enableAutoPreviews.d.cts.map +1 -0
  121. package/dist/pages/enableAutoPreviews.d.ts +5 -5
  122. package/dist/pages/enableAutoPreviews.d.ts.map +1 -0
  123. package/dist/pages/enableAutoPreviews.js +3 -3
  124. package/dist/pages/enableAutoPreviews.js.map +1 -1
  125. package/dist/pages/exitPreview.cjs +9 -10
  126. package/dist/pages/exitPreview.cjs.map +1 -1
  127. package/dist/pages/exitPreview.d.cts +9 -10
  128. package/dist/pages/exitPreview.d.cts.map +1 -0
  129. package/dist/pages/exitPreview.d.ts +9 -10
  130. package/dist/pages/exitPreview.d.ts.map +1 -0
  131. package/dist/pages/exitPreview.js +9 -10
  132. package/dist/pages/exitPreview.js.map +1 -1
  133. package/dist/pages/redirectToPreviewURL.cjs.map +1 -1
  134. package/dist/pages/redirectToPreviewURL.d.cts +7 -9
  135. package/dist/pages/redirectToPreviewURL.d.cts.map +1 -0
  136. package/dist/pages/redirectToPreviewURL.d.ts +7 -9
  137. package/dist/pages/redirectToPreviewURL.d.ts.map +1 -0
  138. package/dist/pages/redirectToPreviewURL.js.map +1 -1
  139. package/dist/pages/setPreviewData.cjs.map +1 -1
  140. package/dist/pages/setPreviewData.d.cts.map +1 -0
  141. package/dist/pages/setPreviewData.d.ts.map +1 -0
  142. package/dist/pages/setPreviewData.js.map +1 -1
  143. package/dist/pages/types.d.cts +3 -8
  144. package/dist/pages/types.d.cts.map +1 -0
  145. package/dist/pages/types.d.ts +3 -8
  146. package/dist/pages/types.d.ts.map +1 -0
  147. package/dist/redirectToPreviewURL.cjs.map +1 -1
  148. package/dist/redirectToPreviewURL.d.cts +3 -5
  149. package/dist/redirectToPreviewURL.d.cts.map +1 -0
  150. package/dist/redirectToPreviewURL.d.ts +3 -5
  151. package/dist/redirectToPreviewURL.d.ts.map +1 -0
  152. package/dist/redirectToPreviewURL.js.map +1 -1
  153. package/dist/revalidatePrismicPages.cjs +18 -0
  154. package/dist/revalidatePrismicPages.cjs.map +1 -0
  155. package/dist/revalidatePrismicPages.d.cts +12 -0
  156. package/dist/revalidatePrismicPages.d.cts.map +1 -0
  157. package/dist/revalidatePrismicPages.d.ts +12 -0
  158. package/dist/revalidatePrismicPages.d.ts.map +1 -0
  159. package/dist/revalidatePrismicPages.js +17 -0
  160. package/dist/revalidatePrismicPages.js.map +1 -0
  161. package/dist/types.d.cts +1 -2
  162. package/dist/types.d.cts.map +1 -0
  163. package/dist/types.d.ts +1 -2
  164. package/dist/types.d.ts.map +1 -0
  165. package/package.json +32 -34
  166. package/src/PrismicNextImage.tsx +113 -134
  167. package/src/PrismicNextLink.tsx +43 -45
  168. package/src/PrismicPreview.tsx +28 -29
  169. package/src/PrismicPreviewClient.tsx +42 -44
  170. package/src/SliceSimulator.tsx +38 -43
  171. package/src/SliceSimulatorWrapper.tsx +15 -25
  172. package/src/cacheTagPrismicPages.ts +8 -0
  173. package/src/createLocaleRedirect.ts +3 -3
  174. package/src/enableAutoPreviews.ts +15 -16
  175. package/src/exitPreview.ts +12 -13
  176. package/src/getPrismicCacheTags.ts +44 -0
  177. package/src/getSlices.ts +6 -10
  178. package/src/imgixLoader.ts +10 -10
  179. package/src/index.ts +24 -21
  180. package/src/lib/devMsg.ts +9 -11
  181. package/src/lib/resolveDefaultExport.ts +9 -13
  182. package/src/pages/PrismicPreview.tsx +47 -51
  183. package/src/pages/SliceSimulator.tsx +35 -37
  184. package/src/pages/enableAutoPreviews.ts +17 -17
  185. package/src/pages/exitPreview.ts +17 -18
  186. package/src/pages/index.ts +20 -23
  187. package/src/pages/redirectToPreviewURL.ts +22 -33
  188. package/src/pages/setPreviewData.ts +7 -7
  189. package/src/pages/types.ts +16 -20
  190. package/src/redirectToPreviewURL.ts +21 -30
  191. package/src/revalidatePrismicPages.ts +17 -0
  192. 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,4 @@
1
- import { type Client, cookie as prismicCookie } from "@prismicio/client";
1
+ import { type Client, cookie as prismicCookie } from "@prismicio/client"
2
2
 
3
3
  /** Configuration for `enableAutoPreviews`. */
4
4
  export type EnableAutoPreviewsConfig = {
@@ -6,12 +6,11 @@ export type EnableAutoPreviewsConfig = {
6
6
  // `Pick` is used to use the smallest possible subset of
7
7
  // `prismic.Client`. Doing this reduces the surface area for breaking
8
8
  // type changes.
9
- client: Pick<Client, "queryContentFromRef" | "enableAutoPreviewsFromReq">;
10
- };
9
+ client: Pick<Client, "queryContentFromRef" | "enableAutoPreviewsFromReq">
10
+ }
11
11
 
12
12
  /**
13
- * Configures a Prismic client to automatically query draft content during a
14
- * preview session.
13
+ * Configures a Prismic client to automatically query draft content during a preview session.
15
14
  *
16
15
  * @param config - Configuration for the function.
17
16
  */
@@ -22,32 +21,32 @@ export function enableAutoPreviews(config: EnableAutoPreviewsConfig): void {
22
21
  // Need this to avoid the following Next.js build-time error:
23
22
  // You're importing a component that needs next/headers. That only works
24
23
  // in a Server Component which is not supported in the pages/ directory.
25
- const { cookies, draftMode } = await import("next/headers");
24
+ const { cookies, draftMode } = await import("next/headers")
26
25
 
27
- let isDraftModeEnabled = false;
26
+ let isDraftModeEnabled = false
28
27
  try {
29
- isDraftModeEnabled = (await draftMode()).isEnabled;
28
+ isDraftModeEnabled = (await draftMode()).isEnabled
30
29
  } catch {
31
30
  // `draftMode()` may have been called in a palce that
32
31
  // does not have access to its async storage. This
33
32
  // occurs in places like `generateStaticParams()`. We
34
33
  // can ignore this case.
35
- return;
34
+ return
36
35
  }
37
36
  if (!isDraftModeEnabled) {
38
- return;
37
+ return
39
38
  }
40
39
 
41
- const cookie = (await cookies()).get(prismicCookie.preview)?.value;
40
+ const cookie = (await cookies()).get(prismicCookie.preview)?.value
42
41
  if (!cookie) {
43
- return;
42
+ return
44
43
  }
45
44
 
46
- const isActiveCookie = cookie.includes("websitePreviewId=");
45
+ const isActiveCookie = cookie.includes("websitePreviewId=")
47
46
  if (!isActiveCookie) {
48
- return;
47
+ return
49
48
  }
50
49
 
51
- return cookie;
52
- });
50
+ return cookie
51
+ })
53
52
  }
@@ -1,31 +1,30 @@
1
1
  /**
2
- * Ends a Prismic preview session within a Next.js app. This function should be
3
- * used in a Router Handler.
2
+ * Ends a Prismic preview session within a Next.js app. This function should be used in a Router
3
+ * Handler.
4
4
  *
5
5
  * @example
6
+ * ;```typescript
7
+ * // src/app/api/exit-preview/route.js
6
8
  *
7
- * ```typescript
8
- * // src/app/api/exit-preview/route.js
9
+ * import { exitPreview } from "@prismicio/next"
9
10
  *
10
- * import { exitPreview } from "@prismicio/next";
11
- *
12
- * export async function GET() {
13
- * return await exitPreview();
14
- * }
15
- * ```
11
+ * export async function GET() {
12
+ * return await exitPreview()
13
+ * }
14
+ * ```
16
15
  */
17
16
  export async function exitPreview(): Promise<Response> {
18
17
  // Need this to avoid the following Next.js build-time error:
19
18
  // You're importing a component that needs next/headers. That only works
20
19
  // in a Server Component which is not supported in the pages/ directory.
21
- const { draftMode } = await import("next/headers");
20
+ const { draftMode } = await import("next/headers")
22
21
 
23
- (await draftMode()).disable();
22
+ ;(await draftMode()).disable()
24
23
 
25
24
  // `Cache-Control` header is used to prevent CDN-level caching.
26
25
  return new Response(JSON.stringify({ success: true }), {
27
26
  headers: {
28
27
  "Cache-Control": "no-store",
29
28
  },
30
- });
29
+ })
31
30
  }
@@ -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,34 @@
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 { redirectToPreviewURL } from "./redirectToPreviewURL"
13
+ export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL"
14
14
 
15
- export { PrismicNextImage } from "./PrismicNextImage";
16
- export type { PrismicNextImageProps } from "./PrismicNextImage";
15
+ export { PrismicNextImage } from "./PrismicNextImage"
16
+ export type { PrismicNextImageProps } from "./PrismicNextImage"
17
17
 
18
- export { SliceSimulator } from "./SliceSimulator";
19
- export type {
20
- SliceSimulatorProps,
21
- SliceSimulatorParams,
22
- } from "./SliceSimulator";
18
+ export { SliceSimulator } from "./SliceSimulator"
19
+ export type { SliceSimulatorProps, SliceSimulatorParams } from "./SliceSimulator"
23
20
 
24
- export { getSlices } from "./getSlices";
21
+ export { getSlices } from "./getSlices"
25
22
 
26
- export { imgixLoader } from "./imgixLoader";
23
+ export { imgixLoader } from "./imgixLoader"
27
24
 
28
- export type { CreateClientConfig } from "./types";
25
+ export type { CreateClientConfig } from "./types"
29
26
 
30
- export { createLocaleRedirect } from "./createLocaleRedirect";
31
- export type { CreateLocaleRedirectConfig } from "./createLocaleRedirect";
27
+ export { createLocaleRedirect } from "./createLocaleRedirect"
28
+ export type { CreateLocaleRedirectConfig } from "./createLocaleRedirect"
29
+
30
+ export { getPrismicCacheTags } from "./getPrismicCacheTags"
31
+
32
+ export { cacheTagPrismicPages } from "./cacheTagPrismicPages"
33
+
34
+ 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
  }