@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.
- package/dist/PrismicNextLink.d.cts +3 -3
- package/dist/PrismicNextLink.d.ts +3 -3
- package/dist/PrismicPreviewClient.cjs +2 -2
- package/dist/PrismicPreviewClient.cjs.map +1 -1
- package/dist/PrismicPreviewClient.js +1 -1
- package/dist/SliceSimulator.cjs +5 -4
- package/dist/SliceSimulator.cjs.map +1 -1
- package/dist/SliceSimulator.js +3 -3
- package/dist/SliceSimulator.js.map +1 -1
- package/dist/cacheTagPrismicPages.cjs +24 -0
- package/dist/cacheTagPrismicPages.cjs.map +1 -0
- package/dist/cacheTagPrismicPages.d.cts +19 -0
- package/dist/cacheTagPrismicPages.d.cts.map +1 -0
- package/dist/cacheTagPrismicPages.d.ts +19 -0
- package/dist/cacheTagPrismicPages.d.ts.map +1 -0
- package/dist/cacheTagPrismicPages.js +22 -0
- package/dist/cacheTagPrismicPages.js.map +1 -0
- package/dist/enableAutoPreviews.cjs +2 -16
- package/dist/enableAutoPreviews.cjs.map +1 -1
- package/dist/enableAutoPreviews.d.cts.map +1 -1
- package/dist/enableAutoPreviews.d.ts.map +1 -1
- package/dist/enableAutoPreviews.js +2 -15
- package/dist/enableAutoPreviews.js.map +1 -1
- package/dist/exitPreview.cjs +4 -1
- package/dist/exitPreview.cjs.map +1 -1
- package/dist/exitPreview.d.cts.map +1 -1
- package/dist/exitPreview.d.ts.map +1 -1
- package/dist/exitPreview.js +3 -1
- package/dist/exitPreview.js.map +1 -1
- package/dist/getPreviewRef.cjs +38 -0
- package/dist/getPreviewRef.cjs.map +1 -0
- package/dist/getPreviewRef.d.cts +22 -0
- package/dist/getPreviewRef.d.cts.map +1 -0
- package/dist/getPreviewRef.d.ts +22 -0
- package/dist/getPreviewRef.d.ts.map +1 -0
- package/dist/getPreviewRef.js +37 -0
- package/dist/getPreviewRef.js.map +1 -0
- package/dist/getPrismicCacheTags.cjs +31 -0
- package/dist/getPrismicCacheTags.cjs.map +1 -0
- package/dist/getPrismicCacheTags.d.cts +16 -0
- package/dist/getPrismicCacheTags.d.cts.map +1 -0
- package/dist/getPrismicCacheTags.d.ts +16 -0
- package/dist/getPrismicCacheTags.d.ts.map +1 -0
- package/dist/getPrismicCacheTags.js +30 -0
- package/dist/getPrismicCacheTags.js.map +1 -0
- package/dist/getSlices.cjs +3 -2
- package/dist/getSlices.cjs.map +1 -1
- package/dist/getSlices.js +2 -2
- package/dist/getSlices.js.map +1 -1
- package/dist/index.cjs +8 -0
- package/dist/index.d.cts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +5 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/redirectToPreviewURL.cjs +2 -2
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.d.cts.map +1 -1
- package/dist/redirectToPreviewURL.d.ts.map +1 -1
- package/dist/redirectToPreviewURL.js +2 -2
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/dist/revalidatePrismicPages.cjs +20 -0
- package/dist/revalidatePrismicPages.cjs.map +1 -0
- package/dist/revalidatePrismicPages.d.cts +14 -0
- package/dist/revalidatePrismicPages.d.cts.map +1 -0
- package/dist/revalidatePrismicPages.d.ts +14 -0
- package/dist/revalidatePrismicPages.d.ts.map +1 -0
- package/dist/revalidatePrismicPages.js +19 -0
- package/dist/revalidatePrismicPages.js.map +1 -0
- package/package.json +7 -6
- package/src/SliceSimulator.tsx +2 -2
- package/src/cacheTagPrismicPages.ts +21 -0
- package/src/enableAutoPreviews.ts +4 -33
- package/src/exitPreview.ts +7 -1
- package/src/getPreviewRef.ts +51 -0
- package/src/getPrismicCacheTags.ts +46 -0
- package/src/getSlices.ts +2 -2
- package/src/index.ts +8 -0
- package/src/redirectToPreviewURL.ts +5 -2
- 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
|
|
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
|
+
}
|