@prismicio/next 1.0.0-alpha.1 → 1.0.0-alpha.2
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 +20 -30
- package/dist/PrismicNextImage.cjs.map +1 -1
- package/dist/PrismicNextImage.d.ts +6 -1
- package/dist/PrismicNextImage.js +20 -30
- package/dist/PrismicNextImage.js.map +1 -1
- package/dist/PrismicPreview.cjs.map +1 -1
- package/dist/PrismicPreview.d.ts +1 -1
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/imgixLoader.cjs +17 -0
- package/dist/imgixLoader.cjs.map +1 -0
- package/dist/imgixLoader.d.ts +9 -0
- package/dist/imgixLoader.js +17 -0
- package/dist/imgixLoader.js.map +1 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +8 -13
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.js +1 -1
- package/package.json +6 -6
- package/src/PrismicNextImage.tsx +28 -60
- package/src/PrismicPreview.tsx +1 -1
- package/src/imgixLoader.ts +25 -0
- package/src/index.ts +15 -13
|
@@ -6,6 +6,7 @@ const imgixUrlBuilder = require("imgix-url-builder");
|
|
|
6
6
|
const prismicH = require("@prismicio/helpers");
|
|
7
7
|
const __PRODUCTION__ = require("./lib/__PRODUCTION__.cjs");
|
|
8
8
|
const devMsg = require("./lib/devMsg.cjs");
|
|
9
|
+
const imgixLoader = require("./imgixLoader.cjs");
|
|
9
10
|
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
|
|
10
11
|
function _interopNamespace(e) {
|
|
11
12
|
if (e && e.__esModule)
|
|
@@ -39,23 +40,15 @@ const castInt = (input) => {
|
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
};
|
|
42
|
-
const imgixLoader = (args) => {
|
|
43
|
-
const url = new URL(args.src);
|
|
44
|
-
const params = {
|
|
45
|
-
fit: url.searchParams.get("fit") || "max",
|
|
46
|
-
w: args.width,
|
|
47
|
-
h: void 0
|
|
48
|
-
};
|
|
49
|
-
if (args.quality) {
|
|
50
|
-
params.q = args.quality;
|
|
51
|
-
}
|
|
52
|
-
return imgixUrlBuilder.buildURL(args.src, params);
|
|
53
|
-
};
|
|
54
43
|
const PrismicNextImage = ({
|
|
55
44
|
field,
|
|
56
45
|
imgixParams = {},
|
|
57
46
|
alt,
|
|
58
47
|
fallbackAlt,
|
|
48
|
+
fill,
|
|
49
|
+
width,
|
|
50
|
+
height,
|
|
51
|
+
fallback = null,
|
|
59
52
|
...restProps
|
|
60
53
|
}) => {
|
|
61
54
|
if (!__PRODUCTION__.__PRODUCTION__) {
|
|
@@ -69,31 +62,28 @@ const PrismicNextImage = ({
|
|
|
69
62
|
if (prismicH__namespace.isFilled.imageThumbnail(field)) {
|
|
70
63
|
const src = imgixUrlBuilder.buildURL(field.url, imgixParams);
|
|
71
64
|
const ar = field.dimensions.width / field.dimensions.height;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
} else {
|
|
81
|
-
if (castedHeight) {
|
|
82
|
-
resolvedWidth = ar * castedHeight;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
resolvedHeight = resolvedWidth / ar;
|
|
65
|
+
const castedWidth = castInt(width);
|
|
66
|
+
const castedHeight = castInt(height);
|
|
67
|
+
let resolvedWidth = castedWidth != null ? castedWidth : field.dimensions.width;
|
|
68
|
+
let resolvedHeight = castedHeight != null ? castedHeight : field.dimensions.width;
|
|
69
|
+
if (castedWidth != null && castedHeight == null) {
|
|
70
|
+
resolvedHeight = castedWidth / ar;
|
|
71
|
+
} else if (castedWidth == null && castedHeight != null) {
|
|
72
|
+
resolvedWidth = castedHeight * ar;
|
|
86
73
|
}
|
|
87
74
|
return jsxRuntime.jsx(Image__default.default, {
|
|
88
75
|
src,
|
|
89
|
-
width:
|
|
90
|
-
height:
|
|
76
|
+
width: fill ? void 0 : resolvedWidth,
|
|
77
|
+
height: fill ? void 0 : resolvedHeight,
|
|
91
78
|
alt: alt != null ? alt : field.alt || fallbackAlt,
|
|
92
|
-
|
|
79
|
+
fill,
|
|
80
|
+
loader: imgixLoader.imgixLoader,
|
|
93
81
|
...restProps
|
|
94
82
|
});
|
|
95
83
|
} else {
|
|
96
|
-
return
|
|
84
|
+
return jsxRuntime.jsx(jsxRuntime.Fragment, {
|
|
85
|
+
children: fallback
|
|
86
|
+
});
|
|
97
87
|
}
|
|
98
88
|
};
|
|
99
89
|
exports.PrismicNextImage = PrismicNextImage;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicNextImage.cjs","sources":["../src/PrismicNextImage.tsx"],"sourcesContent":["import Image, { ImageProps
|
|
1
|
+
{"version":3,"file":"PrismicNextImage.cjs","sources":["../src/PrismicNextImage.tsx"],"sourcesContent":["import Image, { ImageProps } from \"next/image\";\nimport { buildURL, ImgixURLParams } from \"imgix-url-builder\";\nimport * as prismicH from \"@prismicio/helpers\";\nimport * as prismicT from \"@prismicio/types\";\n\nimport { __PRODUCTION__ } from \"./lib/__PRODUCTION__\";\nimport { devMsg } from \"./lib/devMsg\";\n\nimport { imgixLoader } from \"./imgixLoader\";\n\nconst castInt = (input: string | number | undefined): number | undefined => {\n\tif (typeof input === \"number\" || typeof input === \"undefined\") {\n\t\treturn input;\n\t} else {\n\t\tconst parsed = Number.parseInt(input);\n\n\t\tif (Number.isNaN(parsed)) {\n\t\t\treturn undefined;\n\t\t} else {\n\t\t\treturn parsed;\n\t\t}\n\t}\n};\n\nexport type PrismicNextImageProps = Omit<ImageProps, \"src\" | \"alt\"> & {\n\t/**\n\t * The Prismic Image field or thumbnail to render.\n\t */\n\tfield: prismicT.ImageFieldImage | null | undefined;\n\n\t/**\n\t * An object of Imgix URL API parameters to transform the image.\n\t *\n\t * @see https://docs.imgix.com/apis/rendering\n\t */\n\timgixParams?: ImgixURLParams;\n\n\t/**\n\t * Declare an image as decorative by providing `alt=\"\"`.\n\t *\n\t * See:\n\t * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images\n\t */\n\talt?: \"\";\n\n\t/**\n\t * Declare an image as decorative only if the Image field does not have\n\t * alternative text by providing `fallbackAlt=\"\"`.\n\t *\n\t * See:\n\t * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images\n\t */\n\tfallbackAlt?: \"\";\n\n\t/**\n\t * Rendered when the field is empty. If a fallback is not given, `null` will\n\t * be rendered.\n\t */\n\tfallback?: React.ReactNode;\n};\n\n/**\n * React component that renders an image from a Prismic Image field or one of\n * its thumbnails using `next/image`. It will automatically set the `alt`\n * attribute using the Image field's `alt` property.\n *\n * It uses an Imgix URL-based loader by default. A custom loader can be provided\n * with the `loader` prop. If you would like to use the Next.js Image\n * Optimization API instead, set `loader={undefined}`.\n *\n * @param props - Props for the component.\n *\n * @returns A responsive image component using `next/image` for the given Image\n * field.\n * @see To learn more about `next/image`, see: https://nextjs.org/docs/api-reference/next/image\n */\nexport const PrismicNextImage = ({\n\tfield,\n\timgixParams = {},\n\talt,\n\tfallbackAlt,\n\tfill,\n\twidth,\n\theight,\n\tfallback = null,\n\t...restProps\n}: PrismicNextImageProps): JSX.Element => {\n\tif (!__PRODUCTION__) {\n\t\tif (typeof alt === \"string\" && alt !== \"\") {\n\t\t\tconsole.warn(\n\t\t\t\t`[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(\n\t\t\t\t\t\"alt-must-be-an-empty-string\",\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\n\t\tif (typeof fallbackAlt === \"string\" && fallbackAlt !== \"\") {\n\t\t\tconsole.warn(\n\t\t\t\t`[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(\n\t\t\t\t\t\"alt-must-be-an-empty-string\",\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tif (prismicH.isFilled.imageThumbnail(field)) {\n\t\tconst src = buildURL(field.url, imgixParams);\n\n\t\tconst ar = field.dimensions.width / field.dimensions.height;\n\n\t\tconst castedWidth = castInt(width);\n\t\tconst castedHeight = castInt(height);\n\n\t\tlet resolvedWidth = castedWidth ?? field.dimensions.width;\n\t\tlet resolvedHeight = castedHeight ?? field.dimensions.width;\n\n\t\tif (castedWidth != null && castedHeight == null) {\n\t\t\tresolvedHeight = castedWidth / ar;\n\t\t} else if (castedWidth == null && castedHeight != null) {\n\t\t\tresolvedWidth = castedHeight * ar;\n\t\t}\n\n\t\treturn (\n\t\t\t<Image\n\t\t\t\tsrc={src}\n\t\t\t\twidth={fill ? undefined : resolvedWidth}\n\t\t\t\theight={fill ? undefined : resolvedHeight}\n\t\t\t\t// A non-null assertion is required since we\n\t\t\t\t// can't statically know if an alt attribute is\n\t\t\t\t// available.\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\t\talt={(alt ?? (field.alt || fallbackAlt))!}\n\t\t\t\tfill={fill}\n\t\t\t\tloader={imgixLoader}\n\t\t\t\t{...restProps}\n\t\t\t/>\n\t\t);\n\t} else {\n\t\treturn <>{fallback}</>;\n\t}\n};\n"],"names":["castInt","input","parsed","Number","parseInt","isNaN","undefined","PrismicNextImage","field","imgixParams","alt","fallbackAlt","fill","width","height","fallback","restProps","__PRODUCTION__","console","warn","devMsg","prismicH","isFilled","imageThumbnail","src","buildURL","url","ar","dimensions","castedWidth","castedHeight","resolvedWidth","resolvedHeight","_jsx","Image","loader","imgixLoader","_Fragment","children"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUA,MAAMA,UAAWC,CAA0D,UAAA;AAC1E,MAAI,OAAOA,UAAU,YAAY,OAAOA,UAAU,aAAa;AACvDA,WAAAA;AAAAA,EAAAA,OACD;AACAC,UAAAA,SAASC,OAAOC,SAASH,KAAK;AAEhCE,QAAAA,OAAOE,MAAMH,MAAM,GAAG;AAClBI,aAAAA;AAAAA,IAAAA,OACD;AACCJ,aAAAA;AAAAA,IACP;AAAA,EACD;AACF;AAsDO,MAAMK,mBAAmB,CAAC;AAAA,EAChCC;AAAAA,EACAC,cAAc,CAAE;AAAA,EAChBC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC,WAAW;AAAA,KACRC;AAAS,MAC4B;AACxC,MAAI,CAACC,eAAAA,gBAAgB;AACpB,QAAI,OAAOP,QAAQ,YAAYA,QAAQ,IAAI;AAC1CQ,cAAQC,KACkQ,yQAAAC,OACxQ,OAAA,6BAA6B,GAC3B;AAAA,IAEJ;AAED,QAAI,OAAOT,gBAAgB,YAAYA,gBAAgB,IAAI;AAC1DO,cAAQC,KACkS,ySAAAC,OACxS,OAAA,6BAA6B,GAC3B;AAAA,IAEJ;AAAA,EACD;AAED,MAAIC,oBAASC,SAASC,eAAef,KAAK,GAAG;AAC5C,UAAMgB,MAAMC,gBAAAA,SAASjB,MAAMkB,KAAKjB,WAAW;AAE3C,UAAMkB,KAAKnB,MAAMoB,WAAWf,QAAQL,MAAMoB,WAAWd;AAE/Ce,UAAAA,cAAc7B,QAAQa,KAAK;AAC3BiB,UAAAA,eAAe9B,QAAQc,MAAM;AAE/BiB,QAAAA,gBAAgBF,oCAAerB,MAAMoB,WAAWf;AAChDmB,QAAAA,iBAAiBF,sCAAgBtB,MAAMoB,WAAWf;AAElDgB,QAAAA,eAAe,QAAQC,gBAAgB,MAAM;AAChDE,uBAAiBH,cAAcF;AAAAA,IACrBE,WAAAA,eAAe,QAAQC,gBAAgB,MAAM;AACvDC,sBAAgBD,eAAeH;AAAAA,IAC/B;AAED,WACCM,WAAAA,IAACC,eAAAA,SAAK;AAAA,MACLV;AAAAA,MACAX,OAAOD,OAAON,SAAYyB;AAAAA,MAC1BjB,QAAQF,OAAON,SAAY0B;AAAAA,MAK3BtB,KAAMA,oBAAQF,MAAME,OAAOC;AAAAA,MAC3BC;AAAAA,MACAuB,QAAQC,YAAAA;AAAAA,MACJ,GAAApB;AAAAA,IAAAA,CACH;AAAA,EAAA,OAEG;AACN,WAAOiB,WAAAA,IAAAI,WAAAA,UAAA;AAAA,MAAAC,UAAGvB;AAAAA,IAAAA,CAAQ;AAAA,EAClB;AACF;;"}
|
|
@@ -28,6 +28,11 @@ export declare type PrismicNextImageProps = Omit<ImageProps, "src" | "alt"> & {
|
|
|
28
28
|
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
29
29
|
*/
|
|
30
30
|
fallbackAlt?: "";
|
|
31
|
+
/**
|
|
32
|
+
* Rendered when the field is empty. If a fallback is not given, `null` will
|
|
33
|
+
* be rendered.
|
|
34
|
+
*/
|
|
35
|
+
fallback?: React.ReactNode;
|
|
31
36
|
};
|
|
32
37
|
/**
|
|
33
38
|
* React component that renders an image from a Prismic Image field or one of
|
|
@@ -44,4 +49,4 @@ export declare type PrismicNextImageProps = Omit<ImageProps, "src" | "alt"> & {
|
|
|
44
49
|
* field.
|
|
45
50
|
* @see To learn more about `next/image`, see: https://nextjs.org/docs/api-reference/next/image
|
|
46
51
|
*/
|
|
47
|
-
export declare const PrismicNextImage: ({ field, imgixParams, alt, fallbackAlt, ...restProps }: PrismicNextImageProps) => JSX.Element
|
|
52
|
+
export declare const PrismicNextImage: ({ field, imgixParams, alt, fallbackAlt, fill, width, height, fallback, ...restProps }: PrismicNextImageProps) => JSX.Element;
|
package/dist/PrismicNextImage.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { jsx } from "react/jsx-runtime";
|
|
1
|
+
import { jsx, Fragment } from "react/jsx-runtime";
|
|
2
2
|
import Image from "next/image";
|
|
3
3
|
import { buildURL } from "imgix-url-builder";
|
|
4
4
|
import * as prismicH from "@prismicio/helpers";
|
|
5
5
|
import { __PRODUCTION__ } from "./lib/__PRODUCTION__.js";
|
|
6
6
|
import { devMsg } from "./lib/devMsg.js";
|
|
7
|
+
import { imgixLoader } from "./imgixLoader.js";
|
|
7
8
|
const castInt = (input) => {
|
|
8
9
|
if (typeof input === "number" || typeof input === "undefined") {
|
|
9
10
|
return input;
|
|
@@ -16,23 +17,15 @@ const castInt = (input) => {
|
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
};
|
|
19
|
-
const imgixLoader = (args) => {
|
|
20
|
-
const url = new URL(args.src);
|
|
21
|
-
const params = {
|
|
22
|
-
fit: url.searchParams.get("fit") || "max",
|
|
23
|
-
w: args.width,
|
|
24
|
-
h: void 0
|
|
25
|
-
};
|
|
26
|
-
if (args.quality) {
|
|
27
|
-
params.q = args.quality;
|
|
28
|
-
}
|
|
29
|
-
return buildURL(args.src, params);
|
|
30
|
-
};
|
|
31
20
|
const PrismicNextImage = ({
|
|
32
21
|
field,
|
|
33
22
|
imgixParams = {},
|
|
34
23
|
alt,
|
|
35
24
|
fallbackAlt,
|
|
25
|
+
fill,
|
|
26
|
+
width,
|
|
27
|
+
height,
|
|
28
|
+
fallback = null,
|
|
36
29
|
...restProps
|
|
37
30
|
}) => {
|
|
38
31
|
if (!__PRODUCTION__) {
|
|
@@ -46,31 +39,28 @@ const PrismicNextImage = ({
|
|
|
46
39
|
if (prismicH.isFilled.imageThumbnail(field)) {
|
|
47
40
|
const src = buildURL(field.url, imgixParams);
|
|
48
41
|
const ar = field.dimensions.width / field.dimensions.height;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
} else {
|
|
58
|
-
if (castedHeight) {
|
|
59
|
-
resolvedWidth = ar * castedHeight;
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
resolvedHeight = resolvedWidth / ar;
|
|
42
|
+
const castedWidth = castInt(width);
|
|
43
|
+
const castedHeight = castInt(height);
|
|
44
|
+
let resolvedWidth = castedWidth != null ? castedWidth : field.dimensions.width;
|
|
45
|
+
let resolvedHeight = castedHeight != null ? castedHeight : field.dimensions.width;
|
|
46
|
+
if (castedWidth != null && castedHeight == null) {
|
|
47
|
+
resolvedHeight = castedWidth / ar;
|
|
48
|
+
} else if (castedWidth == null && castedHeight != null) {
|
|
49
|
+
resolvedWidth = castedHeight * ar;
|
|
63
50
|
}
|
|
64
51
|
return jsx(Image, {
|
|
65
52
|
src,
|
|
66
|
-
width:
|
|
67
|
-
height:
|
|
53
|
+
width: fill ? void 0 : resolvedWidth,
|
|
54
|
+
height: fill ? void 0 : resolvedHeight,
|
|
68
55
|
alt: alt != null ? alt : field.alt || fallbackAlt,
|
|
56
|
+
fill,
|
|
69
57
|
loader: imgixLoader,
|
|
70
58
|
...restProps
|
|
71
59
|
});
|
|
72
60
|
} else {
|
|
73
|
-
return
|
|
61
|
+
return jsx(Fragment, {
|
|
62
|
+
children: fallback
|
|
63
|
+
});
|
|
74
64
|
}
|
|
75
65
|
};
|
|
76
66
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicNextImage.js","sources":["../src/PrismicNextImage.tsx"],"sourcesContent":["import Image, { ImageProps
|
|
1
|
+
{"version":3,"file":"PrismicNextImage.js","sources":["../src/PrismicNextImage.tsx"],"sourcesContent":["import Image, { ImageProps } from \"next/image\";\nimport { buildURL, ImgixURLParams } from \"imgix-url-builder\";\nimport * as prismicH from \"@prismicio/helpers\";\nimport * as prismicT from \"@prismicio/types\";\n\nimport { __PRODUCTION__ } from \"./lib/__PRODUCTION__\";\nimport { devMsg } from \"./lib/devMsg\";\n\nimport { imgixLoader } from \"./imgixLoader\";\n\nconst castInt = (input: string | number | undefined): number | undefined => {\n\tif (typeof input === \"number\" || typeof input === \"undefined\") {\n\t\treturn input;\n\t} else {\n\t\tconst parsed = Number.parseInt(input);\n\n\t\tif (Number.isNaN(parsed)) {\n\t\t\treturn undefined;\n\t\t} else {\n\t\t\treturn parsed;\n\t\t}\n\t}\n};\n\nexport type PrismicNextImageProps = Omit<ImageProps, \"src\" | \"alt\"> & {\n\t/**\n\t * The Prismic Image field or thumbnail to render.\n\t */\n\tfield: prismicT.ImageFieldImage | null | undefined;\n\n\t/**\n\t * An object of Imgix URL API parameters to transform the image.\n\t *\n\t * @see https://docs.imgix.com/apis/rendering\n\t */\n\timgixParams?: ImgixURLParams;\n\n\t/**\n\t * Declare an image as decorative by providing `alt=\"\"`.\n\t *\n\t * See:\n\t * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images\n\t */\n\talt?: \"\";\n\n\t/**\n\t * Declare an image as decorative only if the Image field does not have\n\t * alternative text by providing `fallbackAlt=\"\"`.\n\t *\n\t * See:\n\t * https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images\n\t */\n\tfallbackAlt?: \"\";\n\n\t/**\n\t * Rendered when the field is empty. If a fallback is not given, `null` will\n\t * be rendered.\n\t */\n\tfallback?: React.ReactNode;\n};\n\n/**\n * React component that renders an image from a Prismic Image field or one of\n * its thumbnails using `next/image`. It will automatically set the `alt`\n * attribute using the Image field's `alt` property.\n *\n * It uses an Imgix URL-based loader by default. A custom loader can be provided\n * with the `loader` prop. If you would like to use the Next.js Image\n * Optimization API instead, set `loader={undefined}`.\n *\n * @param props - Props for the component.\n *\n * @returns A responsive image component using `next/image` for the given Image\n * field.\n * @see To learn more about `next/image`, see: https://nextjs.org/docs/api-reference/next/image\n */\nexport const PrismicNextImage = ({\n\tfield,\n\timgixParams = {},\n\talt,\n\tfallbackAlt,\n\tfill,\n\twidth,\n\theight,\n\tfallback = null,\n\t...restProps\n}: PrismicNextImageProps): JSX.Element => {\n\tif (!__PRODUCTION__) {\n\t\tif (typeof alt === \"string\" && alt !== \"\") {\n\t\t\tconsole.warn(\n\t\t\t\t`[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(\n\t\t\t\t\t\"alt-must-be-an-empty-string\",\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\n\t\tif (typeof fallbackAlt === \"string\" && fallbackAlt !== \"\") {\n\t\t\tconsole.warn(\n\t\t\t\t`[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(\n\t\t\t\t\t\"alt-must-be-an-empty-string\",\n\t\t\t\t)}`,\n\t\t\t);\n\t\t}\n\t}\n\n\tif (prismicH.isFilled.imageThumbnail(field)) {\n\t\tconst src = buildURL(field.url, imgixParams);\n\n\t\tconst ar = field.dimensions.width / field.dimensions.height;\n\n\t\tconst castedWidth = castInt(width);\n\t\tconst castedHeight = castInt(height);\n\n\t\tlet resolvedWidth = castedWidth ?? field.dimensions.width;\n\t\tlet resolvedHeight = castedHeight ?? field.dimensions.width;\n\n\t\tif (castedWidth != null && castedHeight == null) {\n\t\t\tresolvedHeight = castedWidth / ar;\n\t\t} else if (castedWidth == null && castedHeight != null) {\n\t\t\tresolvedWidth = castedHeight * ar;\n\t\t}\n\n\t\treturn (\n\t\t\t<Image\n\t\t\t\tsrc={src}\n\t\t\t\twidth={fill ? undefined : resolvedWidth}\n\t\t\t\theight={fill ? undefined : resolvedHeight}\n\t\t\t\t// A non-null assertion is required since we\n\t\t\t\t// can't statically know if an alt attribute is\n\t\t\t\t// available.\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\t\t\talt={(alt ?? (field.alt || fallbackAlt))!}\n\t\t\t\tfill={fill}\n\t\t\t\tloader={imgixLoader}\n\t\t\t\t{...restProps}\n\t\t\t/>\n\t\t);\n\t} else {\n\t\treturn <>{fallback}</>;\n\t}\n};\n"],"names":["castInt","input","parsed","Number","parseInt","isNaN","undefined","PrismicNextImage","field","imgixParams","alt","fallbackAlt","fill","width","height","fallback","restProps","__PRODUCTION__","console","warn","devMsg","prismicH","isFilled","imageThumbnail","src","buildURL","url","ar","dimensions","castedWidth","castedHeight","resolvedWidth","resolvedHeight","_jsx","Image","loader","imgixLoader","_Fragment","children"],"mappings":";;;;;;;AAUA,MAAMA,UAAWC,CAA0D,UAAA;AAC1E,MAAI,OAAOA,UAAU,YAAY,OAAOA,UAAU,aAAa;AACvDA,WAAAA;AAAAA,EAAAA,OACD;AACAC,UAAAA,SAASC,OAAOC,SAASH,KAAK;AAEhCE,QAAAA,OAAOE,MAAMH,MAAM,GAAG;AAClBI,aAAAA;AAAAA,IAAAA,OACD;AACCJ,aAAAA;AAAAA,IACP;AAAA,EACD;AACF;AAsDO,MAAMK,mBAAmB,CAAC;AAAA,EAChCC;AAAAA,EACAC,cAAc,CAAE;AAAA,EAChBC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC,WAAW;AAAA,KACRC;AAAS,MAC4B;AACxC,MAAI,CAACC,gBAAgB;AACpB,QAAI,OAAOP,QAAQ,YAAYA,QAAQ,IAAI;AAC1CQ,cAAQC,KACkQ,yQAAAC,OACxQ,6BAA6B,GAC3B;AAAA,IAEJ;AAED,QAAI,OAAOT,gBAAgB,YAAYA,gBAAgB,IAAI;AAC1DO,cAAQC,KACkS,ySAAAC,OACxS,6BAA6B,GAC3B;AAAA,IAEJ;AAAA,EACD;AAED,MAAIC,SAASC,SAASC,eAAef,KAAK,GAAG;AAC5C,UAAMgB,MAAMC,SAASjB,MAAMkB,KAAKjB,WAAW;AAE3C,UAAMkB,KAAKnB,MAAMoB,WAAWf,QAAQL,MAAMoB,WAAWd;AAE/Ce,UAAAA,cAAc7B,QAAQa,KAAK;AAC3BiB,UAAAA,eAAe9B,QAAQc,MAAM;AAE/BiB,QAAAA,gBAAgBF,oCAAerB,MAAMoB,WAAWf;AAChDmB,QAAAA,iBAAiBF,sCAAgBtB,MAAMoB,WAAWf;AAElDgB,QAAAA,eAAe,QAAQC,gBAAgB,MAAM;AAChDE,uBAAiBH,cAAcF;AAAAA,IACrBE,WAAAA,eAAe,QAAQC,gBAAgB,MAAM;AACvDC,sBAAgBD,eAAeH;AAAAA,IAC/B;AAED,WACCM,IAACC,OAAK;AAAA,MACLV;AAAAA,MACAX,OAAOD,OAAON,SAAYyB;AAAAA,MAC1BjB,QAAQF,OAAON,SAAY0B;AAAAA,MAK3BtB,KAAMA,oBAAQF,MAAME,OAAOC;AAAAA,MAC3BC;AAAAA,MACAuB,QAAQC;AAAAA,MACJ,GAAApB;AAAAA,IAAAA,CACH;AAAA,EAAA,OAEG;AACN,WAAOiB,IAAAI,UAAA;AAAA,MAAAC,UAAGvB;AAAAA,IAAAA,CAAQ;AAAA,EAClB;AACF;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreview.cjs","sources":["../src/PrismicPreview.tsx"],"sourcesContent":["import * as React from \"react\";\nimport { useRouter } from \"next/router\";\nimport Script from \"next/script\";\n\nimport { getPrismicPreviewCookie } from \"./lib/getPrismicPreviewCookie\";\nimport { getPreviewCookieRepositoryName } from \"./lib/getPreviewCookieRepositoryName\";\n\n/**\n * Props for `<PrismicPreview>`.\n */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered\n\t * using this repository.\n\t */\n\trepositoryName: string;\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`).\n\t * This URL will be fetched on preview update events.\n\t *\n\t * **Note**: If your `next.config.js` file contains a `basePath`, it is\n\t * automatically included.\n\t */\n\tupdatePreviewURL?: string;\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`).\n\t * This URL will be fetched on preview exit events.\n\t *\n\t * **Note**: If your `next.config.js` file contains a `basePath`, it is\n\t * automatically included.\n\t */\n\texitPreviewURL?: string;\n\n\t/**\n\t * Children to render adjacent to the Prismic Toolbar. The Prismic Toolbar\n\t * will be rendered last.\n\t */\n\tchildren?: React.ReactNode;\n};\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When\n * the Prismic Toolbar send events to the browser, such as on preview updates\n * and exiting, this component will automatically update the Next.js preview\n * cookie and refresh the page.\n *\n * This component can be wrapped around your app or added anywhere in your app's\n * tree. It must be rendered on every page.\n */\nexport function PrismicPreview({\n\trepositoryName,\n\tchildren,\n\tupdatePreviewURL = \"/api/preview\",\n\texitPreviewURL = \"/api/exit-preview\",\n}: PrismicPreviewProps):
|
|
1
|
+
{"version":3,"file":"PrismicPreview.cjs","sources":["../src/PrismicPreview.tsx"],"sourcesContent":["import * as React from \"react\";\nimport { useRouter } from \"next/router\";\nimport Script from \"next/script\";\n\nimport { getPrismicPreviewCookie } from \"./lib/getPrismicPreviewCookie\";\nimport { getPreviewCookieRepositoryName } from \"./lib/getPreviewCookieRepositoryName\";\n\n/**\n * Props for `<PrismicPreview>`.\n */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered\n\t * using this repository.\n\t */\n\trepositoryName: string;\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`).\n\t * This URL will be fetched on preview update events.\n\t *\n\t * **Note**: If your `next.config.js` file contains a `basePath`, it is\n\t * automatically included.\n\t */\n\tupdatePreviewURL?: string;\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`).\n\t * This URL will be fetched on preview exit events.\n\t *\n\t * **Note**: If your `next.config.js` file contains a `basePath`, it is\n\t * automatically included.\n\t */\n\texitPreviewURL?: string;\n\n\t/**\n\t * Children to render adjacent to the Prismic Toolbar. The Prismic Toolbar\n\t * will be rendered last.\n\t */\n\tchildren?: React.ReactNode;\n};\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When\n * the Prismic Toolbar send events to the browser, such as on preview updates\n * and exiting, this component will automatically update the Next.js preview\n * cookie and refresh the page.\n *\n * This component can be wrapped around your app or added anywhere in your app's\n * tree. It must be rendered on every page.\n */\nexport function PrismicPreview({\n\trepositoryName,\n\tchildren,\n\tupdatePreviewURL = \"/api/preview\",\n\texitPreviewURL = \"/api/exit-preview\",\n}: PrismicPreviewProps): React.ReactNode {\n\tconst router = useRouter();\n\n\tconst resolvedUpdatePreviewURL = router.basePath + updatePreviewURL;\n\tconst resolvedExitPreviewURL = router.basePath + exitPreviewURL;\n\n\tReact.useEffect(() => {\n\t\t/**\n\t\t * Starts Preview Mode and refreshes the page's props.\n\t\t */\n\t\tconst startPreviewMode = async () => {\n\t\t\t// Start Next.js Preview Mode via the given preview API endpoint.\n\t\t\tconst res = await globalThis.fetch(resolvedUpdatePreviewURL);\n\n\t\t\tif (res.ok) {\n\t\t\t\tglobalThis.location.reload();\n\t\t\t} else {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[<PrismicPreview>] Failed to start or update Preview Mode using the \"${resolvedUpdatePreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t);\n\t\t\t}\n\t\t};\n\n\t\tconst handlePrismicPreviewUpdate = async (event: Event) => {\n\t\t\t// Prevent the toolbar from reloading the page.\n\t\t\tevent.preventDefault();\n\n\t\t\tawait startPreviewMode();\n\t\t};\n\n\t\tconst handlePrismicPreviewEnd = async (event: Event) => {\n\t\t\t// Prevent the toolbar from reloading the page.\n\t\t\tevent.preventDefault();\n\n\t\t\t// Exit Next.js Preview Mode via the given preview API endpoint.\n\t\t\tconst res = await globalThis.fetch(resolvedExitPreviewURL);\n\n\t\t\tif (res.ok) {\n\t\t\t\tglobalThis.location.reload();\n\t\t\t} else {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[<PrismicPreview>] Failed to exit Preview Mode using the \"${resolvedExitPreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t);\n\t\t\t}\n\t\t};\n\n\t\tif (router.isPreview) {\n\t\t\t// Register Prismic Toolbar event handlers.\n\t\t\twindow.addEventListener(\n\t\t\t\t\"prismicPreviewUpdate\",\n\t\t\t\thandlePrismicPreviewUpdate,\n\t\t\t);\n\t\t\twindow.addEventListener(\"prismicPreviewEnd\", handlePrismicPreviewEnd);\n\t\t} else {\n\t\t\tconst prismicPreviewCookie = getPrismicPreviewCookie(\n\t\t\t\tglobalThis.document.cookie,\n\t\t\t);\n\n\t\t\tif (prismicPreviewCookie) {\n\t\t\t\t// If a Prismic preview cookie is present, but Next.js Preview\n\t\t\t\t// Mode is not active, we must activate Preview Mode manually.\n\t\t\t\t//\n\t\t\t\t// This will happen when a visitor accesses the page using a\n\t\t\t\t// Prismic preview share link.\n\n\t\t\t\t/**\n\t\t\t\t * Determines if the current location is a descendant of the app's base\n\t\t\t\t * path.\n\t\t\t\t *\n\t\t\t\t * This is used to prevent infinite refrehes; when\n\t\t\t\t * `isDescendantOfBasePath` is `false`, `router.isPreview` is also\n\t\t\t\t * `false`.\n\t\t\t\t *\n\t\t\t\t * If the app does not have a base path, this should always be `true`.\n\t\t\t\t */\n\t\t\t\tconst locationIsDescendantOfBasePath = window.location.href.startsWith(\n\t\t\t\t\twindow.location.origin + router.basePath,\n\t\t\t\t);\n\n\t\t\t\tconst prismicPreviewCookieRepositoryName =\n\t\t\t\t\tgetPreviewCookieRepositoryName(prismicPreviewCookie);\n\n\t\t\t\tif (\n\t\t\t\t\tlocationIsDescendantOfBasePath &&\n\t\t\t\t\tprismicPreviewCookieRepositoryName === repositoryName\n\t\t\t\t) {\n\t\t\t\t\tstartPreviewMode();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// On cleanup, unregister Prismic Toolbar event handlers.\n\t\treturn () => {\n\t\t\twindow.removeEventListener(\n\t\t\t\t\"prismicPreviewUpdate\",\n\t\t\t\thandlePrismicPreviewUpdate,\n\t\t\t);\n\t\t\twindow.removeEventListener(\"prismicPreviewEnd\", handlePrismicPreviewEnd);\n\t\t};\n\t}, [\n\t\trepositoryName,\n\t\tresolvedExitPreviewURL,\n\t\tresolvedUpdatePreviewURL,\n\t\trouter.isPreview,\n\t\trouter.basePath,\n\t]);\n\n\treturn (\n\t\t<>\n\t\t\t{children}\n\t\t\t<Script\n\t\t\t\tsrc={`https://static.cdn.prismic.io/prismic.js?repo=${repositoryName}&new=true`}\n\t\t\t\tstrategy=\"lazyOnload\"\n\t\t\t/>\n\t\t</>\n\t);\n}\n"],"names":["PrismicPreview","repositoryName","children","updatePreviewURL","exitPreviewURL","router","useRouter","resolvedUpdatePreviewURL","basePath","resolvedExitPreviewURL","React","useEffect","startPreviewMode","res","globalThis","fetch","ok","location","reload","error","handlePrismicPreviewUpdate","event","preventDefault","handlePrismicPreviewEnd","isPreview","addEventListener","prismicPreviewCookie","getPrismicPreviewCookie","document","cookie","locationIsDescendantOfBasePath","window","href","startsWith","origin","prismicPreviewCookieRepositoryName","getPreviewCookieRepositoryName","removeEventListener","_jsxs","_Fragment","_jsx","Script","src","strategy"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAmDgB,SAAAA,eAAe;AAAA,EAC9BC;AAAAA,EACAC;AAAAA,EACAC,mBAAmB;AAAA,EACnBC,iBAAiB;AACI,GAAA;AACrB,QAAMC,WAASC,OAAAA;AAETC,QAAAA,2BAA2BF,SAAOG,WAAWL;AAC7CM,QAAAA,yBAAyBJ,SAAOG,WAAWJ;AAEjDM,mBAAMC,UAAU,MAAK;AAIpB,UAAMC,mBAAmB,YAAW;AAEnC,YAAMC,MAAM,MAAMC,WAAWC,MAAMR,wBAAwB;AAE3D,UAAIM,IAAIG,IAAI;AACXF,mBAAWG,SAASC;MAAQ,OACtB;AACEC,gBAAAA,8EACiEZ,wDAAwD;AAAA,MAEjI;AAAA,IAAA;AAGIa,UAAAA,6BAA6B,OAAOC,UAAgB;AAEzDA,YAAMC,eAAgB;AAEtB,YAAMV,iBAAkB;AAAA,IAAA;AAGnBW,UAAAA,0BAA0B,OAAOF,UAAgB;AAEtDA,YAAMC,eAAgB;AAGtB,YAAMT,MAAM,MAAMC,WAAWC,MAAMN,sBAAsB;AAEzD,UAAII,IAAIG,IAAI;AACXF,mBAAWG,SAASC;MAAQ,OACtB;AACEC,gBAAAA,mEACsDV,sDAAsD;AAAA,MAEpH;AAAA,IAAA;AAGF,QAAIJ,SAAOmB,WAAW;AAEdC,aAAAA,iBACN,wBACAL,0BAA0B;AAEpBK,aAAAA,iBAAiB,qBAAqBF,uBAAuB;AAAA,IAAA,OAC9D;AACN,YAAMG,uBAAuBC,wBAAAA,wBAC5Bb,WAAWc,SAASC,MAAM;AAG3B,UAAIH,sBAAsB;AAiBnBI,cAAAA,iCAAiCC,OAAOd,SAASe,KAAKC,WAC3DF,OAAOd,SAASiB,SAAS7B,SAAOG,QAAQ;AAGnC2B,cAAAA,qCACLC,8DAA+BV,oBAAoB;AAGnDI,YAAAA,kCACAK,uCAAuClC,gBACtC;AACiB;QAClB;AAAA,MACD;AAAA,IACD;AAGD,WAAO,MAAK;AACJoC,aAAAA,oBACN,wBACAjB,0BAA0B;AAEpBiB,aAAAA,oBAAoB,qBAAqBd,uBAAuB;AAAA,IAAA;AAAA,EACxE,GACE,CACFtB,gBACAQ,wBACAF,0BACAF,SAAOmB,WACPnB,SAAOG,QAAQ,CACf;AAED,SACC8B,WAAAA,KACEC,WAAAA,UAAA;AAAA,IAAArC,UAAA,CAAAA,UACDsC,WAAAA,IAACC;MACAC,sDAAsDzC;AAAAA,MACtD0C,UAAS;AAAA,IAAA,CACR,CAAA;AAAA,EAAA,CAAA;AAGL;;"}
|
package/dist/PrismicPreview.d.ts
CHANGED
|
@@ -39,4 +39,4 @@ export declare type PrismicPreviewProps = {
|
|
|
39
39
|
* This component can be wrapped around your app or added anywhere in your app's
|
|
40
40
|
* tree. It must be rendered on every page.
|
|
41
41
|
*/
|
|
42
|
-
export declare function PrismicPreview({ repositoryName, children, updatePreviewURL, exitPreviewURL, }: PrismicPreviewProps):
|
|
42
|
+
export declare function PrismicPreview({ repositoryName, children, updatePreviewURL, exitPreviewURL, }: PrismicPreviewProps): React.ReactNode;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreview.js","sources":["../src/PrismicPreview.tsx"],"sourcesContent":["import * as React from \"react\";\nimport { useRouter } from \"next/router\";\nimport Script from \"next/script\";\n\nimport { getPrismicPreviewCookie } from \"./lib/getPrismicPreviewCookie\";\nimport { getPreviewCookieRepositoryName } from \"./lib/getPreviewCookieRepositoryName\";\n\n/**\n * Props for `<PrismicPreview>`.\n */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered\n\t * using this repository.\n\t */\n\trepositoryName: string;\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`).\n\t * This URL will be fetched on preview update events.\n\t *\n\t * **Note**: If your `next.config.js` file contains a `basePath`, it is\n\t * automatically included.\n\t */\n\tupdatePreviewURL?: string;\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`).\n\t * This URL will be fetched on preview exit events.\n\t *\n\t * **Note**: If your `next.config.js` file contains a `basePath`, it is\n\t * automatically included.\n\t */\n\texitPreviewURL?: string;\n\n\t/**\n\t * Children to render adjacent to the Prismic Toolbar. The Prismic Toolbar\n\t * will be rendered last.\n\t */\n\tchildren?: React.ReactNode;\n};\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When\n * the Prismic Toolbar send events to the browser, such as on preview updates\n * and exiting, this component will automatically update the Next.js preview\n * cookie and refresh the page.\n *\n * This component can be wrapped around your app or added anywhere in your app's\n * tree. It must be rendered on every page.\n */\nexport function PrismicPreview({\n\trepositoryName,\n\tchildren,\n\tupdatePreviewURL = \"/api/preview\",\n\texitPreviewURL = \"/api/exit-preview\",\n}: PrismicPreviewProps):
|
|
1
|
+
{"version":3,"file":"PrismicPreview.js","sources":["../src/PrismicPreview.tsx"],"sourcesContent":["import * as React from \"react\";\nimport { useRouter } from \"next/router\";\nimport Script from \"next/script\";\n\nimport { getPrismicPreviewCookie } from \"./lib/getPrismicPreviewCookie\";\nimport { getPreviewCookieRepositoryName } from \"./lib/getPreviewCookieRepositoryName\";\n\n/**\n * Props for `<PrismicPreview>`.\n */\nexport type PrismicPreviewProps = {\n\t/**\n\t * The name of your Prismic repository. A Prismic Toolbar will be registered\n\t * using this repository.\n\t */\n\trepositoryName: string;\n\n\t/**\n\t * The URL of your app's Prismic preview endpoint (default: `/api/preview`).\n\t * This URL will be fetched on preview update events.\n\t *\n\t * **Note**: If your `next.config.js` file contains a `basePath`, it is\n\t * automatically included.\n\t */\n\tupdatePreviewURL?: string;\n\n\t/**\n\t * The URL of your app's exit preview endpoint (default: `/api/exit-preview`).\n\t * This URL will be fetched on preview exit events.\n\t *\n\t * **Note**: If your `next.config.js` file contains a `basePath`, it is\n\t * automatically included.\n\t */\n\texitPreviewURL?: string;\n\n\t/**\n\t * Children to render adjacent to the Prismic Toolbar. The Prismic Toolbar\n\t * will be rendered last.\n\t */\n\tchildren?: React.ReactNode;\n};\n\n/**\n * React component that sets up Prismic Previews using the Prismic Toolbar. When\n * the Prismic Toolbar send events to the browser, such as on preview updates\n * and exiting, this component will automatically update the Next.js preview\n * cookie and refresh the page.\n *\n * This component can be wrapped around your app or added anywhere in your app's\n * tree. It must be rendered on every page.\n */\nexport function PrismicPreview({\n\trepositoryName,\n\tchildren,\n\tupdatePreviewURL = \"/api/preview\",\n\texitPreviewURL = \"/api/exit-preview\",\n}: PrismicPreviewProps): React.ReactNode {\n\tconst router = useRouter();\n\n\tconst resolvedUpdatePreviewURL = router.basePath + updatePreviewURL;\n\tconst resolvedExitPreviewURL = router.basePath + exitPreviewURL;\n\n\tReact.useEffect(() => {\n\t\t/**\n\t\t * Starts Preview Mode and refreshes the page's props.\n\t\t */\n\t\tconst startPreviewMode = async () => {\n\t\t\t// Start Next.js Preview Mode via the given preview API endpoint.\n\t\t\tconst res = await globalThis.fetch(resolvedUpdatePreviewURL);\n\n\t\t\tif (res.ok) {\n\t\t\t\tglobalThis.location.reload();\n\t\t\t} else {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[<PrismicPreview>] Failed to start or update Preview Mode using the \"${resolvedUpdatePreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t);\n\t\t\t}\n\t\t};\n\n\t\tconst handlePrismicPreviewUpdate = async (event: Event) => {\n\t\t\t// Prevent the toolbar from reloading the page.\n\t\t\tevent.preventDefault();\n\n\t\t\tawait startPreviewMode();\n\t\t};\n\n\t\tconst handlePrismicPreviewEnd = async (event: Event) => {\n\t\t\t// Prevent the toolbar from reloading the page.\n\t\t\tevent.preventDefault();\n\n\t\t\t// Exit Next.js Preview Mode via the given preview API endpoint.\n\t\t\tconst res = await globalThis.fetch(resolvedExitPreviewURL);\n\n\t\t\tif (res.ok) {\n\t\t\t\tglobalThis.location.reload();\n\t\t\t} else {\n\t\t\t\tconsole.error(\n\t\t\t\t\t`[<PrismicPreview>] Failed to exit Preview Mode using the \"${resolvedExitPreviewURL}\" API endpoint. Does it exist?`,\n\t\t\t\t);\n\t\t\t}\n\t\t};\n\n\t\tif (router.isPreview) {\n\t\t\t// Register Prismic Toolbar event handlers.\n\t\t\twindow.addEventListener(\n\t\t\t\t\"prismicPreviewUpdate\",\n\t\t\t\thandlePrismicPreviewUpdate,\n\t\t\t);\n\t\t\twindow.addEventListener(\"prismicPreviewEnd\", handlePrismicPreviewEnd);\n\t\t} else {\n\t\t\tconst prismicPreviewCookie = getPrismicPreviewCookie(\n\t\t\t\tglobalThis.document.cookie,\n\t\t\t);\n\n\t\t\tif (prismicPreviewCookie) {\n\t\t\t\t// If a Prismic preview cookie is present, but Next.js Preview\n\t\t\t\t// Mode is not active, we must activate Preview Mode manually.\n\t\t\t\t//\n\t\t\t\t// This will happen when a visitor accesses the page using a\n\t\t\t\t// Prismic preview share link.\n\n\t\t\t\t/**\n\t\t\t\t * Determines if the current location is a descendant of the app's base\n\t\t\t\t * path.\n\t\t\t\t *\n\t\t\t\t * This is used to prevent infinite refrehes; when\n\t\t\t\t * `isDescendantOfBasePath` is `false`, `router.isPreview` is also\n\t\t\t\t * `false`.\n\t\t\t\t *\n\t\t\t\t * If the app does not have a base path, this should always be `true`.\n\t\t\t\t */\n\t\t\t\tconst locationIsDescendantOfBasePath = window.location.href.startsWith(\n\t\t\t\t\twindow.location.origin + router.basePath,\n\t\t\t\t);\n\n\t\t\t\tconst prismicPreviewCookieRepositoryName =\n\t\t\t\t\tgetPreviewCookieRepositoryName(prismicPreviewCookie);\n\n\t\t\t\tif (\n\t\t\t\t\tlocationIsDescendantOfBasePath &&\n\t\t\t\t\tprismicPreviewCookieRepositoryName === repositoryName\n\t\t\t\t) {\n\t\t\t\t\tstartPreviewMode();\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// On cleanup, unregister Prismic Toolbar event handlers.\n\t\treturn () => {\n\t\t\twindow.removeEventListener(\n\t\t\t\t\"prismicPreviewUpdate\",\n\t\t\t\thandlePrismicPreviewUpdate,\n\t\t\t);\n\t\t\twindow.removeEventListener(\"prismicPreviewEnd\", handlePrismicPreviewEnd);\n\t\t};\n\t}, [\n\t\trepositoryName,\n\t\tresolvedExitPreviewURL,\n\t\tresolvedUpdatePreviewURL,\n\t\trouter.isPreview,\n\t\trouter.basePath,\n\t]);\n\n\treturn (\n\t\t<>\n\t\t\t{children}\n\t\t\t<Script\n\t\t\t\tsrc={`https://static.cdn.prismic.io/prismic.js?repo=${repositoryName}&new=true`}\n\t\t\t\tstrategy=\"lazyOnload\"\n\t\t\t/>\n\t\t</>\n\t);\n}\n"],"names":["PrismicPreview","repositoryName","children","updatePreviewURL","exitPreviewURL","router","useRouter","resolvedUpdatePreviewURL","basePath","resolvedExitPreviewURL","React","useEffect","startPreviewMode","res","globalThis","fetch","ok","location","reload","error","handlePrismicPreviewUpdate","event","preventDefault","handlePrismicPreviewEnd","isPreview","addEventListener","prismicPreviewCookie","getPrismicPreviewCookie","document","cookie","locationIsDescendantOfBasePath","window","href","startsWith","origin","prismicPreviewCookieRepositoryName","getPreviewCookieRepositoryName","removeEventListener","_jsxs","_Fragment","_jsx","Script","src","strategy"],"mappings":";;;;;;AAmDgB,SAAAA,eAAe;AAAA,EAC9BC;AAAAA,EACAC;AAAAA,EACAC,mBAAmB;AAAA,EACnBC,iBAAiB;AACI,GAAA;AACrB,QAAMC,SAASC;AAETC,QAAAA,2BAA2BF,OAAOG,WAAWL;AAC7CM,QAAAA,yBAAyBJ,OAAOG,WAAWJ;AAEjDM,QAAMC,UAAU,MAAK;AAIpB,UAAMC,mBAAmB,YAAW;AAEnC,YAAMC,MAAM,MAAMC,WAAWC,MAAMR,wBAAwB;AAE3D,UAAIM,IAAIG,IAAI;AACXF,mBAAWG,SAASC;MAAQ,OACtB;AACEC,gBAAAA,8EACiEZ,wDAAwD;AAAA,MAEjI;AAAA,IAAA;AAGIa,UAAAA,6BAA6B,OAAOC,UAAgB;AAEzDA,YAAMC,eAAgB;AAEtB,YAAMV,iBAAkB;AAAA,IAAA;AAGnBW,UAAAA,0BAA0B,OAAOF,UAAgB;AAEtDA,YAAMC,eAAgB;AAGtB,YAAMT,MAAM,MAAMC,WAAWC,MAAMN,sBAAsB;AAEzD,UAAII,IAAIG,IAAI;AACXF,mBAAWG,SAASC;MAAQ,OACtB;AACEC,gBAAAA,mEACsDV,sDAAsD;AAAA,MAEpH;AAAA,IAAA;AAGF,QAAIJ,OAAOmB,WAAW;AAEdC,aAAAA,iBACN,wBACAL,0BAA0B;AAEpBK,aAAAA,iBAAiB,qBAAqBF,uBAAuB;AAAA,IAAA,OAC9D;AACN,YAAMG,uBAAuBC,wBAC5Bb,WAAWc,SAASC,MAAM;AAG3B,UAAIH,sBAAsB;AAiBnBI,cAAAA,iCAAiCC,OAAOd,SAASe,KAAKC,WAC3DF,OAAOd,SAASiB,SAAS7B,OAAOG,QAAQ;AAGnC2B,cAAAA,qCACLC,+BAA+BV,oBAAoB;AAGnDI,YAAAA,kCACAK,uCAAuClC,gBACtC;AACiB;QAClB;AAAA,MACD;AAAA,IACD;AAGD,WAAO,MAAK;AACJoC,aAAAA,oBACN,wBACAjB,0BAA0B;AAEpBiB,aAAAA,oBAAoB,qBAAqBd,uBAAuB;AAAA,IAAA;AAAA,EACxE,GACE,CACFtB,gBACAQ,wBACAF,0BACAF,OAAOmB,WACPnB,OAAOG,QAAQ,CACf;AAED,SACC8B,KACEC,UAAA;AAAA,IAAArC,UAAA,CAAAA,UACDsC,IAACC;MACAC,sDAAsDzC;AAAAA,MACtD0C,UAAS;AAAA,IAAA,CACR,CAAA;AAAA,EAAA,CAAA;AAGL;"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const imgixUrlBuilder = require("imgix-url-builder");
|
|
4
|
+
const imgixLoader = (args) => {
|
|
5
|
+
const url = new URL(args.src);
|
|
6
|
+
const params = {
|
|
7
|
+
fit: url.searchParams.get("fit") || "max",
|
|
8
|
+
w: args.width,
|
|
9
|
+
h: void 0
|
|
10
|
+
};
|
|
11
|
+
if (args.quality) {
|
|
12
|
+
params.q = args.quality;
|
|
13
|
+
}
|
|
14
|
+
return imgixUrlBuilder.buildURL(args.src, params);
|
|
15
|
+
};
|
|
16
|
+
exports.imgixLoader = imgixLoader;
|
|
17
|
+
//# sourceMappingURL=imgixLoader.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imgixLoader.cjs","sources":["../src/imgixLoader.ts"],"sourcesContent":["import { ImageLoaderProps } from \"next/image\";\nimport { buildURL, ImgixURLParams } from \"imgix-url-builder\";\n\n/**\n * A `next/image` loader for Imgix, which Prismic uses, with an optional\n * collection of default Imgix parameters.\n *\n * @see To learn about `next/image` loaders: https://nextjs.org/docs/api-reference/next/image#loader\n * @see To learn about Imgix's URL API: https://docs.imgix.com/apis/rendering\n */\nexport const imgixLoader = (args: ImageLoaderProps): string => {\n\tconst url = new URL(args.src);\n\n\tconst params: ImgixURLParams = {\n\t\tfit: (url.searchParams.get(\"fit\") as ImgixURLParams[\"fit\"]) || \"max\",\n\t\tw: args.width,\n\t\th: undefined,\n\t};\n\n\tif (args.quality) {\n\t\tparams.q = args.quality;\n\t}\n\n\treturn buildURL(args.src, params);\n};\n"],"names":["buildURL"],"mappings":";;;AAUa,MAAA,cAAc,CAAC,SAAkC;AAC7D,QAAM,MAAM,IAAI,IAAI,KAAK,GAAG;AAE5B,QAAM,SAAyB;AAAA,IAC9B,KAAM,IAAI,aAAa,IAAI,KAAK,KAA+B;AAAA,IAC/D,GAAG,KAAK;AAAA,IACR,GAAG;AAAA,EAAA;AAGJ,MAAI,KAAK,SAAS;AACjB,WAAO,IAAI,KAAK;AAAA,EAChB;AAEM,SAAAA,yBAAS,KAAK,KAAK,MAAM;AACjC;;"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ImageLoaderProps } from "next/image";
|
|
2
|
+
/**
|
|
3
|
+
* A `next/image` loader for Imgix, which Prismic uses, with an optional
|
|
4
|
+
* collection of default Imgix parameters.
|
|
5
|
+
*
|
|
6
|
+
* @see To learn about `next/image` loaders: https://nextjs.org/docs/api-reference/next/image#loader
|
|
7
|
+
* @see To learn about Imgix's URL API: https://docs.imgix.com/apis/rendering
|
|
8
|
+
*/
|
|
9
|
+
export declare const imgixLoader: (args: ImageLoaderProps) => string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { buildURL } from "imgix-url-builder";
|
|
2
|
+
const imgixLoader = (args) => {
|
|
3
|
+
const url = new URL(args.src);
|
|
4
|
+
const params = {
|
|
5
|
+
fit: url.searchParams.get("fit") || "max",
|
|
6
|
+
w: args.width,
|
|
7
|
+
h: void 0
|
|
8
|
+
};
|
|
9
|
+
if (args.quality) {
|
|
10
|
+
params.q = args.quality;
|
|
11
|
+
}
|
|
12
|
+
return buildURL(args.src, params);
|
|
13
|
+
};
|
|
14
|
+
export {
|
|
15
|
+
imgixLoader
|
|
16
|
+
};
|
|
17
|
+
//# sourceMappingURL=imgixLoader.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imgixLoader.js","sources":["../src/imgixLoader.ts"],"sourcesContent":["import { ImageLoaderProps } from \"next/image\";\nimport { buildURL, ImgixURLParams } from \"imgix-url-builder\";\n\n/**\n * A `next/image` loader for Imgix, which Prismic uses, with an optional\n * collection of default Imgix parameters.\n *\n * @see To learn about `next/image` loaders: https://nextjs.org/docs/api-reference/next/image#loader\n * @see To learn about Imgix's URL API: https://docs.imgix.com/apis/rendering\n */\nexport const imgixLoader = (args: ImageLoaderProps): string => {\n\tconst url = new URL(args.src);\n\n\tconst params: ImgixURLParams = {\n\t\tfit: (url.searchParams.get(\"fit\") as ImgixURLParams[\"fit\"]) || \"max\",\n\t\tw: args.width,\n\t\th: undefined,\n\t};\n\n\tif (args.quality) {\n\t\tparams.q = args.quality;\n\t}\n\n\treturn buildURL(args.src, params);\n};\n"],"names":[],"mappings":";AAUa,MAAA,cAAc,CAAC,SAAkC;AAC7D,QAAM,MAAM,IAAI,IAAI,KAAK,GAAG;AAE5B,QAAM,SAAyB;AAAA,IAC9B,KAAM,IAAI,aAAa,IAAI,KAAK,KAA+B;AAAA,IAC/D,GAAG,KAAK;AAAA,IACR,GAAG;AAAA,EAAA;AAGJ,MAAI,KAAK,SAAS;AACjB,WAAO,IAAI,KAAK;AAAA,EAChB;AAEM,SAAA,SAAS,KAAK,KAAK,MAAM;AACjC;"}
|
package/dist/index.cjs
CHANGED
|
@@ -6,10 +6,12 @@ const PrismicPreview = require("./PrismicPreview.cjs");
|
|
|
6
6
|
const enableAutoPreviews = require("./enableAutoPreviews.cjs");
|
|
7
7
|
const redirectToPreviewURL = require("./redirectToPreviewURL.cjs");
|
|
8
8
|
const PrismicNextImage = require("./PrismicNextImage.cjs");
|
|
9
|
+
const imgixLoader = require("./imgixLoader.cjs");
|
|
9
10
|
exports.setPreviewData = setPreviewData.setPreviewData;
|
|
10
11
|
exports.exitPreview = exitPreview.exitPreview;
|
|
11
12
|
exports.PrismicPreview = PrismicPreview.PrismicPreview;
|
|
12
13
|
exports.enableAutoPreviews = enableAutoPreviews.enableAutoPreviews;
|
|
13
14
|
exports.redirectToPreviewURL = redirectToPreviewURL.redirectToPreviewURL;
|
|
14
15
|
exports.PrismicNextImage = PrismicNextImage.PrismicNextImage;
|
|
16
|
+
exports.imgixLoader = imgixLoader.imgixLoader;
|
|
15
17
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
export { setPreviewData } from "./setPreviewData";
|
|
2
|
-
export
|
|
3
|
-
export {
|
|
4
|
-
export
|
|
5
|
-
export {
|
|
6
|
-
export
|
|
7
|
-
export {
|
|
8
|
-
export
|
|
9
|
-
export { redirectToPreviewURL } from "./redirectToPreviewURL";
|
|
10
|
-
export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL";
|
|
11
|
-
export { PrismicNextImage } from "./PrismicNextImage";
|
|
12
|
-
export type { PrismicNextImageProps } from "./PrismicNextImage";
|
|
13
|
-
export type { CreateClientConfig } from "./types";
|
|
1
|
+
export { setPreviewData, SetPreviewDataConfig } from "./setPreviewData";
|
|
2
|
+
export { exitPreview, ExitPreviewConfig } from "./exitPreview";
|
|
3
|
+
export { PrismicPreview, PrismicPreviewProps } from "./PrismicPreview";
|
|
4
|
+
export { EnableAutoPreviewsConfig, enableAutoPreviews, } from "./enableAutoPreviews";
|
|
5
|
+
export { RedirectToPreviewURLConfig, redirectToPreviewURL, } from "./redirectToPreviewURL";
|
|
6
|
+
export { PrismicNextImage, PrismicNextImageProps } from "./PrismicNextImage";
|
|
7
|
+
export { imgixLoader } from "./imgixLoader";
|
|
8
|
+
export { CreateClientConfig } from "./types";
|
package/dist/index.js
CHANGED
|
@@ -4,11 +4,13 @@ import { PrismicPreview } from "./PrismicPreview.js";
|
|
|
4
4
|
import { enableAutoPreviews } from "./enableAutoPreviews.js";
|
|
5
5
|
import { redirectToPreviewURL } from "./redirectToPreviewURL.js";
|
|
6
6
|
import { PrismicNextImage } from "./PrismicNextImage.js";
|
|
7
|
+
import { imgixLoader } from "./imgixLoader.js";
|
|
7
8
|
export {
|
|
8
9
|
PrismicNextImage,
|
|
9
10
|
PrismicPreview,
|
|
10
11
|
enableAutoPreviews,
|
|
11
12
|
exitPreview,
|
|
13
|
+
imgixLoader,
|
|
12
14
|
redirectToPreviewURL,
|
|
13
15
|
setPreviewData
|
|
14
16
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;"}
|
package/dist/package.cjs
CHANGED
package/dist/package.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/next",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.2",
|
|
4
4
|
"description": "Helpers to integrate Prismic into Next.js apps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"unit:watch": "vitest watch"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@prismicio/client": "^6.7.1",
|
|
50
49
|
"@prismicio/helpers": "^2.3.5",
|
|
51
50
|
"@prismicio/types": "^0.2.3",
|
|
52
51
|
"imgix-url-builder": "^0.0.3"
|
|
53
52
|
},
|
|
54
53
|
"devDependencies": {
|
|
54
|
+
"@prismicio/client": "^6.7.1",
|
|
55
55
|
"@prismicio/mock": "^0.1.1",
|
|
56
56
|
"@size-limit/preset-small-lib": "^8.1.0",
|
|
57
57
|
"@types/react-test-renderer": "^18.0.0",
|
|
@@ -67,10 +67,10 @@
|
|
|
67
67
|
"eslint-plugin-tsdoc": "^0.2.17",
|
|
68
68
|
"happy-dom": "^7.5.13",
|
|
69
69
|
"next": "^13.0.0",
|
|
70
|
-
"nyc": "^15.1.0",
|
|
71
70
|
"prettier": "^2.7.1",
|
|
72
71
|
"prettier-plugin-jsdoc": "^0.4.2",
|
|
73
72
|
"react": "^18.1.0",
|
|
73
|
+
"react-dom": "^18.2.0",
|
|
74
74
|
"react-test-renderer": "^18.2.0",
|
|
75
75
|
"size-limit": "^8.1.0",
|
|
76
76
|
"standard-version": "^9.5.0",
|
|
@@ -80,9 +80,9 @@
|
|
|
80
80
|
"vitest": "^0.24.3"
|
|
81
81
|
},
|
|
82
82
|
"peerDependencies": {
|
|
83
|
-
"@prismicio/client": "^6
|
|
84
|
-
"next": "^
|
|
85
|
-
"react": "^
|
|
83
|
+
"@prismicio/client": "^6",
|
|
84
|
+
"next": "^13",
|
|
85
|
+
"react": "^18"
|
|
86
86
|
},
|
|
87
87
|
"engines": {
|
|
88
88
|
"node": ">=14.15.0"
|
package/src/PrismicNextImage.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import Image, { ImageProps
|
|
1
|
+
import Image, { ImageProps } from "next/image";
|
|
2
2
|
import { buildURL, ImgixURLParams } from "imgix-url-builder";
|
|
3
3
|
import * as prismicH from "@prismicio/helpers";
|
|
4
4
|
import * as prismicT from "@prismicio/types";
|
|
@@ -6,12 +6,7 @@ import * as prismicT from "@prismicio/types";
|
|
|
6
6
|
import { __PRODUCTION__ } from "./lib/__PRODUCTION__";
|
|
7
7
|
import { devMsg } from "./lib/devMsg";
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
* Value for `next/image`'s `layout` prop which existed in Next 12 and lower.
|
|
11
|
-
* `<PrismicNextImage>` is comatible with the current and legacy image
|
|
12
|
-
* components, requiring us to handle both APIs.
|
|
13
|
-
*/
|
|
14
|
-
type LegacyImageLayout = "intrinsic" | "fixed" | "responsive" | "fill";
|
|
9
|
+
import { imgixLoader } from "./imgixLoader";
|
|
15
10
|
|
|
16
11
|
const castInt = (input: string | number | undefined): number | undefined => {
|
|
17
12
|
if (typeof input === "number" || typeof input === "undefined") {
|
|
@@ -27,29 +22,6 @@ const castInt = (input: string | number | undefined): number | undefined => {
|
|
|
27
22
|
}
|
|
28
23
|
};
|
|
29
24
|
|
|
30
|
-
/**
|
|
31
|
-
* Creates a `next/image` loader for Imgix, which Prismic uses, with an optional
|
|
32
|
-
* collection of default Imgix parameters.
|
|
33
|
-
*
|
|
34
|
-
* @see To learn about `next/image` loaders: https://nextjs.org/docs/api-reference/next/image#loader
|
|
35
|
-
* @see To learn about Imgix's URL API: https://docs.imgix.com/apis/rendering
|
|
36
|
-
*/
|
|
37
|
-
const imgixLoader = (args: ImageLoaderProps): string => {
|
|
38
|
-
const url = new URL(args.src);
|
|
39
|
-
|
|
40
|
-
const params: ImgixURLParams = {
|
|
41
|
-
fit: (url.searchParams.get("fit") as ImgixURLParams["fit"]) || "max",
|
|
42
|
-
w: args.width,
|
|
43
|
-
h: undefined,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
if (args.quality) {
|
|
47
|
-
params.q = args.quality;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return buildURL(args.src, params);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
25
|
export type PrismicNextImageProps = Omit<ImageProps, "src" | "alt"> & {
|
|
54
26
|
/**
|
|
55
27
|
* The Prismic Image field or thumbnail to render.
|
|
@@ -79,6 +51,12 @@ export type PrismicNextImageProps = Omit<ImageProps, "src" | "alt"> & {
|
|
|
79
51
|
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
80
52
|
*/
|
|
81
53
|
fallbackAlt?: "";
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Rendered when the field is empty. If a fallback is not given, `null` will
|
|
57
|
+
* be rendered.
|
|
58
|
+
*/
|
|
59
|
+
fallback?: React.ReactNode;
|
|
82
60
|
};
|
|
83
61
|
|
|
84
62
|
/**
|
|
@@ -101,8 +79,12 @@ export const PrismicNextImage = ({
|
|
|
101
79
|
imgixParams = {},
|
|
102
80
|
alt,
|
|
103
81
|
fallbackAlt,
|
|
82
|
+
fill,
|
|
83
|
+
width,
|
|
84
|
+
height,
|
|
85
|
+
fallback = null,
|
|
104
86
|
...restProps
|
|
105
|
-
}: PrismicNextImageProps) => {
|
|
87
|
+
}: PrismicNextImageProps): JSX.Element => {
|
|
106
88
|
if (!__PRODUCTION__) {
|
|
107
89
|
if (typeof alt === "string" && alt !== "") {
|
|
108
90
|
console.warn(
|
|
@@ -123,51 +105,37 @@ export const PrismicNextImage = ({
|
|
|
123
105
|
|
|
124
106
|
if (prismicH.isFilled.imageThumbnail(field)) {
|
|
125
107
|
const src = buildURL(field.url, imgixParams);
|
|
108
|
+
|
|
126
109
|
const ar = field.dimensions.width / field.dimensions.height;
|
|
127
110
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (
|
|
139
|
-
(layout === "intrinsic" || layout === "fixed") &&
|
|
140
|
-
("width" in restProps || "height" in restProps)
|
|
141
|
-
) {
|
|
142
|
-
const castedWidth = castInt(restProps.width);
|
|
143
|
-
const castedHeight = castInt(restProps.height);
|
|
144
|
-
|
|
145
|
-
if (castedWidth) {
|
|
146
|
-
resolvedWidth = castedWidth;
|
|
147
|
-
} else {
|
|
148
|
-
if (castedHeight) {
|
|
149
|
-
resolvedWidth = ar * castedHeight;
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
resolvedHeight = resolvedWidth / ar;
|
|
111
|
+
const castedWidth = castInt(width);
|
|
112
|
+
const castedHeight = castInt(height);
|
|
113
|
+
|
|
114
|
+
let resolvedWidth = castedWidth ?? field.dimensions.width;
|
|
115
|
+
let resolvedHeight = castedHeight ?? field.dimensions.width;
|
|
116
|
+
|
|
117
|
+
if (castedWidth != null && castedHeight == null) {
|
|
118
|
+
resolvedHeight = castedWidth / ar;
|
|
119
|
+
} else if (castedWidth == null && castedHeight != null) {
|
|
120
|
+
resolvedWidth = castedHeight * ar;
|
|
154
121
|
}
|
|
155
122
|
|
|
156
123
|
return (
|
|
157
124
|
<Image
|
|
158
125
|
src={src}
|
|
159
|
-
width={
|
|
160
|
-
height={
|
|
126
|
+
width={fill ? undefined : resolvedWidth}
|
|
127
|
+
height={fill ? undefined : resolvedHeight}
|
|
161
128
|
// A non-null assertion is required since we
|
|
162
129
|
// can't statically know if an alt attribute is
|
|
163
130
|
// available.
|
|
164
131
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
165
132
|
alt={(alt ?? (field.alt || fallbackAlt))!}
|
|
133
|
+
fill={fill}
|
|
166
134
|
loader={imgixLoader}
|
|
167
135
|
{...restProps}
|
|
168
136
|
/>
|
|
169
137
|
);
|
|
170
138
|
} else {
|
|
171
|
-
return
|
|
139
|
+
return <>{fallback}</>;
|
|
172
140
|
}
|
|
173
141
|
};
|
package/src/PrismicPreview.tsx
CHANGED
|
@@ -54,7 +54,7 @@ export function PrismicPreview({
|
|
|
54
54
|
children,
|
|
55
55
|
updatePreviewURL = "/api/preview",
|
|
56
56
|
exitPreviewURL = "/api/exit-preview",
|
|
57
|
-
}: PrismicPreviewProps):
|
|
57
|
+
}: PrismicPreviewProps): React.ReactNode {
|
|
58
58
|
const router = useRouter();
|
|
59
59
|
|
|
60
60
|
const resolvedUpdatePreviewURL = router.basePath + updatePreviewURL;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { ImageLoaderProps } from "next/image";
|
|
2
|
+
import { buildURL, ImgixURLParams } from "imgix-url-builder";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A `next/image` loader for Imgix, which Prismic uses, with an optional
|
|
6
|
+
* collection of default Imgix parameters.
|
|
7
|
+
*
|
|
8
|
+
* @see To learn about `next/image` loaders: https://nextjs.org/docs/api-reference/next/image#loader
|
|
9
|
+
* @see To learn about Imgix's URL API: https://docs.imgix.com/apis/rendering
|
|
10
|
+
*/
|
|
11
|
+
export const imgixLoader = (args: ImageLoaderProps): string => {
|
|
12
|
+
const url = new URL(args.src);
|
|
13
|
+
|
|
14
|
+
const params: ImgixURLParams = {
|
|
15
|
+
fit: (url.searchParams.get("fit") as ImgixURLParams["fit"]) || "max",
|
|
16
|
+
w: args.width,
|
|
17
|
+
h: undefined,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
if (args.quality) {
|
|
21
|
+
params.q = args.quality;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return buildURL(args.src, params);
|
|
25
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
export { setPreviewData } from "./setPreviewData";
|
|
2
|
-
export type { SetPreviewDataConfig } from "./setPreviewData";
|
|
1
|
+
export { setPreviewData, SetPreviewDataConfig } from "./setPreviewData";
|
|
3
2
|
|
|
4
|
-
export { exitPreview } from "./exitPreview";
|
|
5
|
-
export type { ExitPreviewConfig } from "./exitPreview";
|
|
3
|
+
export { exitPreview, ExitPreviewConfig } from "./exitPreview";
|
|
6
4
|
|
|
7
|
-
export { PrismicPreview } from "./PrismicPreview";
|
|
8
|
-
export type { PrismicPreviewProps } from "./PrismicPreview";
|
|
5
|
+
export { PrismicPreview, PrismicPreviewProps } from "./PrismicPreview";
|
|
9
6
|
|
|
10
|
-
export {
|
|
11
|
-
|
|
7
|
+
export {
|
|
8
|
+
EnableAutoPreviewsConfig,
|
|
9
|
+
enableAutoPreviews,
|
|
10
|
+
} from "./enableAutoPreviews";
|
|
12
11
|
|
|
13
|
-
export {
|
|
14
|
-
|
|
12
|
+
export {
|
|
13
|
+
RedirectToPreviewURLConfig,
|
|
14
|
+
redirectToPreviewURL,
|
|
15
|
+
} from "./redirectToPreviewURL";
|
|
15
16
|
|
|
16
|
-
export { PrismicNextImage } from "./PrismicNextImage";
|
|
17
|
-
export type { PrismicNextImageProps } from "./PrismicNextImage";
|
|
17
|
+
export { PrismicNextImage, PrismicNextImageProps } from "./PrismicNextImage";
|
|
18
18
|
|
|
19
|
-
export
|
|
19
|
+
export { imgixLoader } from "./imgixLoader";
|
|
20
|
+
|
|
21
|
+
export { CreateClientConfig } from "./types";
|