@prismicio/next 2.2.2 → 2.2.3-canary.1abb259
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/exitPreview.ts
CHANGED
|
@@ -1,31 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Ends a Prismic preview session within a Next.js app. This function should be
|
|
3
|
-
*
|
|
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
|
-
*
|
|
8
|
-
* // src/app/api/exit-preview/route.js
|
|
9
|
+
* import { exitPreview } from "@prismicio/next"
|
|
9
10
|
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
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
|
}
|
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
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,31 +1,28 @@
|
|
|
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"
|
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
|
-
*
|
|
9
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
8
|
-
*
|
|
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
|
-
*
|
|
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
|
}
|
|
@@ -1,41 +1,40 @@
|
|
|
1
|
-
import { getToolbarSrc, cookie as prismicCookie } from "@prismicio/client"
|
|
2
|
-
import { useRouter } from "next/router"
|
|
3
|
-
import Script from "next/script"
|
|
4
|
-
import type { FC } from "react"
|
|
5
|
-
import { type ReactNode, useEffect } from "react"
|
|
1
|
+
import { getToolbarSrc, cookie as prismicCookie } from "@prismicio/client"
|
|
2
|
+
import { useRouter } from "next/router"
|
|
3
|
+
import Script from "next/script"
|
|
4
|
+
import type { FC } from "react"
|
|
5
|
+
import { type ReactNode, useEffect } from "react"
|
|
6
6
|
|
|
7
7
|
/** Props for `<PrismicPreview>`. */
|
|
8
8
|
export type PrismicPreviewProps = {
|
|
9
9
|
/**
|
|
10
|
-
* The name of your Prismic repository. A Prismic Toolbar will be registered
|
|
11
|
-
*
|
|
10
|
+
* The name of your Prismic repository. A Prismic Toolbar will be registered using this
|
|
11
|
+
* repository.
|
|
12
12
|
*/
|
|
13
|
-
repositoryName: string
|
|
13
|
+
repositoryName: string
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* The URL of your app's Prismic preview endpoint (default: `/api/preview`).
|
|
17
|
-
*
|
|
16
|
+
* The URL of your app's Prismic preview endpoint (default: `/api/preview`). This URL will be
|
|
17
|
+
* fetched on preview update events.
|
|
18
18
|
*/
|
|
19
|
-
updatePreviewURL?: string
|
|
19
|
+
updatePreviewURL?: string
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
|
-
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`).
|
|
23
|
-
*
|
|
22
|
+
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`). This URL will be
|
|
23
|
+
* fetched on preview exit events.
|
|
24
24
|
*/
|
|
25
|
-
exitPreviewURL?: string
|
|
25
|
+
exitPreviewURL?: string
|
|
26
26
|
|
|
27
27
|
/** Children to render adjacent to the Prismic Toolbar. */
|
|
28
|
-
children?: ReactNode
|
|
29
|
-
}
|
|
28
|
+
children?: ReactNode
|
|
29
|
+
}
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
* React component that sets up Prismic Previews using the Prismic Toolbar. When
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
* changes.
|
|
32
|
+
* React component that sets up Prismic Previews using the Prismic Toolbar. When the Prismic Toolbar
|
|
33
|
+
* send events to the browser, such as on preview updates and exiting, this component will
|
|
34
|
+
* automatically refresh the page with the changes.
|
|
36
35
|
*
|
|
37
|
-
* This component can be wrapped around your app or added anywhere in your app's
|
|
38
|
-
*
|
|
36
|
+
* This component can be wrapped around your app or added anywhere in your app's tree. It must be
|
|
37
|
+
* rendered on every page.
|
|
39
38
|
*/
|
|
40
39
|
export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
41
40
|
const {
|
|
@@ -43,21 +42,21 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
43
42
|
updatePreviewURL = "/api/preview",
|
|
44
43
|
exitPreviewURL = "/api/exit-preview",
|
|
45
44
|
children,
|
|
46
|
-
} = props
|
|
45
|
+
} = props
|
|
47
46
|
|
|
48
|
-
const router = useRouter()
|
|
47
|
+
const router = useRouter()
|
|
49
48
|
|
|
50
|
-
const toolbarSrc = getToolbarSrc(repositoryName)
|
|
49
|
+
const toolbarSrc = getToolbarSrc(repositoryName)
|
|
51
50
|
|
|
52
51
|
useEffect(() => {
|
|
53
|
-
const controller = new AbortController()
|
|
52
|
+
const controller = new AbortController()
|
|
54
53
|
|
|
55
54
|
window.addEventListener("prismicPreviewUpdate", onUpdate, {
|
|
56
55
|
signal: controller.signal,
|
|
57
|
-
})
|
|
56
|
+
})
|
|
58
57
|
window.addEventListener("prismicPreviewEnd", onEnd, {
|
|
59
58
|
signal: controller.signal,
|
|
60
|
-
})
|
|
59
|
+
})
|
|
61
60
|
|
|
62
61
|
// Start the preview for preview share links. Previews from
|
|
63
62
|
// share links do not go to the `updatePreviewURL` like a normal
|
|
@@ -66,35 +65,33 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
66
65
|
// We check that the current URL is a descendant of the base
|
|
67
66
|
// path to prevent infinite refrehes.
|
|
68
67
|
if (
|
|
69
|
-
window.location.href.startsWith(
|
|
70
|
-
window.location.origin + router.basePath,
|
|
71
|
-
) &&
|
|
68
|
+
window.location.href.startsWith(window.location.origin + router.basePath) &&
|
|
72
69
|
getPreviewCookieRepositoryName() === repositoryName &&
|
|
73
70
|
!router.isPreview
|
|
74
71
|
) {
|
|
75
|
-
start()
|
|
72
|
+
start()
|
|
76
73
|
}
|
|
77
74
|
|
|
78
75
|
function onEnd(event: Event) {
|
|
79
|
-
event.preventDefault()
|
|
76
|
+
event.preventDefault()
|
|
80
77
|
fetch(router.basePath + exitPreviewURL, { signal: controller.signal })
|
|
81
78
|
.then((res) => {
|
|
82
79
|
if (!res.ok) {
|
|
83
80
|
console.error(
|
|
84
81
|
`[<PrismicPreview>] Failed to exit Preview Mode using the "${exitPreviewURL}" API endpoint. Does it exist?`,
|
|
85
|
-
)
|
|
82
|
+
)
|
|
86
83
|
|
|
87
|
-
return
|
|
84
|
+
return
|
|
88
85
|
}
|
|
89
86
|
|
|
90
|
-
refresh()
|
|
87
|
+
refresh()
|
|
91
88
|
})
|
|
92
|
-
.catch(() => {})
|
|
89
|
+
.catch(() => {})
|
|
93
90
|
}
|
|
94
91
|
|
|
95
92
|
function onUpdate(event: Event) {
|
|
96
|
-
event.preventDefault()
|
|
97
|
-
start()
|
|
93
|
+
event.preventDefault()
|
|
94
|
+
start()
|
|
98
95
|
}
|
|
99
96
|
|
|
100
97
|
function start() {
|
|
@@ -110,37 +107,36 @@ export const PrismicPreview: FC<PrismicPreviewProps> = (props) => {
|
|
|
110
107
|
if (res.type !== "opaqueredirect") {
|
|
111
108
|
console.error(
|
|
112
109
|
`[<PrismicPreview>] Failed to start or update the preview using "${updatePreviewURL}". Does it exist?`,
|
|
113
|
-
)
|
|
110
|
+
)
|
|
114
111
|
|
|
115
|
-
return
|
|
112
|
+
return
|
|
116
113
|
}
|
|
117
114
|
|
|
118
|
-
refresh()
|
|
115
|
+
refresh()
|
|
119
116
|
})
|
|
120
|
-
.catch(() => {})
|
|
117
|
+
.catch(() => {})
|
|
121
118
|
}
|
|
122
119
|
|
|
123
120
|
function refresh() {
|
|
124
|
-
router.replace(router.asPath, undefined, { scroll: false })
|
|
121
|
+
router.replace(router.asPath, undefined, { scroll: false })
|
|
125
122
|
}
|
|
126
123
|
|
|
127
|
-
return () => controller.abort()
|
|
128
|
-
}, [exitPreviewURL, updatePreviewURL, repositoryName, router])
|
|
124
|
+
return () => controller.abort()
|
|
125
|
+
}, [exitPreviewURL, updatePreviewURL, repositoryName, router])
|
|
129
126
|
|
|
130
127
|
return (
|
|
131
128
|
<>
|
|
132
129
|
{children}
|
|
133
130
|
<Script src={toolbarSrc} strategy="lazyOnload" />
|
|
134
131
|
</>
|
|
135
|
-
)
|
|
136
|
-
}
|
|
132
|
+
)
|
|
133
|
+
}
|
|
137
134
|
|
|
138
135
|
function getPreviewCookieRepositoryName() {
|
|
139
136
|
const cookie = window.document.cookie
|
|
140
137
|
.split("; ")
|
|
141
138
|
.find((row) => row.startsWith(`${prismicCookie.preview}=`))
|
|
142
|
-
?.split("=")[1]
|
|
139
|
+
?.split("=")[1]
|
|
143
140
|
|
|
144
|
-
return (decodeURIComponent(cookie ?? "").match(/"([^"]+)\.prismic\.io"/) ||
|
|
145
|
-
[])[1];
|
|
141
|
+
return (decodeURIComponent(cookie ?? "").match(/"([^"]+)\.prismic\.io"/) || [])[1]
|
|
146
142
|
}
|
|
@@ -1,49 +1,47 @@
|
|
|
1
|
-
import { SliceSimulatorWrapper } from "../SliceSimulatorWrapper";
|
|
2
1
|
import {
|
|
3
2
|
SimulatorManager,
|
|
4
3
|
StateEventType,
|
|
5
4
|
getDefaultMessage,
|
|
6
5
|
getDefaultSlices,
|
|
7
|
-
} from "@prismicio/simulator/kit"
|
|
6
|
+
} from "@prismicio/simulator/kit"
|
|
8
7
|
import type {
|
|
9
8
|
SliceSimulatorProps as BaseSliceSimulatorProps,
|
|
10
9
|
SliceSimulatorState,
|
|
11
|
-
} from "@prismicio/simulator/kit"
|
|
12
|
-
import { useEffect, useState } from "react"
|
|
13
|
-
import type { ComponentType, FC } from "react"
|
|
10
|
+
} from "@prismicio/simulator/kit"
|
|
11
|
+
import { useEffect, useState } from "react"
|
|
12
|
+
import type { ComponentType, FC } from "react"
|
|
14
13
|
|
|
15
|
-
|
|
14
|
+
import { SliceSimulatorWrapper } from "../SliceSimulatorWrapper"
|
|
15
|
+
|
|
16
|
+
const simulatorManager = new SimulatorManager()
|
|
16
17
|
|
|
17
18
|
export type SliceSimulatorSliceZoneProps = {
|
|
18
|
-
slices: SliceSimulatorState["slices"]
|
|
19
|
-
}
|
|
19
|
+
slices: SliceSimulatorState["slices"]
|
|
20
|
+
}
|
|
20
21
|
|
|
21
22
|
export type SliceSimulatorProps = BaseSliceSimulatorProps & {
|
|
22
23
|
/**
|
|
23
24
|
* React component to render simulated Slices.
|
|
24
25
|
*
|
|
25
26
|
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* import { SliceSimulator } from "@slicemachine/adapter-next/simulator"
|
|
29
|
+
* import { SliceZone } from "@prismicio/react"
|
|
26
30
|
*
|
|
27
|
-
*
|
|
28
|
-
* import { SliceSimulator } from "@slicemachine/adapter-next/simulator";
|
|
29
|
-
* import { SliceZone } from "@prismicio/react";
|
|
30
|
-
*
|
|
31
|
-
* import { components } from "../slices";
|
|
31
|
+
* import { components } from "../slices"
|
|
32
32
|
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* />;
|
|
38
|
-
* ```
|
|
33
|
+
* ;<SliceSimulator
|
|
34
|
+
* sliceZone={({ slices }) => <SliceZone slices={slices} components={components} />}
|
|
35
|
+
* />
|
|
36
|
+
* ```
|
|
39
37
|
*/
|
|
40
|
-
sliceZone: ComponentType<SliceSimulatorSliceZoneProps
|
|
41
|
-
className?: string
|
|
42
|
-
}
|
|
38
|
+
sliceZone: ComponentType<SliceSimulatorSliceZoneProps>
|
|
39
|
+
className?: string
|
|
40
|
+
}
|
|
43
41
|
|
|
44
42
|
/**
|
|
45
|
-
* Simulate slices in isolation. The slice simulator enables live slice
|
|
46
|
-
*
|
|
43
|
+
* Simulate slices in isolation. The slice simulator enables live slice development in Slice Machine
|
|
44
|
+
* and live previews in the Page Builder.
|
|
47
45
|
*/
|
|
48
46
|
export const SliceSimulator: FC<SliceSimulatorProps> = ({
|
|
49
47
|
background,
|
|
@@ -51,33 +49,33 @@ export const SliceSimulator: FC<SliceSimulatorProps> = ({
|
|
|
51
49
|
className,
|
|
52
50
|
sliceZone: SliceZoneComp,
|
|
53
51
|
}) => {
|
|
54
|
-
const [slices, setSlices] = useState(() => getDefaultSlices())
|
|
55
|
-
const [message, setMessage] = useState(() => getDefaultMessage())
|
|
52
|
+
const [slices, setSlices] = useState(() => getDefaultSlices())
|
|
53
|
+
const [message, setMessage] = useState(() => getDefaultMessage())
|
|
56
54
|
|
|
57
55
|
useEffect(() => {
|
|
58
56
|
simulatorManager.state.on(
|
|
59
57
|
StateEventType.Slices,
|
|
60
58
|
(_slices) => {
|
|
61
|
-
setSlices(_slices)
|
|
59
|
+
setSlices(_slices)
|
|
62
60
|
},
|
|
63
61
|
"simulator-slices",
|
|
64
|
-
)
|
|
62
|
+
)
|
|
65
63
|
simulatorManager.state.on(
|
|
66
64
|
StateEventType.Message,
|
|
67
65
|
(_message) => {
|
|
68
|
-
setMessage(_message)
|
|
66
|
+
setMessage(_message)
|
|
69
67
|
},
|
|
70
68
|
"simulator-message",
|
|
71
|
-
)
|
|
69
|
+
)
|
|
72
70
|
|
|
73
|
-
simulatorManager.init()
|
|
71
|
+
simulatorManager.init()
|
|
74
72
|
|
|
75
73
|
return () => {
|
|
76
|
-
simulatorManager.state.off(StateEventType.Slices, "simulator-slices")
|
|
74
|
+
simulatorManager.state.off(StateEventType.Slices, "simulator-slices")
|
|
77
75
|
|
|
78
|
-
simulatorManager.state.off(StateEventType.Message, "simulator-message")
|
|
79
|
-
}
|
|
80
|
-
}, [])
|
|
76
|
+
simulatorManager.state.off(StateEventType.Message, "simulator-message")
|
|
77
|
+
}
|
|
78
|
+
}, [])
|
|
81
79
|
|
|
82
80
|
return (
|
|
83
81
|
<SliceSimulatorWrapper
|
|
@@ -89,5 +87,5 @@ export const SliceSimulator: FC<SliceSimulatorProps> = ({
|
|
|
89
87
|
>
|
|
90
88
|
<SliceZoneComp slices={slices} />
|
|
91
89
|
</SliceSimulatorWrapper>
|
|
92
|
-
)
|
|
93
|
-
}
|
|
90
|
+
)
|
|
91
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { Client } from "@prismicio/client"
|
|
2
|
+
import type { PreviewData } from "next"
|
|
3
3
|
|
|
4
|
-
import type { NextApiRequestLike } from "./types"
|
|
4
|
+
import type { NextApiRequestLike } from "./types"
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Configuration for `enableAutoPreviews`.
|
|
@@ -13,26 +13,26 @@ export type EnableAutoPreviewsConfig = {
|
|
|
13
13
|
// `Pick` is used to use the smallest possible subset of
|
|
14
14
|
// `prismic.Client`. Doing this reduces the surface area for breaking
|
|
15
15
|
// type changes.
|
|
16
|
-
client: Pick<Client, "queryContentFromRef" | "enableAutoPreviewsFromReq"
|
|
16
|
+
client: Pick<Client, "queryContentFromRef" | "enableAutoPreviewsFromReq">
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
|
-
* The `previewData` object provided in the `getStaticProps()` or
|
|
20
|
-
*
|
|
19
|
+
* The `previewData` object provided in the `getStaticProps()` or `getServerSideProps()` context
|
|
20
|
+
* object.
|
|
21
21
|
*/
|
|
22
|
-
previewData?: PreviewData
|
|
22
|
+
previewData?: PreviewData
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* The `req` object from a Next.js API route.
|
|
26
26
|
*
|
|
27
27
|
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
28
28
|
*/
|
|
29
|
-
req?: NextApiRequestLike
|
|
30
|
-
}
|
|
29
|
+
req?: NextApiRequestLike
|
|
30
|
+
}
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
|
-
* Configures a Prismic client to automatically query draft content during a
|
|
34
|
-
*
|
|
35
|
-
*
|
|
33
|
+
* Configures a Prismic client to automatically query draft content during a preview session. It
|
|
34
|
+
* either takes in a Next.js `getStaticProps` context object or a Next.js API endpoint request
|
|
35
|
+
* object.
|
|
36
36
|
*
|
|
37
37
|
* @param config - Configuration for the function.
|
|
38
38
|
*/
|
|
@@ -42,21 +42,21 @@ export function enableAutoPreviews(config: EnableAutoPreviewsConfig): void {
|
|
|
42
42
|
// `getServerSideProps()` with active Preview Mode.
|
|
43
43
|
|
|
44
44
|
if (isPrismicPreviewData(config.previewData)) {
|
|
45
|
-
config.client.queryContentFromRef(config.previewData.ref)
|
|
45
|
+
config.client.queryContentFromRef(config.previewData.ref)
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
return
|
|
48
|
+
return
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
if ("req" in config && config.req) {
|
|
52
52
|
// Assume we are in an API Route.
|
|
53
53
|
|
|
54
|
-
config.client.enableAutoPreviewsFromReq(config.req)
|
|
54
|
+
config.client.enableAutoPreviewsFromReq(config.req)
|
|
55
55
|
|
|
56
|
-
return
|
|
56
|
+
return
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
function isPrismicPreviewData(input: unknown): input is { ref: string } {
|
|
61
|
-
return typeof input === "object" && input !== null && "ref" in input
|
|
61
|
+
return typeof input === "object" && input !== null && "ref" in input
|
|
62
62
|
}
|
package/src/pages/exitPreview.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
1
|
+
import type { NextApiRequestLike, NextApiResponseLike } from "./types"
|
|
2
2
|
|
|
3
3
|
/** Configuration for `exitPreview()`. */
|
|
4
4
|
export type ExitPreviewAPIRouteConfig = {
|
|
@@ -7,39 +7,38 @@ export type ExitPreviewAPIRouteConfig = {
|
|
|
7
7
|
*
|
|
8
8
|
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
9
9
|
*/
|
|
10
|
-
req?: NextApiRequestLike
|
|
10
|
+
req?: NextApiRequestLike
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* The `res` object from a Next.js API route.
|
|
14
14
|
*
|
|
15
15
|
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
16
16
|
*/
|
|
17
|
-
res: NextApiResponseLike
|
|
18
|
-
}
|
|
17
|
+
res: NextApiResponseLike
|
|
18
|
+
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* Ends a Prismic preview session within a Next.js app. This function should be
|
|
22
|
-
*
|
|
21
|
+
* Ends a Prismic preview session within a Next.js app. This function should be used in an API
|
|
22
|
+
* route.
|
|
23
23
|
*
|
|
24
24
|
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* // src/pages/api/exit-preview.js
|
|
25
27
|
*
|
|
26
|
-
*
|
|
27
|
-
* // src/pages/api/exit-preview.js
|
|
28
|
-
*
|
|
29
|
-
* import { exitPreview } from "@prismicio/next";
|
|
28
|
+
* import { exitPreview } from "@prismicio/next"
|
|
30
29
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
30
|
+
* export default function handler(_req, res) {
|
|
31
|
+
* return exitPreview({ res })
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
35
34
|
*/
|
|
36
35
|
export function exitPreview(config: ExitPreviewAPIRouteConfig): void {
|
|
37
|
-
config.res.clearPreviewData()
|
|
36
|
+
config.res.clearPreviewData()
|
|
38
37
|
|
|
39
38
|
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
40
|
-
config.res.setHeader("Cache-Control", "no-store")
|
|
39
|
+
config.res.setHeader("Cache-Control", "no-store")
|
|
41
40
|
|
|
42
|
-
config.res.json({ success: true })
|
|
41
|
+
config.res.json({ success: true })
|
|
43
42
|
|
|
44
|
-
return
|
|
43
|
+
return
|
|
45
44
|
}
|