@prismicio/next 2.2.2 → 2.2.3-pr.136.19d76d2
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 +16 -20
- package/dist/PrismicNextImage.cjs.map +1 -1
- package/dist/PrismicNextImage.d.cts +14 -23
- package/dist/PrismicNextImage.d.cts.map +1 -0
- package/dist/PrismicNextImage.d.ts +14 -23
- package/dist/PrismicNextImage.d.ts.map +1 -0
- package/dist/PrismicNextImage.js +11 -15
- package/dist/PrismicNextImage.js.map +1 -1
- package/dist/PrismicNextLink.cjs +7 -8
- package/dist/PrismicNextLink.cjs.map +1 -1
- package/dist/PrismicNextLink.d.cts +10 -10
- package/dist/PrismicNextLink.d.cts.map +1 -0
- package/dist/PrismicNextLink.d.ts +10 -10
- package/dist/PrismicNextLink.d.ts.map +1 -0
- package/dist/PrismicNextLink.js +3 -4
- package/dist/PrismicNextLink.js.map +1 -1
- package/dist/PrismicPreview.cjs +13 -15
- package/dist/PrismicPreview.cjs.map +1 -1
- package/dist/PrismicPreview.d.cts +12 -14
- package/dist/PrismicPreview.d.cts.map +1 -0
- package/dist/PrismicPreview.d.ts +12 -14
- package/dist/PrismicPreview.d.ts.map +1 -0
- package/dist/PrismicPreview.js +8 -10
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/PrismicPreviewClient.cjs +5 -9
- package/dist/PrismicPreviewClient.cjs.map +1 -1
- package/dist/PrismicPreviewClient.js +6 -10
- package/dist/PrismicPreviewClient.js.map +1 -1
- package/dist/SliceSimulator.cjs +6 -8
- package/dist/SliceSimulator.cjs.map +1 -1
- package/dist/SliceSimulator.d.cts +3 -6
- package/dist/SliceSimulator.d.cts.map +1 -0
- package/dist/SliceSimulator.d.ts +3 -6
- package/dist/SliceSimulator.d.ts.map +1 -0
- package/dist/SliceSimulator.js +4 -6
- package/dist/SliceSimulator.js.map +1 -1
- package/dist/SliceSimulatorWrapper.cjs +3 -7
- package/dist/SliceSimulatorWrapper.cjs.map +1 -1
- package/dist/SliceSimulatorWrapper.js +2 -6
- package/dist/SliceSimulatorWrapper.js.map +1 -1
- package/dist/_virtual/{rolldown_runtime.cjs → _rolldown/runtime.cjs} +8 -14
- package/dist/createLocaleRedirect.cjs +2 -3
- 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 +1 -2
- package/dist/createLocaleRedirect.js.map +1 -1
- package/dist/enableAutoPreviews.cjs +4 -6
- package/dist/enableAutoPreviews.cjs.map +1 -1
- package/dist/enableAutoPreviews.d.cts +2 -4
- package/dist/enableAutoPreviews.d.cts.map +1 -0
- package/dist/enableAutoPreviews.d.ts +2 -4
- package/dist/enableAutoPreviews.d.ts.map +1 -0
- package/dist/enableAutoPreviews.js +3 -5
- package/dist/enableAutoPreviews.js.map +1 -1
- package/dist/exitPreview.cjs +11 -13
- 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 +11 -12
- package/dist/exitPreview.js.map +1 -1
- package/dist/getSlices.cjs +2 -3
- 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 +1 -2
- package/dist/getSlices.js.map +1 -1
- package/dist/imgixLoader.cjs +4 -5
- package/dist/imgixLoader.cjs.map +1 -1
- package/dist/imgixLoader.d.cts +3 -4
- package/dist/imgixLoader.d.cts.map +1 -0
- package/dist/imgixLoader.d.ts +3 -4
- package/dist/imgixLoader.d.ts.map +1 -0
- package/dist/imgixLoader.js +3 -4
- package/dist/imgixLoader.js.map +1 -1
- package/dist/index.cjs +12 -12
- package/dist/index.js +1 -2
- package/dist/lib/devMsg.cjs +8 -11
- package/dist/lib/devMsg.cjs.map +1 -1
- package/dist/lib/devMsg.js +7 -10
- package/dist/lib/devMsg.js.map +1 -1
- package/dist/lib/resolveDefaultExport.cjs +9 -14
- package/dist/lib/resolveDefaultExport.cjs.map +1 -1
- package/dist/lib/resolveDefaultExport.js +9 -13
- package/dist/lib/resolveDefaultExport.js.map +1 -1
- package/dist/package.cjs +7 -8
- package/dist/package.js +2 -2
- package/dist/pages/PrismicPreview.cjs +12 -14
- package/dist/pages/PrismicPreview.cjs.map +1 -1
- package/dist/pages/PrismicPreview.d.cts +12 -14
- package/dist/pages/PrismicPreview.d.cts.map +1 -0
- package/dist/pages/PrismicPreview.d.ts +12 -14
- package/dist/pages/PrismicPreview.d.ts.map +1 -0
- package/dist/pages/PrismicPreview.js +8 -10
- package/dist/pages/PrismicPreview.js.map +1 -1
- package/dist/pages/SliceSimulator.cjs +5 -6
- 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 +3 -4
- package/dist/pages/SliceSimulator.js.map +1 -1
- package/dist/pages/enableAutoPreviews.cjs +4 -5
- package/dist/pages/enableAutoPreviews.cjs.map +1 -1
- package/dist/pages/enableAutoPreviews.d.cts +6 -8
- package/dist/pages/enableAutoPreviews.d.cts.map +1 -0
- package/dist/pages/enableAutoPreviews.d.ts +6 -8
- package/dist/pages/enableAutoPreviews.d.ts.map +1 -0
- package/dist/pages/enableAutoPreviews.js +4 -4
- package/dist/pages/enableAutoPreviews.js.map +1 -1
- package/dist/pages/exitPreview.cjs +10 -12
- 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 +10 -11
- package/dist/pages/exitPreview.js.map +1 -1
- package/dist/pages/redirectToPreviewURL.cjs +1 -2
- package/dist/pages/redirectToPreviewURL.cjs.map +1 -1
- package/dist/pages/redirectToPreviewURL.d.cts +8 -11
- package/dist/pages/redirectToPreviewURL.d.cts.map +1 -0
- package/dist/pages/redirectToPreviewURL.d.ts +8 -11
- package/dist/pages/redirectToPreviewURL.d.ts.map +1 -0
- package/dist/pages/redirectToPreviewURL.js +1 -1
- package/dist/pages/redirectToPreviewURL.js.map +1 -1
- package/dist/pages/setPreviewData.cjs +2 -3
- 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 +1 -2
- package/dist/pages/setPreviewData.js.map +1 -1
- package/dist/pages/types.d.cts +3 -9
- package/dist/pages/types.d.cts.map +1 -0
- package/dist/pages/types.d.ts +3 -9
- package/dist/pages/types.d.ts.map +1 -0
- package/dist/pages.cjs +12 -12
- package/dist/pages.js +1 -2
- package/dist/redirectToPreviewURL.cjs +3 -4
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.d.cts +4 -7
- package/dist/redirectToPreviewURL.d.cts.map +1 -0
- package/dist/redirectToPreviewURL.d.ts +4 -7
- package/dist/redirectToPreviewURL.d.ts.map +1 -0
- package/dist/redirectToPreviewURL.js +2 -3
- package/dist/redirectToPreviewURL.js.map +1 -1
- 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 +34 -36
- 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/createLocaleRedirect.ts +3 -3
- package/src/enableAutoPreviews.ts +15 -16
- package/src/exitPreview.ts +12 -13
- package/src/getSlices.ts +6 -10
- package/src/imgixLoader.ts +10 -10
- package/src/index.ts +18 -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/types.ts +12 -13
package/src/PrismicNextLink.tsx
CHANGED
|
@@ -1,67 +1,65 @@
|
|
|
1
|
-
import type { ComponentProps} from "react";
|
|
2
|
-
import { forwardRef } from "react";
|
|
3
|
-
import Link from "next/link";
|
|
4
1
|
import type {
|
|
5
2
|
AsLinkAttrsConfig,
|
|
6
3
|
LinkField,
|
|
7
4
|
LinkResolverFunction,
|
|
8
|
-
PrismicDocument
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
PrismicDocument,
|
|
6
|
+
} from "@prismicio/client"
|
|
7
|
+
import { asLinkAttrs } from "@prismicio/client"
|
|
8
|
+
import Link from "next/link"
|
|
9
|
+
import type { ComponentProps } from "react"
|
|
10
|
+
import { forwardRef } from "react"
|
|
12
11
|
|
|
13
|
-
import { resolveDefaultExport } from "./lib/resolveDefaultExport"
|
|
12
|
+
import { resolveDefaultExport } from "./lib/resolveDefaultExport"
|
|
14
13
|
|
|
15
14
|
export type PrismicNextLinkProps = Omit<
|
|
16
15
|
ComponentProps<typeof Link>,
|
|
17
16
|
"field" | "document" | "href" | "rel"
|
|
18
17
|
> & {
|
|
19
|
-
linkResolver?: LinkResolverFunction
|
|
20
|
-
rel?: string | AsLinkAttrsConfig["rel"]
|
|
18
|
+
linkResolver?: LinkResolverFunction
|
|
19
|
+
rel?: string | AsLinkAttrsConfig["rel"]
|
|
21
20
|
} & (
|
|
22
21
|
| {
|
|
23
|
-
field: LinkField | null | undefined
|
|
24
|
-
document?: never
|
|
25
|
-
href?: never
|
|
22
|
+
field: LinkField | null | undefined
|
|
23
|
+
document?: never
|
|
24
|
+
href?: never
|
|
26
25
|
}
|
|
27
26
|
| {
|
|
28
|
-
field?: never
|
|
29
|
-
document: PrismicDocument | null | undefined
|
|
30
|
-
href?: never
|
|
27
|
+
field?: never
|
|
28
|
+
document: PrismicDocument | null | undefined
|
|
29
|
+
href?: never
|
|
31
30
|
}
|
|
32
31
|
| {
|
|
33
|
-
field?: never
|
|
34
|
-
document?: never
|
|
35
|
-
href: ComponentProps<typeof Link>["href"]
|
|
32
|
+
field?: never
|
|
33
|
+
document?: never
|
|
34
|
+
href: ComponentProps<typeof Link>["href"]
|
|
36
35
|
}
|
|
37
|
-
)
|
|
36
|
+
)
|
|
38
37
|
|
|
39
|
-
export const PrismicNextLink = forwardRef<
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
rel: typeof restProps.rel === "function" ? restProps.rel : undefined,
|
|
51
|
-
});
|
|
38
|
+
export const PrismicNextLink = forwardRef<HTMLAnchorElement, PrismicNextLinkProps>(
|
|
39
|
+
function PrismicNextLink(props, ref) {
|
|
40
|
+
const { field, document, linkResolver, children, ...restProps } = props
|
|
41
|
+
const {
|
|
42
|
+
href: computedHref,
|
|
43
|
+
rel: computedRel,
|
|
44
|
+
...attrs
|
|
45
|
+
} = asLinkAttrs(field ?? document, {
|
|
46
|
+
linkResolver,
|
|
47
|
+
rel: typeof restProps.rel === "function" ? restProps.rel : undefined,
|
|
48
|
+
})
|
|
52
49
|
|
|
53
|
-
|
|
50
|
+
const href = ("href" in restProps ? restProps.href : computedHref) || ""
|
|
54
51
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
let rel = computedRel
|
|
53
|
+
if ("rel" in restProps && typeof restProps.rel !== "function") {
|
|
54
|
+
rel = restProps.rel
|
|
55
|
+
}
|
|
59
56
|
|
|
60
|
-
|
|
57
|
+
const ResolvedLink = resolveDefaultExport(Link)
|
|
61
58
|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
59
|
+
return (
|
|
60
|
+
<ResolvedLink ref={ref} {...attrs} {...restProps} href={href} rel={rel}>
|
|
61
|
+
{"children" in props ? children : field?.text}
|
|
62
|
+
</ResolvedLink>
|
|
63
|
+
)
|
|
64
|
+
},
|
|
65
|
+
)
|
package/src/PrismicPreview.tsx
CHANGED
|
@@ -1,55 +1,54 @@
|
|
|
1
|
-
import
|
|
2
|
-
import Script from "next/script"
|
|
3
|
-
import {
|
|
1
|
+
import { getToolbarSrc } from "@prismicio/client"
|
|
2
|
+
import Script from "next/script"
|
|
3
|
+
import type { FC, ReactNode } from "react"
|
|
4
4
|
|
|
5
|
-
import { resolveDefaultExport } from "./lib/resolveDefaultExport"
|
|
6
|
-
import { PrismicPreviewClient } from "./PrismicPreviewClient"
|
|
5
|
+
import { resolveDefaultExport } from "./lib/resolveDefaultExport"
|
|
6
|
+
import { PrismicPreviewClient } from "./PrismicPreviewClient"
|
|
7
7
|
|
|
8
8
|
/** Props for `<PrismicPreview>`. */
|
|
9
9
|
export type PrismicPreviewProps = {
|
|
10
10
|
/**
|
|
11
|
-
* The name of your Prismic repository. A Prismic Toolbar will be registered
|
|
12
|
-
*
|
|
11
|
+
* The name of your Prismic repository. A Prismic Toolbar will be registered using this
|
|
12
|
+
* repository.
|
|
13
13
|
*/
|
|
14
|
-
repositoryName: string
|
|
14
|
+
repositoryName: string
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
|
-
* The URL of your app's Prismic preview endpoint (default: `/api/preview`).
|
|
18
|
-
*
|
|
17
|
+
* The URL of your app's Prismic preview endpoint (default: `/api/preview`). This URL will be
|
|
18
|
+
* fetched on preview update events.
|
|
19
19
|
*/
|
|
20
|
-
updatePreviewURL?: string
|
|
20
|
+
updatePreviewURL?: string
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`).
|
|
24
|
-
*
|
|
23
|
+
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`). This URL will be
|
|
24
|
+
* fetched on preview exit events.
|
|
25
25
|
*/
|
|
26
|
-
exitPreviewURL?: string
|
|
26
|
+
exitPreviewURL?: string
|
|
27
27
|
|
|
28
28
|
/** Children to render adjacent to the Prismic Toolbar. */
|
|
29
|
-
children?: ReactNode
|
|
30
|
-
}
|
|
29
|
+
children?: ReactNode
|
|
30
|
+
}
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
* React component that sets up Prismic Previews using the Prismic Toolbar. When
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* changes.
|
|
33
|
+
* React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar
|
|
34
|
+
* send events to the browser, such as on preview updates and exiting, this component will
|
|
35
|
+
* automatically refresh the page with the changes.
|
|
37
36
|
*
|
|
38
|
-
* This component can be wrapped around your app or added anywhere in your app's
|
|
39
|
-
*
|
|
37
|
+
* This component can be wrapped around your app or added anywhere in your app's tree. It must be
|
|
38
|
+
* rendered on every page.
|
|
40
39
|
*/
|
|
41
40
|
export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
|
|
42
|
-
const { repositoryName, children, ...otherProps } = props
|
|
41
|
+
const { repositoryName, children, ...otherProps } = props
|
|
43
42
|
|
|
44
43
|
// Need this to avoid the following Next.js build-time error:
|
|
45
44
|
// You're importing a component that needs next/headers. That only works
|
|
46
45
|
// in a Server Component which is not supported in the pages/ directory.
|
|
47
|
-
const { draftMode } = await import("next/headers")
|
|
46
|
+
const { draftMode } = await import("next/headers")
|
|
48
47
|
|
|
49
|
-
const toolbarSrc = getToolbarSrc(repositoryName)
|
|
50
|
-
const isDraftMode = (await draftMode()).isEnabled
|
|
48
|
+
const toolbarSrc = getToolbarSrc(repositoryName)
|
|
49
|
+
const isDraftMode = (await draftMode()).isEnabled
|
|
51
50
|
|
|
52
|
-
const ResolvedScript = resolveDefaultExport(Script)
|
|
51
|
+
const ResolvedScript = resolveDefaultExport(Script)
|
|
53
52
|
|
|
54
53
|
return (
|
|
55
54
|
<>
|
|
@@ -61,5 +60,5 @@ export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
|
|
|
61
60
|
/>
|
|
62
61
|
<ResolvedScript src={toolbarSrc} strategy="lazyOnload" />
|
|
63
62
|
</>
|
|
64
|
-
)
|
|
65
|
-
}
|
|
63
|
+
)
|
|
64
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -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
|
}
|