@prismicio/next 2.2.3 → 2.2.4-pr.114.343dd9b
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/PrismicNextImage.cjs +9 -11
- package/dist/PrismicNextImage.cjs.map +1 -1
- package/dist/PrismicNextImage.d.cts +13 -21
- package/dist/PrismicNextImage.d.cts.map +1 -0
- package/dist/PrismicNextImage.d.ts +13 -21
- package/dist/PrismicNextImage.d.ts.map +1 -0
- package/dist/PrismicNextImage.js +9 -11
- package/dist/PrismicNextImage.js.map +1 -1
- package/dist/PrismicNextLink.cjs.map +1 -1
- package/dist/PrismicNextLink.d.cts +3 -3
- package/dist/PrismicNextLink.d.cts.map +1 -0
- package/dist/PrismicNextLink.d.ts +3 -3
- package/dist/PrismicNextLink.d.ts.map +1 -0
- package/dist/PrismicNextLink.js.map +1 -1
- package/dist/PrismicPreview.cjs +6 -7
- package/dist/PrismicPreview.cjs.map +1 -1
- package/dist/PrismicPreview.d.cts +11 -12
- package/dist/PrismicPreview.d.cts.map +1 -0
- package/dist/PrismicPreview.d.ts +11 -12
- package/dist/PrismicPreview.d.ts.map +1 -0
- package/dist/PrismicPreview.js +6 -7
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/PrismicPreviewClient.cjs +1 -3
- package/dist/PrismicPreviewClient.cjs.map +1 -1
- package/dist/PrismicPreviewClient.js +1 -3
- package/dist/PrismicPreviewClient.js.map +1 -1
- package/dist/SliceSimulator.cjs +3 -3
- package/dist/SliceSimulator.cjs.map +1 -1
- package/dist/SliceSimulator.d.cts +3 -5
- package/dist/SliceSimulator.d.cts.map +1 -0
- package/dist/SliceSimulator.d.ts +3 -5
- package/dist/SliceSimulator.d.ts.map +1 -0
- package/dist/SliceSimulator.js +3 -3
- package/dist/SliceSimulator.js.map +1 -1
- package/dist/SliceSimulatorWrapper.cjs +1 -4
- package/dist/SliceSimulatorWrapper.cjs.map +1 -1
- package/dist/SliceSimulatorWrapper.js +1 -4
- package/dist/SliceSimulatorWrapper.js.map +1 -1
- package/dist/cacheTagPrismicPages.cjs +23 -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/createLocaleRedirect.cjs.map +1 -1
- package/dist/createLocaleRedirect.d.cts.map +1 -0
- package/dist/createLocaleRedirect.d.ts.map +1 -0
- package/dist/createLocaleRedirect.js.map +1 -1
- package/dist/enableAutoPreviews.cjs +3 -18
- package/dist/enableAutoPreviews.cjs.map +1 -1
- package/dist/enableAutoPreviews.d.cts +1 -2
- package/dist/enableAutoPreviews.d.cts.map +1 -0
- package/dist/enableAutoPreviews.d.ts +1 -2
- package/dist/enableAutoPreviews.d.ts.map +1 -0
- package/dist/enableAutoPreviews.js +3 -17
- package/dist/enableAutoPreviews.js.map +1 -1
- package/dist/exitPreview.cjs +13 -11
- package/dist/exitPreview.cjs.map +1 -1
- package/dist/exitPreview.d.cts +9 -10
- package/dist/exitPreview.d.cts.map +1 -0
- package/dist/exitPreview.d.ts +9 -10
- package/dist/exitPreview.d.ts.map +1 -0
- package/dist/exitPreview.js +12 -11
- 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.map +1 -1
- package/dist/getSlices.d.cts.map +1 -0
- package/dist/getSlices.d.ts.map +1 -0
- package/dist/getSlices.js.map +1 -1
- package/dist/imgixLoader.cjs +2 -2
- package/dist/imgixLoader.cjs.map +1 -1
- package/dist/imgixLoader.d.cts +2 -2
- package/dist/imgixLoader.d.cts.map +1 -0
- package/dist/imgixLoader.d.ts +2 -2
- package/dist/imgixLoader.d.ts.map +1 -0
- package/dist/imgixLoader.js +2 -2
- package/dist/imgixLoader.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/lib/devMsg.cjs +6 -8
- package/dist/lib/devMsg.cjs.map +1 -1
- package/dist/lib/devMsg.js +6 -8
- package/dist/lib/devMsg.js.map +1 -1
- package/dist/lib/resolveDefaultExport.cjs +8 -12
- package/dist/lib/resolveDefaultExport.cjs.map +1 -1
- package/dist/lib/resolveDefaultExport.js +8 -12
- package/dist/lib/resolveDefaultExport.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/dist/pages/PrismicPreview.cjs +6 -7
- package/dist/pages/PrismicPreview.cjs.map +1 -1
- package/dist/pages/PrismicPreview.d.cts +11 -12
- package/dist/pages/PrismicPreview.d.cts.map +1 -0
- package/dist/pages/PrismicPreview.d.ts +11 -12
- package/dist/pages/PrismicPreview.d.ts.map +1 -0
- package/dist/pages/PrismicPreview.js +6 -7
- package/dist/pages/PrismicPreview.js.map +1 -1
- package/dist/pages/SliceSimulator.cjs +2 -2
- package/dist/pages/SliceSimulator.cjs.map +1 -1
- package/dist/pages/SliceSimulator.d.cts +10 -13
- package/dist/pages/SliceSimulator.d.cts.map +1 -0
- package/dist/pages/SliceSimulator.d.ts +10 -13
- package/dist/pages/SliceSimulator.d.ts.map +1 -0
- package/dist/pages/SliceSimulator.js +2 -2
- package/dist/pages/SliceSimulator.js.map +1 -1
- package/dist/pages/enableAutoPreviews.cjs +3 -3
- package/dist/pages/enableAutoPreviews.cjs.map +1 -1
- package/dist/pages/enableAutoPreviews.d.cts +5 -5
- package/dist/pages/enableAutoPreviews.d.cts.map +1 -0
- package/dist/pages/enableAutoPreviews.d.ts +5 -5
- package/dist/pages/enableAutoPreviews.d.ts.map +1 -0
- package/dist/pages/enableAutoPreviews.js +3 -3
- package/dist/pages/enableAutoPreviews.js.map +1 -1
- package/dist/pages/exitPreview.cjs +9 -10
- package/dist/pages/exitPreview.cjs.map +1 -1
- package/dist/pages/exitPreview.d.cts +9 -10
- package/dist/pages/exitPreview.d.cts.map +1 -0
- package/dist/pages/exitPreview.d.ts +9 -10
- package/dist/pages/exitPreview.d.ts.map +1 -0
- package/dist/pages/exitPreview.js +9 -10
- package/dist/pages/exitPreview.js.map +1 -1
- package/dist/pages/redirectToPreviewURL.cjs.map +1 -1
- package/dist/pages/redirectToPreviewURL.d.cts +7 -9
- package/dist/pages/redirectToPreviewURL.d.cts.map +1 -0
- package/dist/pages/redirectToPreviewURL.d.ts +7 -9
- package/dist/pages/redirectToPreviewURL.d.ts.map +1 -0
- package/dist/pages/redirectToPreviewURL.js.map +1 -1
- package/dist/pages/setPreviewData.cjs.map +1 -1
- package/dist/pages/setPreviewData.d.cts.map +1 -0
- package/dist/pages/setPreviewData.d.ts.map +1 -0
- package/dist/pages/setPreviewData.js.map +1 -1
- package/dist/pages/types.d.cts +3 -8
- package/dist/pages/types.d.cts.map +1 -0
- package/dist/pages/types.d.ts +3 -8
- package/dist/pages/types.d.ts.map +1 -0
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.d.cts +3 -5
- package/dist/redirectToPreviewURL.d.cts.map +1 -0
- package/dist/redirectToPreviewURL.d.ts +3 -5
- package/dist/redirectToPreviewURL.d.ts.map +1 -0
- 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/dist/types.d.cts +1 -2
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +1 -2
- package/dist/types.d.ts.map +1 -0
- package/package.json +32 -34
- package/src/PrismicNextImage.tsx +113 -134
- package/src/PrismicNextLink.tsx +43 -45
- package/src/PrismicPreview.tsx +28 -29
- package/src/PrismicPreviewClient.tsx +42 -44
- package/src/SliceSimulator.tsx +38 -43
- package/src/SliceSimulatorWrapper.tsx +15 -25
- package/src/cacheTagPrismicPages.ts +20 -0
- package/src/createLocaleRedirect.ts +3 -3
- package/src/enableAutoPreviews.ts +7 -37
- package/src/exitPreview.ts +18 -13
- package/src/getPreviewRef.ts +51 -0
- package/src/getPrismicCacheTags.ts +46 -0
- package/src/getSlices.ts +6 -10
- package/src/imgixLoader.ts +10 -10
- package/src/index.ts +26 -21
- package/src/lib/devMsg.ts +9 -11
- package/src/lib/resolveDefaultExport.ts +9 -13
- package/src/pages/PrismicPreview.tsx +47 -51
- package/src/pages/SliceSimulator.tsx +35 -37
- package/src/pages/enableAutoPreviews.ts +17 -17
- package/src/pages/exitPreview.ts +17 -18
- package/src/pages/index.ts +20 -23
- package/src/pages/redirectToPreviewURL.ts +22 -33
- package/src/pages/setPreviewData.ts +7 -7
- package/src/pages/types.ts +16 -20
- package/src/redirectToPreviewURL.ts +21 -30
- package/src/revalidatePrismicPages.ts +19 -0
- package/src/types.ts +12 -13
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
"use client"
|
|
1
|
+
"use client"
|
|
2
2
|
|
|
3
|
-
import { cookie as prismicCookie } from "@prismicio/client"
|
|
4
|
-
import { useRouter } from "next/navigation"
|
|
5
|
-
import type { FC } from "react"
|
|
6
|
-
import { useEffect } from "react"
|
|
3
|
+
import { cookie as prismicCookie } from "@prismicio/client"
|
|
4
|
+
import { useRouter } from "next/navigation"
|
|
5
|
+
import type { FC } from "react"
|
|
6
|
+
import { useEffect } from "react"
|
|
7
7
|
|
|
8
8
|
type PrismicPreviewClientProps = {
|
|
9
|
-
repositoryName: string
|
|
10
|
-
isDraftMode: boolean
|
|
11
|
-
updatePreviewURL?: string
|
|
12
|
-
exitPreviewURL?: string
|
|
13
|
-
}
|
|
9
|
+
repositoryName: string
|
|
10
|
+
isDraftMode: boolean
|
|
11
|
+
updatePreviewURL?: string
|
|
12
|
+
exitPreviewURL?: string
|
|
13
|
+
}
|
|
14
14
|
|
|
15
15
|
export const PrismicPreviewClient: FC<PrismicPreviewClientProps> = (props) => {
|
|
16
16
|
const {
|
|
@@ -18,25 +18,25 @@ export const PrismicPreviewClient: FC<PrismicPreviewClientProps> = (props) => {
|
|
|
18
18
|
isDraftMode,
|
|
19
19
|
updatePreviewURL = "/api/preview",
|
|
20
20
|
exitPreviewURL = "/api/exit-preview",
|
|
21
|
-
} = props
|
|
21
|
+
} = props
|
|
22
22
|
|
|
23
|
-
const { refresh } = useRouter()
|
|
23
|
+
const { refresh } = useRouter()
|
|
24
24
|
|
|
25
25
|
useEffect(() => {
|
|
26
|
-
const controller = new AbortController()
|
|
26
|
+
const controller = new AbortController()
|
|
27
27
|
|
|
28
28
|
window.addEventListener("prismicPreviewUpdate", onUpdate, {
|
|
29
29
|
signal: controller.signal,
|
|
30
|
-
})
|
|
30
|
+
})
|
|
31
31
|
window.addEventListener("prismicPreviewEnd", onEnd, {
|
|
32
32
|
signal: controller.signal,
|
|
33
|
-
})
|
|
33
|
+
})
|
|
34
34
|
|
|
35
|
-
const cookie = getPrismicPreviewCookie(window.document.cookie)
|
|
35
|
+
const cookie = getPrismicPreviewCookie(window.document.cookie)
|
|
36
36
|
const cookieRepositoryName = cookie
|
|
37
37
|
? (decodeURIComponent(cookie).match(/"([^"]+)\.prismic\.io"/) || [])[1]
|
|
38
|
-
: undefined
|
|
39
|
-
const hasCookieForRepository = cookieRepositoryName === repositoryName
|
|
38
|
+
: undefined
|
|
39
|
+
const hasCookieForRepository = cookieRepositoryName === repositoryName
|
|
40
40
|
|
|
41
41
|
// Start the preview for preview share links. Previews from
|
|
42
42
|
// share links do not go to the `updatePreviewURL` like a normal
|
|
@@ -55,75 +55,73 @@ export const PrismicPreviewClient: FC<PrismicPreviewClientProps> = (props) => {
|
|
|
55
55
|
if (res.type !== "opaqueredirect") {
|
|
56
56
|
console.error(
|
|
57
57
|
`[<PrismicPreview>] Failed to start the preview using "${updatePreviewURL}". Does it exist?`,
|
|
58
|
-
)
|
|
58
|
+
)
|
|
59
59
|
|
|
60
|
-
return
|
|
60
|
+
return
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
refresh()
|
|
63
|
+
refresh()
|
|
64
64
|
})
|
|
65
65
|
.catch(() => {
|
|
66
66
|
// noop
|
|
67
|
-
})
|
|
67
|
+
})
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
function onUpdate(event: Event) {
|
|
71
|
-
event.preventDefault()
|
|
72
|
-
refresh()
|
|
71
|
+
event.preventDefault()
|
|
72
|
+
refresh()
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
function onEnd(event: Event) {
|
|
76
|
-
event.preventDefault()
|
|
76
|
+
event.preventDefault()
|
|
77
77
|
globalThis
|
|
78
78
|
.fetch(exitPreviewURL, { signal: controller.signal })
|
|
79
79
|
.then((res) => {
|
|
80
80
|
if (!res.ok) {
|
|
81
81
|
console.error(
|
|
82
82
|
`[<PrismicPreview>] Failed to exit Preview Mode using the "${exitPreviewURL}" API endpoint. Does it exist?`,
|
|
83
|
-
)
|
|
83
|
+
)
|
|
84
84
|
|
|
85
|
-
return
|
|
85
|
+
return
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
refresh()
|
|
88
|
+
refresh()
|
|
89
89
|
})
|
|
90
90
|
.catch(() => {
|
|
91
91
|
// noop
|
|
92
|
-
})
|
|
92
|
+
})
|
|
93
93
|
}
|
|
94
94
|
|
|
95
|
-
return () => controller.abort()
|
|
96
|
-
}, [repositoryName, isDraftMode, updatePreviewURL, exitPreviewURL, refresh])
|
|
95
|
+
return () => controller.abort()
|
|
96
|
+
}, [repositoryName, isDraftMode, updatePreviewURL, exitPreviewURL, refresh])
|
|
97
97
|
|
|
98
|
-
return null
|
|
99
|
-
}
|
|
98
|
+
return null
|
|
99
|
+
}
|
|
100
100
|
|
|
101
101
|
/**
|
|
102
102
|
* Returns the value of a cookie from a given cookie store.
|
|
103
103
|
*
|
|
104
|
-
* @param cookieJar - The stringified cookie store from which to read the
|
|
105
|
-
* cookie.
|
|
106
|
-
*
|
|
104
|
+
* @param cookieJar - The stringified cookie store from which to read the cookie.
|
|
107
105
|
* @returns The value of the cookie, if it exists.
|
|
108
106
|
*/
|
|
109
107
|
function getPrismicPreviewCookie(cookieJar: string): string | undefined {
|
|
110
108
|
function readValue(value: string): string {
|
|
111
|
-
return value.replace(/%3B/g, ";")
|
|
109
|
+
return value.replace(/%3B/g, ";")
|
|
112
110
|
}
|
|
113
111
|
|
|
114
|
-
const cookies = cookieJar.split("; ")
|
|
112
|
+
const cookies = cookieJar.split("; ")
|
|
115
113
|
|
|
116
|
-
let value: string | undefined
|
|
114
|
+
let value: string | undefined
|
|
117
115
|
|
|
118
116
|
for (const cookie of cookies) {
|
|
119
|
-
const parts = cookie.split("=")
|
|
120
|
-
const name = readValue(parts[0]).replace(/%3D/g, "=")
|
|
117
|
+
const parts = cookie.split("=")
|
|
118
|
+
const name = readValue(parts[0]).replace(/%3D/g, "=")
|
|
121
119
|
|
|
122
120
|
if (name === prismicCookie.preview) {
|
|
123
|
-
value = readValue(parts.slice(1).join("="))
|
|
124
|
-
continue
|
|
121
|
+
value = readValue(parts.slice(1).join("="))
|
|
122
|
+
continue
|
|
125
123
|
}
|
|
126
124
|
}
|
|
127
125
|
|
|
128
|
-
return value
|
|
126
|
+
return value
|
|
129
127
|
}
|
package/src/SliceSimulator.tsx
CHANGED
|
@@ -1,39 +1,34 @@
|
|
|
1
|
-
"use client"
|
|
1
|
+
"use client"
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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
|
-
|
|
10
|
+
import { getSlices } from "./getSlices"
|
|
11
|
+
import { SliceSimulatorWrapper } from "./SliceSimulatorWrapper"
|
|
17
12
|
|
|
18
|
-
const
|
|
13
|
+
const STATE_PARAMS_KEY = "state"
|
|
19
14
|
|
|
20
|
-
|
|
21
|
-
|
|
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
|
-
*
|
|
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,20 @@
|
|
|
1
|
+
import type { PrismicDocument } from "@prismicio/client"
|
|
2
|
+
import { cacheTag } from "next/cache"
|
|
3
|
+
|
|
4
|
+
import { getPrismicCacheTags } from "./getPrismicCacheTags"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Tags the current cache entry with cache tags for a set of Prismic pages so they can be
|
|
8
|
+
* revalidated when their content changes.
|
|
9
|
+
*
|
|
10
|
+
* Linked documents (via content relationships) are tagged as well, so the cache entry is
|
|
11
|
+
* revalidated when any of its nested documents change.
|
|
12
|
+
*
|
|
13
|
+
* @param pages - A set of Prismic pages used to tag the cache entry.
|
|
14
|
+
* @experimental
|
|
15
|
+
*
|
|
16
|
+
* @see {@link https://nextjs.org/docs/app/api-reference/functions/cacheTag}
|
|
17
|
+
*/
|
|
18
|
+
export function cacheTagPrismicPages(pages: PrismicDocument[]): void {
|
|
19
|
+
cacheTag(...getPrismicCacheTags(pages))
|
|
20
|
+
}
|
|
@@ -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 {
|
|
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(
|
|
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
|
}
|
package/src/exitPreview.ts
CHANGED
|
@@ -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
|
-
*
|
|
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
|
-
*
|
|
8
|
-
* // src/app/api/exit-preview/route.js
|
|
9
|
-
*
|
|
10
|
-
* import { exitPreview } from "@prismicio/next";
|
|
11
|
+
* import { exitPreview } from "@prismicio/next"
|
|
11
12
|
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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
|
-
|
|
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,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,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
|
|
7
|
-
|
|
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
|
+
}
|
package/src/imgixLoader.ts
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
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
|
-
*
|
|
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
|
+
}
|