@prismicio/next 2.2.3-pr.136.19d76d2 → 2.2.3
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 +11 -9
- package/dist/PrismicNextImage.cjs.map +1 -1
- package/dist/PrismicNextImage.d.cts +21 -13
- package/dist/PrismicNextImage.d.ts +21 -13
- package/dist/PrismicNextImage.js +11 -9
- package/dist/PrismicNextImage.js.map +1 -1
- package/dist/PrismicNextLink.cjs.map +1 -1
- package/dist/PrismicNextLink.js.map +1 -1
- package/dist/PrismicPreview.cjs +7 -6
- package/dist/PrismicPreview.cjs.map +1 -1
- package/dist/PrismicPreview.d.cts +12 -11
- package/dist/PrismicPreview.d.ts +12 -11
- package/dist/PrismicPreview.js +7 -6
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/PrismicPreviewClient.cjs +3 -1
- package/dist/PrismicPreviewClient.cjs.map +1 -1
- package/dist/PrismicPreviewClient.js +3 -1
- 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 +5 -3
- package/dist/SliceSimulator.d.ts +5 -3
- package/dist/SliceSimulator.js +3 -3
- package/dist/SliceSimulator.js.map +1 -1
- package/dist/SliceSimulatorWrapper.cjs +4 -1
- package/dist/SliceSimulatorWrapper.cjs.map +1 -1
- package/dist/SliceSimulatorWrapper.js +4 -1
- package/dist/SliceSimulatorWrapper.js.map +1 -1
- package/dist/createLocaleRedirect.cjs.map +1 -1
- package/dist/createLocaleRedirect.js.map +1 -1
- package/dist/enableAutoPreviews.cjs +2 -1
- package/dist/enableAutoPreviews.cjs.map +1 -1
- package/dist/enableAutoPreviews.d.cts +2 -1
- package/dist/enableAutoPreviews.d.ts +2 -1
- package/dist/enableAutoPreviews.js +2 -1
- package/dist/enableAutoPreviews.js.map +1 -1
- package/dist/exitPreview.cjs +10 -9
- package/dist/exitPreview.cjs.map +1 -1
- package/dist/exitPreview.d.cts +10 -9
- package/dist/exitPreview.d.ts +10 -9
- package/dist/exitPreview.js +10 -9
- package/dist/exitPreview.js.map +1 -1
- package/dist/getSlices.cjs.map +1 -1
- 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.ts +2 -2
- package/dist/imgixLoader.js +2 -2
- package/dist/imgixLoader.js.map +1 -1
- package/dist/lib/devMsg.cjs +8 -6
- package/dist/lib/devMsg.cjs.map +1 -1
- package/dist/lib/devMsg.js +8 -6
- package/dist/lib/devMsg.js.map +1 -1
- package/dist/lib/resolveDefaultExport.cjs +12 -8
- package/dist/lib/resolveDefaultExport.cjs.map +1 -1
- package/dist/lib/resolveDefaultExport.js +12 -8
- 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 +7 -6
- package/dist/pages/PrismicPreview.cjs.map +1 -1
- package/dist/pages/PrismicPreview.d.cts +12 -11
- package/dist/pages/PrismicPreview.d.ts +12 -11
- package/dist/pages/PrismicPreview.js +7 -6
- 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 +13 -10
- package/dist/pages/SliceSimulator.d.ts +13 -10
- 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.ts +5 -5
- package/dist/pages/enableAutoPreviews.js +3 -3
- package/dist/pages/enableAutoPreviews.js.map +1 -1
- package/dist/pages/exitPreview.cjs +10 -9
- package/dist/pages/exitPreview.cjs.map +1 -1
- package/dist/pages/exitPreview.d.cts +10 -9
- package/dist/pages/exitPreview.d.ts +10 -9
- package/dist/pages/exitPreview.js +10 -9
- package/dist/pages/exitPreview.js.map +1 -1
- package/dist/pages/redirectToPreviewURL.cjs.map +1 -1
- package/dist/pages/redirectToPreviewURL.d.cts +9 -7
- package/dist/pages/redirectToPreviewURL.d.ts +9 -7
- package/dist/pages/redirectToPreviewURL.js.map +1 -1
- package/dist/pages/setPreviewData.cjs.map +1 -1
- package/dist/pages/setPreviewData.js.map +1 -1
- package/dist/pages/types.d.cts +8 -3
- package/dist/pages/types.d.ts +8 -3
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.d.cts +5 -3
- package/dist/redirectToPreviewURL.d.ts +5 -3
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/dist/types.d.cts +2 -1
- package/dist/types.d.ts +2 -1
- package/package.json +25 -23
- package/src/PrismicNextImage.tsx +134 -113
- package/src/PrismicNextLink.tsx +45 -43
- package/src/PrismicPreview.tsx +29 -28
- package/src/PrismicPreviewClient.tsx +44 -42
- package/src/SliceSimulator.tsx +43 -38
- package/src/SliceSimulatorWrapper.tsx +25 -15
- package/src/createLocaleRedirect.ts +3 -3
- package/src/enableAutoPreviews.ts +16 -15
- package/src/exitPreview.ts +13 -12
- package/src/getSlices.ts +10 -6
- package/src/imgixLoader.ts +10 -10
- package/src/index.ts +21 -18
- package/src/lib/devMsg.ts +11 -9
- package/src/lib/resolveDefaultExport.ts +13 -9
- package/src/pages/PrismicPreview.tsx +51 -47
- package/src/pages/SliceSimulator.tsx +37 -35
- package/src/pages/enableAutoPreviews.ts +17 -17
- package/src/pages/exitPreview.ts +18 -17
- package/src/pages/index.ts +23 -20
- package/src/pages/redirectToPreviewURL.ts +33 -22
- package/src/pages/setPreviewData.ts +7 -7
- package/src/pages/types.ts +20 -16
- package/src/redirectToPreviewURL.ts +30 -21
- package/src/types.ts +13 -12
- package/dist/PrismicNextImage.d.cts.map +0 -1
- package/dist/PrismicNextImage.d.ts.map +0 -1
- package/dist/PrismicNextLink.d.cts.map +0 -1
- package/dist/PrismicNextLink.d.ts.map +0 -1
- package/dist/PrismicPreview.d.cts.map +0 -1
- package/dist/PrismicPreview.d.ts.map +0 -1
- package/dist/SliceSimulator.d.cts.map +0 -1
- package/dist/SliceSimulator.d.ts.map +0 -1
- package/dist/createLocaleRedirect.d.cts.map +0 -1
- package/dist/createLocaleRedirect.d.ts.map +0 -1
- package/dist/enableAutoPreviews.d.cts.map +0 -1
- package/dist/enableAutoPreviews.d.ts.map +0 -1
- package/dist/exitPreview.d.cts.map +0 -1
- package/dist/exitPreview.d.ts.map +0 -1
- package/dist/getSlices.d.cts.map +0 -1
- package/dist/getSlices.d.ts.map +0 -1
- package/dist/imgixLoader.d.cts.map +0 -1
- package/dist/imgixLoader.d.ts.map +0 -1
- package/dist/pages/PrismicPreview.d.cts.map +0 -1
- package/dist/pages/PrismicPreview.d.ts.map +0 -1
- package/dist/pages/SliceSimulator.d.cts.map +0 -1
- package/dist/pages/SliceSimulator.d.ts.map +0 -1
- package/dist/pages/enableAutoPreviews.d.cts.map +0 -1
- package/dist/pages/enableAutoPreviews.d.ts.map +0 -1
- package/dist/pages/exitPreview.d.cts.map +0 -1
- package/dist/pages/exitPreview.d.ts.map +0 -1
- package/dist/pages/redirectToPreviewURL.d.cts.map +0 -1
- package/dist/pages/redirectToPreviewURL.d.ts.map +0 -1
- package/dist/pages/setPreviewData.d.cts.map +0 -1
- package/dist/pages/setPreviewData.d.ts.map +0 -1
- package/dist/pages/types.d.cts.map +0 -1
- package/dist/pages/types.d.ts.map +0 -1
- package/dist/redirectToPreviewURL.d.cts.map +0 -1
- package/dist/redirectToPreviewURL.d.ts.map +0 -1
- package/dist/types.d.cts.map +0 -1
- package/dist/types.d.ts.map +0 -1
package/src/PrismicNextImage.tsx
CHANGED
|
@@ -1,164 +1,185 @@
|
|
|
1
|
-
"use client"
|
|
2
|
-
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import {
|
|
11
|
-
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
14
|
-
import {
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
ForwardRefExoticComponent,
|
|
5
|
+
PropsWithoutRef,
|
|
6
|
+
RefAttributes} from "react";
|
|
7
|
+
import {
|
|
8
|
+
forwardRef
|
|
9
|
+
} from "react";
|
|
10
|
+
import type { ImageProps } from "next/image";
|
|
11
|
+
import Image from "next/image";
|
|
12
|
+
import type { ImgixURLParams } from "imgix-url-builder";
|
|
13
|
+
import { buildURL } from "imgix-url-builder";
|
|
14
|
+
import type { ImageFieldImage} from "@prismicio/client";
|
|
15
|
+
import { isFilled } from "@prismicio/client";
|
|
16
|
+
|
|
17
|
+
import { devMsg } from "./lib/devMsg";
|
|
18
|
+
import { resolveDefaultExport } from "./lib/resolveDefaultExport";
|
|
19
|
+
|
|
20
|
+
import { imgixLoader } from "./imgixLoader";
|
|
15
21
|
|
|
16
22
|
const castInt = (input: string | number | undefined): number | undefined => {
|
|
17
23
|
if (typeof input === "number" || typeof input === "undefined") {
|
|
18
|
-
return input
|
|
24
|
+
return input;
|
|
19
25
|
} else {
|
|
20
|
-
const parsed = Number.parseInt(input)
|
|
26
|
+
const parsed = Number.parseInt(input);
|
|
21
27
|
|
|
22
28
|
if (Number.isNaN(parsed)) {
|
|
23
|
-
return undefined
|
|
29
|
+
return undefined;
|
|
24
30
|
} else {
|
|
25
|
-
return parsed
|
|
31
|
+
return parsed;
|
|
26
32
|
}
|
|
27
33
|
}
|
|
28
|
-
}
|
|
34
|
+
};
|
|
29
35
|
|
|
30
|
-
export type PrismicNextImageProps = Omit<
|
|
36
|
+
export type PrismicNextImageProps = Omit<
|
|
37
|
+
ImageProps,
|
|
38
|
+
"src" | "alt" | "loader"
|
|
39
|
+
> & {
|
|
31
40
|
/** The Prismic Image field or thumbnail to render. */
|
|
32
|
-
field: ImageFieldImage | null | undefined
|
|
41
|
+
field: ImageFieldImage | null | undefined;
|
|
33
42
|
|
|
34
43
|
/**
|
|
35
44
|
* An object of Imgix URL API parameters to transform the image.
|
|
36
45
|
*
|
|
37
46
|
* @see https://docs.imgix.com/apis/rendering
|
|
38
47
|
*/
|
|
39
|
-
imgixParams?: { [P in keyof ImgixURLParams]: ImgixURLParams[P] | null }
|
|
48
|
+
imgixParams?: { [P in keyof ImgixURLParams]: ImgixURLParams[P] | null };
|
|
40
49
|
|
|
41
50
|
/**
|
|
42
51
|
* Declare an image as decorative by providing `alt=""`.
|
|
43
52
|
*
|
|
44
|
-
* See:
|
|
53
|
+
* See:
|
|
54
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
45
55
|
*/
|
|
46
|
-
alt?: ""
|
|
56
|
+
alt?: "";
|
|
47
57
|
|
|
48
58
|
/**
|
|
49
|
-
* Declare an image as decorative only if the Image field does not have
|
|
50
|
-
* providing `fallbackAlt=""`.
|
|
59
|
+
* Declare an image as decorative only if the Image field does not have
|
|
60
|
+
* alternative text by providing `fallbackAlt=""`.
|
|
51
61
|
*
|
|
52
|
-
* See:
|
|
62
|
+
* See:
|
|
63
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
53
64
|
*/
|
|
54
|
-
fallbackAlt?: ""
|
|
65
|
+
fallbackAlt?: "";
|
|
55
66
|
|
|
56
|
-
/**
|
|
57
|
-
|
|
67
|
+
/**
|
|
68
|
+
* Rendered when the field is empty. If a fallback is not given, `null` will
|
|
69
|
+
* be rendered.
|
|
70
|
+
*/
|
|
71
|
+
fallback?: React.ReactNode;
|
|
58
72
|
|
|
59
|
-
loader?: ImageProps["loader"] | null
|
|
60
|
-
}
|
|
73
|
+
loader?: ImageProps["loader"] | null;
|
|
74
|
+
};
|
|
61
75
|
|
|
62
76
|
/**
|
|
63
|
-
* React component that renders an image from a Prismic Image field or one of
|
|
64
|
-
* `next/image`. It will automatically set the `alt`
|
|
65
|
-
* property.
|
|
77
|
+
* React component that renders an image from a Prismic Image field or one of
|
|
78
|
+
* its thumbnails using `next/image`. It will automatically set the `alt`
|
|
79
|
+
* attribute using the Image field's `alt` property.
|
|
66
80
|
*
|
|
67
|
-
* It uses an Imgix URL-based loader by default. A custom loader can be provided
|
|
68
|
-
* prop. If you would like to use the Next.js Image
|
|
69
|
-
* `loader={undefined}`.
|
|
81
|
+
* It uses an Imgix URL-based loader by default. A custom loader can be provided
|
|
82
|
+
* with the `loader` prop. If you would like to use the Next.js Image
|
|
83
|
+
* Optimization API instead, set `loader={undefined}`.
|
|
70
84
|
*
|
|
71
85
|
* @param props - Props for the component.
|
|
86
|
+
*
|
|
72
87
|
* @returns A responsive image component using `next/image` for the given Image
|
|
73
|
-
*
|
|
88
|
+
* field.
|
|
89
|
+
*
|
|
74
90
|
* @see To learn more about `next/image`, see: https://nextjs.org/docs/api-reference/next/image
|
|
75
91
|
*/
|
|
76
92
|
// The type annotation is necessary to avoid a type reference issue.
|
|
77
93
|
export const PrismicNextImage: ForwardRefExoticComponent<
|
|
78
94
|
PropsWithoutRef<PrismicNextImageProps> & RefAttributes<HTMLImageElement>
|
|
79
|
-
> = forwardRef<HTMLImageElement, PrismicNextImageProps>(
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
"alt
|
|
99
|
-
|
|
100
|
-
|
|
95
|
+
> = forwardRef<HTMLImageElement, PrismicNextImageProps>(
|
|
96
|
+
function PrismicNextImage(
|
|
97
|
+
{
|
|
98
|
+
field,
|
|
99
|
+
imgixParams = {},
|
|
100
|
+
alt,
|
|
101
|
+
fallbackAlt,
|
|
102
|
+
fill,
|
|
103
|
+
width,
|
|
104
|
+
height,
|
|
105
|
+
fallback = null,
|
|
106
|
+
loader = imgixLoader,
|
|
107
|
+
...restProps
|
|
108
|
+
},
|
|
109
|
+
ref,
|
|
110
|
+
) {
|
|
111
|
+
if (process.env.NODE_ENV === "development") {
|
|
112
|
+
if (typeof alt === "string" && alt !== "") {
|
|
113
|
+
console.warn(
|
|
114
|
+
`[PrismicNextImage] The "alt" prop can only be used to declare an image as decorative by passing an empty string (alt="") but was provided a non-empty string. You can resolve this warning by removing the "alt" prop or changing it to alt="". For more details, see ${devMsg(
|
|
115
|
+
"alt-must-be-an-empty-string",
|
|
116
|
+
)}`,
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (typeof fallbackAlt === "string" && fallbackAlt !== "") {
|
|
121
|
+
console.warn(
|
|
122
|
+
`[PrismicNextImage] The "fallbackAlt" prop can only be used to declare an image as decorative by passing an empty string (fallbackAlt="") but was provided a non-empty string. You can resolve this warning by removing the "fallbackAlt" prop or changing it to fallbackAlt="". For more details, see ${devMsg(
|
|
123
|
+
"alt-must-be-an-empty-string",
|
|
124
|
+
)}`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
101
127
|
}
|
|
102
128
|
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
`[PrismicNextImage] The "fallbackAlt" prop can only be used to declare an image as decorative by passing an empty string (fallbackAlt="") but was provided a non-empty string. You can resolve this warning by removing the "fallbackAlt" prop or changing it to fallbackAlt="". For more details, see ${devMsg(
|
|
106
|
-
"alt-must-be-an-empty-string",
|
|
107
|
-
)}`,
|
|
108
|
-
)
|
|
129
|
+
if (!isFilled.imageThumbnail(field)) {
|
|
130
|
+
return <>{fallback}</>;
|
|
109
131
|
}
|
|
110
|
-
}
|
|
111
132
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
for (const x in imgixParams) {
|
|
118
|
-
if (resolvedImgixParams[x as keyof typeof resolvedImgixParams] === null) {
|
|
119
|
-
resolvedImgixParams[x as keyof typeof resolvedImgixParams] = undefined
|
|
133
|
+
const resolvedImgixParams = imgixParams;
|
|
134
|
+
for (const x in imgixParams) {
|
|
135
|
+
if (resolvedImgixParams[x as keyof typeof resolvedImgixParams] === null) {
|
|
136
|
+
resolvedImgixParams[x as keyof typeof resolvedImgixParams] = undefined;
|
|
137
|
+
}
|
|
120
138
|
}
|
|
121
|
-
}
|
|
122
139
|
|
|
123
|
-
|
|
140
|
+
const src = buildURL(field.url, imgixParams as ImgixURLParams);
|
|
124
141
|
|
|
125
|
-
|
|
142
|
+
const ar = field.dimensions.width / field.dimensions.height;
|
|
126
143
|
|
|
127
|
-
|
|
128
|
-
|
|
144
|
+
const castedWidth = castInt(width);
|
|
145
|
+
const castedHeight = castInt(height);
|
|
129
146
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
if (castedWidth != null && castedHeight == null) {
|
|
134
|
-
resolvedHeight = castedWidth / ar
|
|
135
|
-
} else if (castedWidth == null && castedHeight != null) {
|
|
136
|
-
resolvedWidth = castedHeight * ar
|
|
137
|
-
}
|
|
147
|
+
let resolvedWidth = castedWidth ?? field.dimensions.width;
|
|
148
|
+
let resolvedHeight = castedHeight ?? field.dimensions.height;
|
|
138
149
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
150
|
+
if (castedWidth != null && castedHeight == null) {
|
|
151
|
+
resolvedHeight = castedWidth / ar;
|
|
152
|
+
} else if (castedWidth == null && castedHeight != null) {
|
|
153
|
+
resolvedWidth = castedHeight * ar;
|
|
154
|
+
}
|
|
142
155
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
156
|
+
// A non-null assertion is required since we can't statically
|
|
157
|
+
// know if an alt attribute is available.
|
|
158
|
+
const resolvedAlt = (alt ?? (field.alt || fallbackAlt))!;
|
|
159
|
+
|
|
160
|
+
if (
|
|
161
|
+
process.env.NODE_ENV === "development" &&
|
|
162
|
+
typeof resolvedAlt !== "string"
|
|
163
|
+
) {
|
|
164
|
+
console.error(
|
|
165
|
+
`[PrismicNextImage] The following image is missing an "alt" property. Please add Alternative Text to the image in Prismic. To mark the image as decorative instead, add one of \`alt=""\` or \`fallbackAlt=""\`.`,
|
|
166
|
+
src,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
149
169
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
170
|
+
const ResolvedImage = resolveDefaultExport(Image);
|
|
171
|
+
|
|
172
|
+
return (
|
|
173
|
+
<ResolvedImage
|
|
174
|
+
ref={ref}
|
|
175
|
+
src={src}
|
|
176
|
+
width={fill ? undefined : resolvedWidth}
|
|
177
|
+
height={fill ? undefined : resolvedHeight}
|
|
178
|
+
alt={resolvedAlt}
|
|
179
|
+
fill={fill}
|
|
180
|
+
loader={loader === null ? undefined : loader}
|
|
181
|
+
{...restProps}
|
|
182
|
+
/>
|
|
183
|
+
);
|
|
184
|
+
},
|
|
185
|
+
);
|
package/src/PrismicNextLink.tsx
CHANGED
|
@@ -1,65 +1,67 @@
|
|
|
1
|
+
import type { ComponentProps} from "react";
|
|
2
|
+
import { forwardRef } from "react";
|
|
3
|
+
import Link from "next/link";
|
|
1
4
|
import type {
|
|
2
5
|
AsLinkAttrsConfig,
|
|
3
6
|
LinkField,
|
|
4
7
|
LinkResolverFunction,
|
|
5
|
-
PrismicDocument
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
import type { ComponentProps } from "react"
|
|
10
|
-
import { forwardRef } from "react"
|
|
8
|
+
PrismicDocument} from "@prismicio/client";
|
|
9
|
+
import {
|
|
10
|
+
asLinkAttrs,
|
|
11
|
+
} from "@prismicio/client";
|
|
11
12
|
|
|
12
|
-
import { resolveDefaultExport } from "./lib/resolveDefaultExport"
|
|
13
|
+
import { resolveDefaultExport } from "./lib/resolveDefaultExport";
|
|
13
14
|
|
|
14
15
|
export type PrismicNextLinkProps = Omit<
|
|
15
16
|
ComponentProps<typeof Link>,
|
|
16
17
|
"field" | "document" | "href" | "rel"
|
|
17
18
|
> & {
|
|
18
|
-
linkResolver?: LinkResolverFunction
|
|
19
|
-
rel?: string | AsLinkAttrsConfig["rel"]
|
|
19
|
+
linkResolver?: LinkResolverFunction;
|
|
20
|
+
rel?: string | AsLinkAttrsConfig["rel"];
|
|
20
21
|
} & (
|
|
21
22
|
| {
|
|
22
|
-
field: LinkField | null | undefined
|
|
23
|
-
document?: never
|
|
24
|
-
href?: never
|
|
23
|
+
field: LinkField | null | undefined;
|
|
24
|
+
document?: never;
|
|
25
|
+
href?: never;
|
|
25
26
|
}
|
|
26
27
|
| {
|
|
27
|
-
field?: never
|
|
28
|
-
document: PrismicDocument | null | undefined
|
|
29
|
-
href?: never
|
|
28
|
+
field?: never;
|
|
29
|
+
document: PrismicDocument | null | undefined;
|
|
30
|
+
href?: never;
|
|
30
31
|
}
|
|
31
32
|
| {
|
|
32
|
-
field?: never
|
|
33
|
-
document?: never
|
|
34
|
-
href: ComponentProps<typeof Link>["href"]
|
|
33
|
+
field?: never;
|
|
34
|
+
document?: never;
|
|
35
|
+
href: ComponentProps<typeof Link>["href"];
|
|
35
36
|
}
|
|
36
|
-
)
|
|
37
|
+
);
|
|
37
38
|
|
|
38
|
-
export const PrismicNextLink = forwardRef<
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
39
|
+
export const PrismicNextLink = forwardRef<
|
|
40
|
+
HTMLAnchorElement,
|
|
41
|
+
PrismicNextLinkProps
|
|
42
|
+
>(function PrismicNextLink(props, ref) {
|
|
43
|
+
const { field, document, linkResolver, children, ...restProps } = props;
|
|
44
|
+
const {
|
|
45
|
+
href: computedHref,
|
|
46
|
+
rel: computedRel,
|
|
47
|
+
...attrs
|
|
48
|
+
} = asLinkAttrs(field ?? document, {
|
|
49
|
+
linkResolver,
|
|
50
|
+
rel: typeof restProps.rel === "function" ? restProps.rel : undefined,
|
|
51
|
+
});
|
|
49
52
|
|
|
50
|
-
|
|
53
|
+
const href = ("href" in restProps ? restProps.href : computedHref) || "";
|
|
51
54
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
let rel = computedRel;
|
|
56
|
+
if ("rel" in restProps && typeof restProps.rel !== "function") {
|
|
57
|
+
rel = restProps.rel;
|
|
58
|
+
}
|
|
56
59
|
|
|
57
|
-
|
|
60
|
+
const ResolvedLink = resolveDefaultExport(Link);
|
|
58
61
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
)
|
|
62
|
+
return (
|
|
63
|
+
<ResolvedLink ref={ref} {...attrs} {...restProps} href={href} rel={rel}>
|
|
64
|
+
{"children" in props ? children : field?.text}
|
|
65
|
+
</ResolvedLink>
|
|
66
|
+
);
|
|
67
|
+
});
|
package/src/PrismicPreview.tsx
CHANGED
|
@@ -1,54 +1,55 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import Script from "next/script"
|
|
3
|
-
import
|
|
1
|
+
import type { FC, ReactNode } from "react";
|
|
2
|
+
import Script from "next/script";
|
|
3
|
+
import { getToolbarSrc } from "@prismicio/client";
|
|
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
|
-
* repository.
|
|
11
|
+
* The name of your Prismic repository. A Prismic Toolbar will be registered
|
|
12
|
+
* using this 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
|
-
* fetched on preview update events.
|
|
17
|
+
* The URL of your app's Prismic preview endpoint (default: `/api/preview`).
|
|
18
|
+
* This URL will be 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
|
-
* fetched on preview exit events.
|
|
23
|
+
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`).
|
|
24
|
+
* This URL will be 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
|
-
* send events to the browser, such as on preview updates
|
|
35
|
-
* automatically refresh the page with the
|
|
33
|
+
* React component that sets up Prismic Previews using the Prismic Toolbar. When
|
|
34
|
+
* the Prismic Toolbar send events to the browser, such as on preview updates
|
|
35
|
+
* and exiting, this component will automatically refresh the page with the
|
|
36
|
+
* changes.
|
|
36
37
|
*
|
|
37
|
-
* This component can be wrapped around your app or added anywhere in your app's
|
|
38
|
-
* rendered on every page.
|
|
38
|
+
* This component can be wrapped around your app or added anywhere in your app's
|
|
39
|
+
* tree. It must be rendered on every page.
|
|
39
40
|
*/
|
|
40
41
|
export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
|
|
41
|
-
const { repositoryName, children, ...otherProps } = props
|
|
42
|
+
const { repositoryName, children, ...otherProps } = props;
|
|
42
43
|
|
|
43
44
|
// Need this to avoid the following Next.js build-time error:
|
|
44
45
|
// You're importing a component that needs next/headers. That only works
|
|
45
46
|
// in a Server Component which is not supported in the pages/ directory.
|
|
46
|
-
const { draftMode } = await import("next/headers")
|
|
47
|
+
const { draftMode } = await import("next/headers");
|
|
47
48
|
|
|
48
|
-
const toolbarSrc = getToolbarSrc(repositoryName)
|
|
49
|
-
const isDraftMode = (await draftMode()).isEnabled
|
|
49
|
+
const toolbarSrc = getToolbarSrc(repositoryName);
|
|
50
|
+
const isDraftMode = (await draftMode()).isEnabled;
|
|
50
51
|
|
|
51
|
-
const ResolvedScript = resolveDefaultExport(Script)
|
|
52
|
+
const ResolvedScript = resolveDefaultExport(Script);
|
|
52
53
|
|
|
53
54
|
return (
|
|
54
55
|
<>
|
|
@@ -60,5 +61,5 @@ export const PrismicPreview: FC<PrismicPreviewProps> = async (props) => {
|
|
|
60
61
|
/>
|
|
61
62
|
<ResolvedScript src={toolbarSrc} strategy="lazyOnload" />
|
|
62
63
|
</>
|
|
63
|
-
)
|
|
64
|
-
}
|
|
64
|
+
);
|
|
65
|
+
};
|
|
@@ -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,73 +55,75 @@ 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
|
|
104
|
+
* @param cookieJar - The stringified cookie store from which to read the
|
|
105
|
+
* cookie.
|
|
106
|
+
*
|
|
105
107
|
* @returns The value of the cookie, if it exists.
|
|
106
108
|
*/
|
|
107
109
|
function getPrismicPreviewCookie(cookieJar: string): string | undefined {
|
|
108
110
|
function readValue(value: string): string {
|
|
109
|
-
return value.replace(/%3B/g, ";")
|
|
111
|
+
return value.replace(/%3B/g, ";");
|
|
110
112
|
}
|
|
111
113
|
|
|
112
|
-
const cookies = cookieJar.split("; ")
|
|
114
|
+
const cookies = cookieJar.split("; ");
|
|
113
115
|
|
|
114
|
-
let value: string | undefined
|
|
116
|
+
let value: string | undefined;
|
|
115
117
|
|
|
116
118
|
for (const cookie of cookies) {
|
|
117
|
-
const parts = cookie.split("=")
|
|
118
|
-
const name = readValue(parts[0]).replace(/%3D/g, "=")
|
|
119
|
+
const parts = cookie.split("=");
|
|
120
|
+
const name = readValue(parts[0]).replace(/%3D/g, "=");
|
|
119
121
|
|
|
120
122
|
if (name === prismicCookie.preview) {
|
|
121
|
-
value = readValue(parts.slice(1).join("="))
|
|
122
|
-
continue
|
|
123
|
+
value = readValue(parts.slice(1).join("="));
|
|
124
|
+
continue;
|
|
123
125
|
}
|
|
124
126
|
}
|
|
125
127
|
|
|
126
|
-
return value
|
|
128
|
+
return value;
|
|
127
129
|
}
|