@prismicio/next 2.2.4 → 2.3.0-canary.d1640ef

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 (80) hide show
  1. package/dist/PrismicNextLink.d.cts +3 -3
  2. package/dist/PrismicNextLink.d.ts +3 -3
  3. package/dist/PrismicPreviewClient.cjs +2 -2
  4. package/dist/PrismicPreviewClient.cjs.map +1 -1
  5. package/dist/PrismicPreviewClient.js +1 -1
  6. package/dist/SliceSimulator.cjs +5 -4
  7. package/dist/SliceSimulator.cjs.map +1 -1
  8. package/dist/SliceSimulator.js +3 -3
  9. package/dist/SliceSimulator.js.map +1 -1
  10. package/dist/cacheTagPrismicPages.cjs +24 -0
  11. package/dist/cacheTagPrismicPages.cjs.map +1 -0
  12. package/dist/cacheTagPrismicPages.d.cts +19 -0
  13. package/dist/cacheTagPrismicPages.d.cts.map +1 -0
  14. package/dist/cacheTagPrismicPages.d.ts +19 -0
  15. package/dist/cacheTagPrismicPages.d.ts.map +1 -0
  16. package/dist/cacheTagPrismicPages.js +22 -0
  17. package/dist/cacheTagPrismicPages.js.map +1 -0
  18. package/dist/enableAutoPreviews.cjs +2 -16
  19. package/dist/enableAutoPreviews.cjs.map +1 -1
  20. package/dist/enableAutoPreviews.d.cts.map +1 -1
  21. package/dist/enableAutoPreviews.d.ts.map +1 -1
  22. package/dist/enableAutoPreviews.js +2 -15
  23. package/dist/enableAutoPreviews.js.map +1 -1
  24. package/dist/exitPreview.cjs +4 -1
  25. package/dist/exitPreview.cjs.map +1 -1
  26. package/dist/exitPreview.d.cts.map +1 -1
  27. package/dist/exitPreview.d.ts.map +1 -1
  28. package/dist/exitPreview.js +3 -1
  29. package/dist/exitPreview.js.map +1 -1
  30. package/dist/getPreviewRef.cjs +38 -0
  31. package/dist/getPreviewRef.cjs.map +1 -0
  32. package/dist/getPreviewRef.d.cts +22 -0
  33. package/dist/getPreviewRef.d.cts.map +1 -0
  34. package/dist/getPreviewRef.d.ts +22 -0
  35. package/dist/getPreviewRef.d.ts.map +1 -0
  36. package/dist/getPreviewRef.js +37 -0
  37. package/dist/getPreviewRef.js.map +1 -0
  38. package/dist/getPrismicCacheTags.cjs +31 -0
  39. package/dist/getPrismicCacheTags.cjs.map +1 -0
  40. package/dist/getPrismicCacheTags.d.cts +16 -0
  41. package/dist/getPrismicCacheTags.d.cts.map +1 -0
  42. package/dist/getPrismicCacheTags.d.ts +16 -0
  43. package/dist/getPrismicCacheTags.d.ts.map +1 -0
  44. package/dist/getPrismicCacheTags.js +30 -0
  45. package/dist/getPrismicCacheTags.js.map +1 -0
  46. package/dist/getSlices.cjs +3 -2
  47. package/dist/getSlices.cjs.map +1 -1
  48. package/dist/getSlices.js +2 -2
  49. package/dist/getSlices.js.map +1 -1
  50. package/dist/index.cjs +8 -0
  51. package/dist/index.d.cts +5 -1
  52. package/dist/index.d.ts +5 -1
  53. package/dist/index.js +5 -1
  54. package/dist/package.cjs +1 -1
  55. package/dist/package.js +1 -1
  56. package/dist/redirectToPreviewURL.cjs +2 -2
  57. package/dist/redirectToPreviewURL.cjs.map +1 -1
  58. package/dist/redirectToPreviewURL.d.cts.map +1 -1
  59. package/dist/redirectToPreviewURL.d.ts.map +1 -1
  60. package/dist/redirectToPreviewURL.js +2 -2
  61. package/dist/redirectToPreviewURL.js.map +1 -1
  62. package/dist/revalidatePrismicPages.cjs +20 -0
  63. package/dist/revalidatePrismicPages.cjs.map +1 -0
  64. package/dist/revalidatePrismicPages.d.cts +14 -0
  65. package/dist/revalidatePrismicPages.d.cts.map +1 -0
  66. package/dist/revalidatePrismicPages.d.ts +14 -0
  67. package/dist/revalidatePrismicPages.d.ts.map +1 -0
  68. package/dist/revalidatePrismicPages.js +19 -0
  69. package/dist/revalidatePrismicPages.js.map +1 -0
  70. package/package.json +7 -6
  71. package/src/SliceSimulator.tsx +2 -2
  72. package/src/cacheTagPrismicPages.ts +21 -0
  73. package/src/enableAutoPreviews.ts +4 -33
  74. package/src/exitPreview.ts +7 -1
  75. package/src/getPreviewRef.ts +51 -0
  76. package/src/getPrismicCacheTags.ts +46 -0
  77. package/src/getSlices.ts +2 -2
  78. package/src/index.ts +8 -0
  79. package/src/redirectToPreviewURL.ts +5 -2
  80. package/src/revalidatePrismicPages.ts +19 -0
@@ -0,0 +1,51 @@
1
+ import { cookie as prismicCookie } from "@prismicio/client"
2
+
3
+ /**
4
+ * Reads the Prismic preview ref for the current request when an active preview session exists.
5
+ *
6
+ * This is the read-side counterpart to `redirectToPreviewURL`, which writes the preview cookie. Use
7
+ * it with Next.js Cache Components to read the ref _outside_ a cached function and pass it _in_ as
8
+ * an argument so it becomes part of the cache key:
9
+ *
10
+ * @example
11
+ * ;```typescript
12
+ * import { getPreviewRef } from "@prismicio/next"
13
+ *
14
+ * const page = await fetchPage(uid, await getPreviewRef())
15
+ * ```
16
+ *
17
+ * @returns The active preview ref, or `undefined` if no preview session is active.
18
+ * @experimental
19
+ */
20
+ export async function getPreviewRef(): Promise<string | undefined> {
21
+ // Need this to avoid the following Next.js build-time error:
22
+ // You're importing a component that needs next/headers. That only works
23
+ // in a Server Component which is not supported in the pages/ directory.
24
+ const { cookies, draftMode } = await import("next/headers")
25
+
26
+ let isDraftModeEnabled = false
27
+ try {
28
+ isDraftModeEnabled = (await draftMode()).isEnabled
29
+ } catch {
30
+ // `draftMode()` may have been called in a place that
31
+ // does not have access to its async storage. This
32
+ // occurs in places like `generateStaticParams()`. We
33
+ // can ignore this case.
34
+ return
35
+ }
36
+ if (!isDraftModeEnabled) {
37
+ return
38
+ }
39
+
40
+ const cookie = (await cookies()).get(prismicCookie.preview)?.value
41
+ if (!cookie) {
42
+ return
43
+ }
44
+
45
+ const isActiveCookie = cookie.includes("websitePreviewId=")
46
+ if (!isActiveCookie) {
47
+ return
48
+ }
49
+
50
+ return cookie
51
+ }
@@ -0,0 +1,46 @@
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
+ * @experimental
11
+ *
12
+ * @see {@link https://nextjs.org/docs/app/api-reference/functions/cacheTag}
13
+ */
14
+ export function getPrismicCacheTags(pages: PrismicDocument[]): string[] {
15
+ const ids = [...new Set(getPrismicDocumentIDs(pages))]
16
+ return ids.map((id) => buildPrismicCacheTag(id))
17
+ }
18
+
19
+ export function buildPrismicCacheTag(id: string): string {
20
+ return `${CACHE_TAG_PREFIX}${id}`
21
+ }
22
+
23
+ function getPrismicDocumentIDs(input: unknown): string[] {
24
+ if (typeof input !== "object" || input == null) return []
25
+
26
+ if (Array.isArray(input)) return input.flatMap((item) => getPrismicDocumentIDs(item))
27
+
28
+ if ("id" in input && typeof input.id === "string") {
29
+ // Document
30
+ // Content relationship or link
31
+ if (
32
+ ("href" in input &&
33
+ typeof input.href === "string" &&
34
+ input.href.includes("/api/v2/documents/search?")) ||
35
+ ("link_type" in input && input.link_type === "Document")
36
+ ) {
37
+ if ("data" in input && typeof input.data === "object" && input.data !== null) {
38
+ return [input.id, ...getPrismicDocumentIDs(input.data)]
39
+ } else {
40
+ return [input.id]
41
+ }
42
+ }
43
+ }
44
+
45
+ return Object.values(input).flatMap((input) => getPrismicDocumentIDs(input))
46
+ }
package/src/getSlices.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { getDefaultSlices } from "@prismicio/simulator/kit"
2
2
  import type { StateEvents, StateEventType } from "@prismicio/simulator/kit"
3
- import { decompressFromEncodedURIComponent } from "lz-string"
3
+ import lzString from "lz-string"
4
4
 
5
5
  export const getSlices = (state: string | null | undefined): StateEvents[StateEventType.Slices] => {
6
- return state ? JSON.parse(decompressFromEncodedURIComponent(state)) : getDefaultSlices()
6
+ return state ? JSON.parse(lzString.decompressFromEncodedURIComponent(state)) : getDefaultSlices()
7
7
  }
package/src/index.ts CHANGED
@@ -9,6 +9,8 @@ export type { PrismicNextLinkProps } from "./PrismicNextLink"
9
9
  export { enableAutoPreviews } from "./enableAutoPreviews"
10
10
  export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews"
11
11
 
12
+ export { getPreviewRef } from "./getPreviewRef"
13
+
12
14
  export { redirectToPreviewURL } from "./redirectToPreviewURL"
13
15
  export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL"
14
16
 
@@ -26,3 +28,9 @@ export type { CreateClientConfig } from "./types"
26
28
 
27
29
  export { createLocaleRedirect } from "./createLocaleRedirect"
28
30
  export type { CreateLocaleRedirectConfig } from "./createLocaleRedirect"
31
+
32
+ export { getPrismicCacheTags } from "./getPrismicCacheTags"
33
+
34
+ export { cacheTagPrismicPages } from "./cacheTagPrismicPages"
35
+
36
+ export { revalidatePrismicPages } from "./revalidatePrismicPages"
@@ -1,5 +1,4 @@
1
1
  import { cookie as prismicCookie, type Client, type LinkResolverFunction } from "@prismicio/client"
2
- import { redirect } from "next/navigation"
3
2
 
4
3
  import type { NextRequestLike } from "./types"
5
4
 
@@ -41,6 +40,10 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
41
40
  // in a Server Component which is not supported in the pages/ directory.
42
41
  const { cookies, draftMode } = await import("next/headers")
43
42
 
43
+ // Dynamic so Node.js can load this module: the bare specifier (kept by
44
+ // tsdown.config.ts for Next.js's server alias) is not resolvable by Node.js.
45
+ const { redirect } = await import("next/navigation")
46
+
44
47
  const documentID = request.nextUrl.searchParams.get("documentId") ?? undefined
45
48
 
46
49
  // Set the initial preview cookie. Setting the cookie here is necessary
@@ -62,5 +65,5 @@ export async function redirectToPreviewURL(config: RedirectToPreviewURLConfig):
62
65
 
63
66
  ;(await draftMode()).enable()
64
67
 
65
- redirect(previewURL)
68
+ return redirect(previewURL)
66
69
  }
@@ -0,0 +1,19 @@
1
+ import { revalidateTag } from "next/cache"
2
+
3
+ import { buildPrismicCacheTag } from "./getPrismicCacheTags"
4
+
5
+ /**
6
+ * Immediately revalidates a set of Prismic pages given a list of their IDs. The list of IDs
7
+ * typically comes from a Prismic webhook triggered when publishing content.
8
+ *
9
+ * @param ids - A list of Prismic page IDs to revalidate.
10
+ * @experimental
11
+ *
12
+ * @see {@link https://nextjs.org/docs/app/api-reference/functions/revalidateTag}
13
+ */
14
+ export function revalidatePrismicPages(ids: string[]): void {
15
+ for (const id of ids) {
16
+ const tag = buildPrismicCacheTag(id)
17
+ revalidateTag(tag, { expire: 0 })
18
+ }
19
+ }