@prismicio/next 0.1.2 → 0.1.4
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/README.md +3 -1
- package/dist/PrismicNextImage.mjs +60 -0
- package/dist/PrismicPreview.mjs +87 -0
- package/dist/enableAutoPreviews.mjs +15 -0
- package/dist/exitPreview.mjs +6 -0
- package/dist/index.d.ts +137 -64
- package/dist/index.mjs +6 -0
- package/dist/lib/__PRODUCTION__.mjs +3 -0
- package/dist/lib/devMsg.mjs +7 -0
- package/dist/lib/getPreviewCookieRepositoryName.mjs +5 -0
- package/dist/lib/getPrismicPreviewCookie.mjs +20 -0
- package/dist/package.mjs +3 -0
- package/dist/redirectToPreviewURL.mjs +20 -0
- package/dist/setPreviewData.mjs +10 -0
- package/package.json +35 -41
- package/src/PrismicNextImage.tsx +126 -0
- package/src/PrismicPreview.tsx +92 -55
- package/src/enableAutoPreviews.ts +8 -8
- package/src/exitPreview.ts +23 -22
- package/src/index.ts +14 -5
- package/src/lib/__PRODUCTION__.ts +7 -0
- package/src/lib/devMsg.ts +20 -0
- package/src/lib/getPreviewCookieRepositoryName.ts +14 -0
- package/src/lib/getPrismicPreviewCookie.ts +33 -0
- package/src/redirectToPreviewURL.ts +79 -21
- package/src/types.ts +3 -49
- package/dist/index.cjs +0 -184
- package/dist/index.js +0 -153
- package/src/lib/extractPreviewRefRepositoryName.ts +0 -60
- package/src/lib/getCookie.ts +0 -58
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
Helpers to integrate Prismic into Next.js apps.
|
|
11
11
|
|
|
12
12
|
- 👁️ Easily set up Prismic Preview
|
|
13
|
-
-
|
|
13
|
+
- 🖼️ Render optimized images using [`next/image`][next-image] and Prismic's built-in [Imgix][imgix] integration
|
|
14
14
|
|
|
15
15
|
## Install
|
|
16
16
|
|
|
@@ -57,6 +57,8 @@ For more clarity on this project and its structure you can also check out the de
|
|
|
57
57
|
<!-- Links -->
|
|
58
58
|
|
|
59
59
|
[prismic]: https://prismic.io
|
|
60
|
+
[imgix]: https://imgix.com/
|
|
61
|
+
[next-image]: https://nextjs.org/docs/basic-features/image-optimization
|
|
60
62
|
|
|
61
63
|
<!-- TODO: Replace link with a more useful one if available -->
|
|
62
64
|
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { jsx } from 'react/jsx-runtime';
|
|
2
|
+
import Image from 'next/image';
|
|
3
|
+
import { buildURL } from 'imgix-url-builder';
|
|
4
|
+
import * as prismicH from '@prismicio/helpers';
|
|
5
|
+
import { __PRODUCTION__ } from './lib/__PRODUCTION__.mjs';
|
|
6
|
+
import { devMsg } from './lib/devMsg.mjs';
|
|
7
|
+
|
|
8
|
+
const imgixLoader = (args) => {
|
|
9
|
+
const url = new URL(args.src);
|
|
10
|
+
const params = {
|
|
11
|
+
fit: url.searchParams.get("fit") || "max",
|
|
12
|
+
w: args.width,
|
|
13
|
+
h: void 0
|
|
14
|
+
};
|
|
15
|
+
if (args.quality) {
|
|
16
|
+
params.q = args.quality;
|
|
17
|
+
}
|
|
18
|
+
return buildURL(args.src, params);
|
|
19
|
+
};
|
|
20
|
+
const PrismicNextImage = ({
|
|
21
|
+
field,
|
|
22
|
+
imgixParams = {},
|
|
23
|
+
alt,
|
|
24
|
+
fallbackAlt,
|
|
25
|
+
layout,
|
|
26
|
+
...restProps
|
|
27
|
+
}) => {
|
|
28
|
+
if (!__PRODUCTION__) {
|
|
29
|
+
if (typeof alt === "string" && alt !== "") {
|
|
30
|
+
console.warn(
|
|
31
|
+
`[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(
|
|
32
|
+
"alt-must-be-an-empty-string"
|
|
33
|
+
)}`
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
if (typeof fallbackAlt === "string" && fallbackAlt !== "") {
|
|
37
|
+
console.warn(
|
|
38
|
+
`[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(
|
|
39
|
+
"alt-must-be-an-empty-string"
|
|
40
|
+
)}`
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (prismicH.isFilled.imageThumbnail(field)) {
|
|
45
|
+
const src = buildURL(field.url, imgixParams);
|
|
46
|
+
return /* @__PURE__ */ jsx(Image, {
|
|
47
|
+
src,
|
|
48
|
+
width: layout === "fill" ? void 0 : field.dimensions.width,
|
|
49
|
+
height: layout === "fill" ? void 0 : field.dimensions.height,
|
|
50
|
+
alt: alt ?? (field.alt || fallbackAlt),
|
|
51
|
+
loader: imgixLoader,
|
|
52
|
+
layout,
|
|
53
|
+
...restProps
|
|
54
|
+
});
|
|
55
|
+
} else {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
export { PrismicNextImage };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { jsxs, Fragment, jsx } from 'react/jsx-runtime';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { PrismicToolbar } from '@prismicio/react';
|
|
4
|
+
import { useRouter } from 'next/router';
|
|
5
|
+
import { getPrismicPreviewCookie } from './lib/getPrismicPreviewCookie.mjs';
|
|
6
|
+
import { getPreviewCookieRepositoryName } from './lib/getPreviewCookieRepositoryName.mjs';
|
|
7
|
+
|
|
8
|
+
function PrismicPreview({
|
|
9
|
+
repositoryName,
|
|
10
|
+
children,
|
|
11
|
+
updatePreviewURL = "/api/preview",
|
|
12
|
+
exitPreviewURL = "/api/exit-preview"
|
|
13
|
+
}) {
|
|
14
|
+
const router = useRouter();
|
|
15
|
+
const resolvedUpdatePreviewURL = router.basePath + updatePreviewURL;
|
|
16
|
+
const resolvedExitPreviewURL = router.basePath + exitPreviewURL;
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
const startPreviewMode = async () => {
|
|
19
|
+
const res = await globalThis.fetch(resolvedUpdatePreviewURL);
|
|
20
|
+
if (res.ok) {
|
|
21
|
+
globalThis.location.reload();
|
|
22
|
+
} else {
|
|
23
|
+
console.error(
|
|
24
|
+
`[<PrismicPreview>] Failed to start or update Preview Mode using the "${resolvedUpdatePreviewURL}" API endpoint. Does it exist?`
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const handlePrismicPreviewUpdate = async (event) => {
|
|
29
|
+
event.preventDefault();
|
|
30
|
+
await startPreviewMode();
|
|
31
|
+
};
|
|
32
|
+
const handlePrismicPreviewEnd = async (event) => {
|
|
33
|
+
event.preventDefault();
|
|
34
|
+
const res = await globalThis.fetch(resolvedExitPreviewURL);
|
|
35
|
+
if (res.ok) {
|
|
36
|
+
globalThis.location.reload();
|
|
37
|
+
} else {
|
|
38
|
+
console.error(
|
|
39
|
+
`[<PrismicPreview>] Failed to exit Preview Mode using the "${resolvedExitPreviewURL}" API endpoint. Does it exist?`
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
if (router.isPreview) {
|
|
44
|
+
window.addEventListener(
|
|
45
|
+
"prismicPreviewUpdate",
|
|
46
|
+
handlePrismicPreviewUpdate
|
|
47
|
+
);
|
|
48
|
+
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
49
|
+
} else {
|
|
50
|
+
const prismicPreviewCookie = getPrismicPreviewCookie(
|
|
51
|
+
globalThis.document.cookie
|
|
52
|
+
);
|
|
53
|
+
if (prismicPreviewCookie) {
|
|
54
|
+
const locationIsDescendantOfBasePath = window.location.href.startsWith(
|
|
55
|
+
window.location.origin + router.basePath
|
|
56
|
+
);
|
|
57
|
+
const prismicPreviewCookieRepositoryName = getPreviewCookieRepositoryName(prismicPreviewCookie);
|
|
58
|
+
if (locationIsDescendantOfBasePath && prismicPreviewCookieRepositoryName === repositoryName) {
|
|
59
|
+
startPreviewMode();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return () => {
|
|
64
|
+
window.removeEventListener(
|
|
65
|
+
"prismicPreviewUpdate",
|
|
66
|
+
handlePrismicPreviewUpdate
|
|
67
|
+
);
|
|
68
|
+
window.removeEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
69
|
+
};
|
|
70
|
+
}, [
|
|
71
|
+
repositoryName,
|
|
72
|
+
resolvedExitPreviewURL,
|
|
73
|
+
resolvedUpdatePreviewURL,
|
|
74
|
+
router.isPreview,
|
|
75
|
+
router.basePath
|
|
76
|
+
]);
|
|
77
|
+
return /* @__PURE__ */ jsxs(Fragment, {
|
|
78
|
+
children: [
|
|
79
|
+
children,
|
|
80
|
+
/* @__PURE__ */ jsx(PrismicToolbar, {
|
|
81
|
+
repositoryName
|
|
82
|
+
})
|
|
83
|
+
]
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export { PrismicPreview };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const isPrismicNextPreviewData = (previewData) => {
|
|
2
|
+
return typeof previewData === "object" && "ref" in previewData;
|
|
3
|
+
};
|
|
4
|
+
const enableAutoPreviews = (config) => {
|
|
5
|
+
if ("previewData" in config && config.previewData) {
|
|
6
|
+
const { previewData } = config;
|
|
7
|
+
if (isPrismicNextPreviewData(previewData) && previewData.ref) {
|
|
8
|
+
config.client.queryContentFromRef(previewData.ref);
|
|
9
|
+
}
|
|
10
|
+
} else if ("req" in config && config.req) {
|
|
11
|
+
config.client.enableAutoPreviewsFromReq(config.req);
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { enableAutoPreviews };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { NextApiRequest, NextApiResponse, PreviewData } from 'next';
|
|
2
|
-
import
|
|
3
|
-
import
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { Client, HttpRequestLike, ClientConfig } from '@prismicio/client';
|
|
4
4
|
import { LinkResolverFunction } from '@prismicio/helpers';
|
|
5
|
+
import { ImageProps } from 'next/image';
|
|
6
|
+
import { ImgixURLParams } from 'imgix-url-builder';
|
|
7
|
+
import * as prismicT from '@prismicio/types';
|
|
5
8
|
|
|
6
9
|
/**
|
|
7
10
|
* Configuration for `setPreviewData`.
|
|
@@ -32,42 +35,6 @@ declare type SetPreviewDataConfig = {
|
|
|
32
35
|
*/
|
|
33
36
|
declare function setPreviewData({ req, res }: SetPreviewDataConfig): void;
|
|
34
37
|
|
|
35
|
-
/**
|
|
36
|
-
* Configuration for `enableAutoPreviews`.
|
|
37
|
-
*
|
|
38
|
-
* @typeParam TPreviewData - Next.js preview data object.
|
|
39
|
-
*/
|
|
40
|
-
declare type EnableAutoPreviewsConfig<TPreviewData extends PreviewData = PreviewData> = {
|
|
41
|
-
/**
|
|
42
|
-
* Prismic client with which automatic previews will be enabled.
|
|
43
|
-
*/
|
|
44
|
-
client: Client;
|
|
45
|
-
} & ({
|
|
46
|
-
/**
|
|
47
|
-
* A Next.js context object (such as the context object from
|
|
48
|
-
* `getStaticProps` or `getServerSideProps`).
|
|
49
|
-
*
|
|
50
|
-
* Pass a `context` object when using `enableAutoPreviews` outside a
|
|
51
|
-
* Next.js API endpoint.
|
|
52
|
-
*/
|
|
53
|
-
previewData?: TPreviewData;
|
|
54
|
-
} | {
|
|
55
|
-
/**
|
|
56
|
-
* A Next.js API endpoint request object.
|
|
57
|
-
*
|
|
58
|
-
* Pass a `req` object when using `enableAutoPreviews` in a Next.js API endpoint.
|
|
59
|
-
*/
|
|
60
|
-
req?: HttpRequestLike;
|
|
61
|
-
});
|
|
62
|
-
/**
|
|
63
|
-
* Configures a Prismic client to automatically query draft content during a
|
|
64
|
-
* preview session. It either takes in a Next.js `getStaticProps` context object
|
|
65
|
-
* or a Next.js API endpoint request object.
|
|
66
|
-
*
|
|
67
|
-
* @param config - Configuration for the function.
|
|
68
|
-
*/
|
|
69
|
-
declare const enableAutoPreviews: <TPreviewData extends PreviewData>(config: EnableAutoPreviewsConfig<TPreviewData>) => void;
|
|
70
|
-
|
|
71
38
|
/**
|
|
72
39
|
* Configuration for `exitPreview`.
|
|
73
40
|
*/
|
|
@@ -79,9 +46,7 @@ declare type ExitPreviewConfig = {
|
|
|
79
46
|
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
80
47
|
*/
|
|
81
48
|
req: {
|
|
82
|
-
headers:
|
|
83
|
-
referer?: NextApiRequest["headers"]["referer"];
|
|
84
|
-
};
|
|
49
|
+
headers: NextApiRequest["headers"];
|
|
85
50
|
};
|
|
86
51
|
/**
|
|
87
52
|
* The `res` object from a Next.js API route. This is given as a parameter to
|
|
@@ -91,14 +56,16 @@ declare type ExitPreviewConfig = {
|
|
|
91
56
|
*/
|
|
92
57
|
res: {
|
|
93
58
|
clearPreviewData: NextApiResponse["clearPreviewData"];
|
|
94
|
-
|
|
59
|
+
status: NextApiResponse["status"];
|
|
60
|
+
json: NextApiResponse["json"];
|
|
95
61
|
};
|
|
62
|
+
/**
|
|
63
|
+
* @deprecated - This property is no longer used. It can be deleted safely.
|
|
64
|
+
*/
|
|
65
|
+
exitPreviewURL?: string;
|
|
96
66
|
};
|
|
97
67
|
/**
|
|
98
68
|
* Exits Next.js's Preview Mode from within a Next.js API route.
|
|
99
|
-
*
|
|
100
|
-
* If the user was sent to the endpoint from a page, the user will be redirected
|
|
101
|
-
* back to that page after exiting Preview Mode.
|
|
102
69
|
*/
|
|
103
70
|
declare function exitPreview(config: ExitPreviewConfig): void;
|
|
104
71
|
|
|
@@ -114,13 +81,23 @@ declare type PrismicPreviewProps = {
|
|
|
114
81
|
/**
|
|
115
82
|
* The URL of your app's Prismic preview endpoint (default: `/api/preview`).
|
|
116
83
|
* This URL will be fetched on preview update events.
|
|
84
|
+
*
|
|
85
|
+
* **Note**: If your `next.config.js` file contains a `basePath`, it is
|
|
86
|
+
* automatically included.
|
|
117
87
|
*/
|
|
118
88
|
updatePreviewURL?: string;
|
|
119
89
|
/**
|
|
120
90
|
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`).
|
|
121
91
|
* This URL will be fetched on preview exit events.
|
|
92
|
+
*
|
|
93
|
+
* **Note**: If your `next.config.js` file contains a `basePath`, it is
|
|
94
|
+
* automatically included.
|
|
122
95
|
*/
|
|
123
96
|
exitPreviewURL?: string;
|
|
97
|
+
/**
|
|
98
|
+
* Children to render adjacent to the Prismic Toolbar. The Prismic Toolbar
|
|
99
|
+
* will be rendered last.
|
|
100
|
+
*/
|
|
124
101
|
children?: React.ReactNode;
|
|
125
102
|
};
|
|
126
103
|
/**
|
|
@@ -135,34 +112,46 @@ declare type PrismicPreviewProps = {
|
|
|
135
112
|
declare function PrismicPreview({ repositoryName, children, updatePreviewURL, exitPreviewURL, }: PrismicPreviewProps): JSX.Element;
|
|
136
113
|
|
|
137
114
|
/**
|
|
138
|
-
*
|
|
139
|
-
*
|
|
140
|
-
|
|
141
|
-
declare function redirectToPreviewURL<TLinkResolverFunction extends LinkResolverFunction<any>>({ req, res, client, linkResolver, defaultURL, }: PreviewConfig<TLinkResolverFunction>): Promise<void>;
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Configuration for creating a Prismic client with automatic preview support in
|
|
145
|
-
* Next.js apps.
|
|
115
|
+
* Configuration for `enableAutoPreviews`.
|
|
116
|
+
*
|
|
117
|
+
* @typeParam TPreviewData - Next.js preview data object.
|
|
146
118
|
*/
|
|
147
|
-
declare type
|
|
119
|
+
declare type EnableAutoPreviewsConfig<TPreviewData extends PreviewData = PreviewData> = {
|
|
148
120
|
/**
|
|
149
|
-
*
|
|
150
|
-
|
|
121
|
+
* Prismic client with which automatic previews will be enabled.
|
|
122
|
+
*/
|
|
123
|
+
client: Client;
|
|
124
|
+
} & ({
|
|
125
|
+
/**
|
|
126
|
+
* A Next.js context object (such as the context object from
|
|
127
|
+
* `getStaticProps` or `getServerSideProps`).
|
|
151
128
|
*
|
|
152
|
-
* Pass `
|
|
129
|
+
* Pass a `context` object when using `enableAutoPreviews` outside a
|
|
130
|
+
* Next.js API endpoint.
|
|
153
131
|
*/
|
|
154
|
-
previewData?:
|
|
132
|
+
previewData?: TPreviewData;
|
|
133
|
+
} | {
|
|
155
134
|
/**
|
|
156
135
|
* A Next.js API endpoint request object.
|
|
157
136
|
*
|
|
158
|
-
* Pass a `req` object when using in a Next.js API
|
|
137
|
+
* Pass a `req` object when using `enableAutoPreviews` in a Next.js API
|
|
138
|
+
* endpoint.
|
|
159
139
|
*/
|
|
160
|
-
req?:
|
|
161
|
-
};
|
|
140
|
+
req?: HttpRequestLike;
|
|
141
|
+
});
|
|
142
|
+
/**
|
|
143
|
+
* Configures a Prismic client to automatically query draft content during a
|
|
144
|
+
* preview session. It either takes in a Next.js `getStaticProps` context object
|
|
145
|
+
* or a Next.js API endpoint request object.
|
|
146
|
+
*
|
|
147
|
+
* @param config - Configuration for the function.
|
|
148
|
+
*/
|
|
149
|
+
declare const enableAutoPreviews: <TPreviewData extends PreviewData>(config: EnableAutoPreviewsConfig<TPreviewData>) => void;
|
|
150
|
+
|
|
162
151
|
/**
|
|
163
152
|
* Preview config for enabling previews with redirectToPreviewURL
|
|
164
153
|
*/
|
|
165
|
-
declare type
|
|
154
|
+
declare type RedirectToPreviewURLConfig<TLinkResolverFunction extends LinkResolverFunction<any> = LinkResolverFunction> = {
|
|
166
155
|
/**
|
|
167
156
|
* The `req` object from a Next.js API route. This is given as a parameter to
|
|
168
157
|
* the API route.
|
|
@@ -192,9 +181,93 @@ declare type PreviewConfig<TLinkResolverFunction extends LinkResolverFunction<an
|
|
|
192
181
|
*/
|
|
193
182
|
linkResolver?: TLinkResolverFunction;
|
|
194
183
|
/**
|
|
195
|
-
* The default redirect URL if a URL cannot be determined for the previewed
|
|
184
|
+
* The default redirect URL if a URL cannot be determined for the previewed
|
|
185
|
+
* document.
|
|
186
|
+
*
|
|
187
|
+
* **Note**: If you `next.config.js` file contains a `basePath`, the
|
|
188
|
+
* `defaultURL` option must _not_ include it. Instead, provide the `basePath`
|
|
189
|
+
* property using the `basePath` option.
|
|
196
190
|
*/
|
|
197
191
|
defaultURL?: string;
|
|
192
|
+
/**
|
|
193
|
+
* The `basePath` for the Next.js app as it is defined in `next.config.js`.
|
|
194
|
+
* This option can be omitted if the app does not have a `basePath`.
|
|
195
|
+
*
|
|
196
|
+
* @remarks
|
|
197
|
+
* The API route is unable to detect the app's `basePath` automatically. It
|
|
198
|
+
* must be provided to `redirectToPreviewURL()` manually.
|
|
199
|
+
*/
|
|
200
|
+
basePath?: string;
|
|
198
201
|
};
|
|
202
|
+
/**
|
|
203
|
+
* Redirects a user to the URL of a previewed Prismic document from within a
|
|
204
|
+
* Next.js API route.
|
|
205
|
+
*/
|
|
206
|
+
declare function redirectToPreviewURL<TLinkResolverFunction extends LinkResolverFunction<any>>(config: RedirectToPreviewURLConfig<TLinkResolverFunction>): Promise<void>;
|
|
207
|
+
|
|
208
|
+
declare type PrismicNextImageProps = Omit<ImageProps, "src" | "alt" | "width" | "height"> & {
|
|
209
|
+
/**
|
|
210
|
+
* The Prismic Image field or thumbnail to render.
|
|
211
|
+
*/
|
|
212
|
+
field: prismicT.ImageFieldImage | null | undefined;
|
|
213
|
+
/**
|
|
214
|
+
* An object of Imgix URL API parameters to transform the image.
|
|
215
|
+
*
|
|
216
|
+
* @see https://docs.imgix.com/apis/rendering
|
|
217
|
+
*/
|
|
218
|
+
imgixParams?: ImgixURLParams;
|
|
219
|
+
/**
|
|
220
|
+
* Declare an image as decorative by providing `alt=""`.
|
|
221
|
+
*
|
|
222
|
+
* See:
|
|
223
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
224
|
+
*/
|
|
225
|
+
alt?: "";
|
|
226
|
+
/**
|
|
227
|
+
* Declare an image as decorative only if the Image field does not have
|
|
228
|
+
* alternative text by providing `fallbackAlt=""`.
|
|
229
|
+
*
|
|
230
|
+
* See:
|
|
231
|
+
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/alt#decorative_images
|
|
232
|
+
*/
|
|
233
|
+
fallbackAlt?: "";
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* React component that renders an image from a Prismic Image field or one of
|
|
237
|
+
* its thumbnails using `next/image`. It will automatically set the `alt`
|
|
238
|
+
* attribute using the Image field's `alt` property.
|
|
239
|
+
*
|
|
240
|
+
* It uses an Imgix URL-based loader by default. A custom loader can be provided
|
|
241
|
+
* with the `loader` prop. If you would like to use the Next.js Image
|
|
242
|
+
* Optimization API instead, set `loader={undefined}`.
|
|
243
|
+
*
|
|
244
|
+
* @param props - Props for the component.
|
|
245
|
+
*
|
|
246
|
+
* @returns A responsive image component using `next/image` for the given Image
|
|
247
|
+
* field.
|
|
248
|
+
*
|
|
249
|
+
* @see To learn more about `next/image`, see: https://nextjs.org/docs/api-reference/next/image
|
|
250
|
+
*/
|
|
251
|
+
declare const PrismicNextImage: ({ field, imgixParams, alt, fallbackAlt, layout, ...restProps }: PrismicNextImageProps) => JSX.Element | null;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Configuration for creating a Prismic client with automatic preview support in
|
|
255
|
+
* Next.js apps.
|
|
256
|
+
*/
|
|
257
|
+
declare type CreateClientConfig = {
|
|
258
|
+
/**
|
|
259
|
+
* Preview data coming from Next.js context object. This context object comes
|
|
260
|
+
* from `getStaticProps` or `getServerSideProps`.
|
|
261
|
+
*
|
|
262
|
+
* Pass `previewData` when using outside a Next.js API endpoint.
|
|
263
|
+
*/
|
|
264
|
+
previewData?: PreviewData;
|
|
265
|
+
/**
|
|
266
|
+
* A Next.js API endpoint request object.
|
|
267
|
+
*
|
|
268
|
+
* Pass a `req` object when using in a Next.js API endpoint.
|
|
269
|
+
*/
|
|
270
|
+
req?: NextApiRequest;
|
|
271
|
+
} & ClientConfig;
|
|
199
272
|
|
|
200
|
-
export { CreateClientConfig, EnableAutoPreviewsConfig, ExitPreviewConfig
|
|
273
|
+
export { CreateClientConfig, EnableAutoPreviewsConfig, ExitPreviewConfig, PrismicNextImage, PrismicNextImageProps, PrismicPreview, PrismicPreviewProps, RedirectToPreviewURLConfig, SetPreviewDataConfig, enableAutoPreviews, exitPreview, redirectToPreviewURL, setPreviewData };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { setPreviewData } from './setPreviewData.mjs';
|
|
2
|
+
export { exitPreview } from './exitPreview.mjs';
|
|
3
|
+
export { PrismicPreview } from './PrismicPreview.mjs';
|
|
4
|
+
export { enableAutoPreviews } from './enableAutoPreviews.mjs';
|
|
5
|
+
export { redirectToPreviewURL } from './redirectToPreviewURL.mjs';
|
|
6
|
+
export { PrismicNextImage } from './PrismicNextImage.mjs';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import * as prismic from '@prismicio/client';
|
|
2
|
+
|
|
3
|
+
const readValue = (value) => {
|
|
4
|
+
return value.replace(/%3B/g, ";");
|
|
5
|
+
};
|
|
6
|
+
const getPrismicPreviewCookie = (cookieJar) => {
|
|
7
|
+
const cookies = cookieJar.split("; ");
|
|
8
|
+
let value;
|
|
9
|
+
for (const cookie of cookies) {
|
|
10
|
+
const parts = cookie.split("=");
|
|
11
|
+
const name = readValue(parts[0]).replace(/%3D/g, "=");
|
|
12
|
+
if (name === prismic.cookie.preview) {
|
|
13
|
+
value = readValue(parts.slice(1).join("="));
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return value;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { getPrismicPreviewCookie };
|
package/dist/package.mjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const isPrismicNextQuery = (query) => {
|
|
2
|
+
return typeof query.documentId === "string" && typeof query.token === "string";
|
|
3
|
+
};
|
|
4
|
+
async function redirectToPreviewURL(config) {
|
|
5
|
+
const defaultURL = config.defaultURL || "/";
|
|
6
|
+
const basePath = config.basePath || "";
|
|
7
|
+
if (isPrismicNextQuery(config.req.query)) {
|
|
8
|
+
const previewUrl = await config.client.resolvePreviewURL({
|
|
9
|
+
linkResolver: config.linkResolver,
|
|
10
|
+
defaultURL,
|
|
11
|
+
documentID: config.req.query.documentId,
|
|
12
|
+
previewToken: config.req.query.token
|
|
13
|
+
});
|
|
14
|
+
config.res.redirect(basePath + previewUrl);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
config.res.redirect(basePath + defaultURL);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { redirectToPreviewURL };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/next",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "Helpers to integrate Prismic into Next.js apps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -15,23 +15,19 @@
|
|
|
15
15
|
},
|
|
16
16
|
"license": "Apache-2.0",
|
|
17
17
|
"author": "Prismic <contact@prismic.io> (https://prismic.io)",
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"type": "module",
|
|
18
20
|
"exports": {
|
|
19
|
-
".":
|
|
20
|
-
"require": "./dist/index.cjs",
|
|
21
|
-
"import": "./dist/index.js"
|
|
22
|
-
},
|
|
23
|
-
"./package.json": "./package.json"
|
|
21
|
+
".": "./dist/index.mjs"
|
|
24
22
|
},
|
|
25
|
-
"main": "./dist/index.cjs",
|
|
26
|
-
"module": "dist/index.js",
|
|
27
23
|
"types": "dist/index.d.ts",
|
|
28
24
|
"files": [
|
|
29
25
|
"dist",
|
|
30
26
|
"src"
|
|
31
27
|
],
|
|
32
28
|
"scripts": {
|
|
33
|
-
"build": "
|
|
34
|
-
"dev": "
|
|
29
|
+
"build": "unbuild",
|
|
30
|
+
"dev": "chokidar src --initial --command unbuild",
|
|
35
31
|
"format": "prettier --write .",
|
|
36
32
|
"lint": "eslint --ext .js,.ts .",
|
|
37
33
|
"prepare": "npm run build",
|
|
@@ -41,44 +37,42 @@
|
|
|
41
37
|
"release:dry": "standard-version --dry-run",
|
|
42
38
|
"size": "size-limit",
|
|
43
39
|
"test": "npm run lint && npm run unit && npm run build && npm run size",
|
|
44
|
-
"unit": "
|
|
40
|
+
"unit": "vitest run --coverage",
|
|
41
|
+
"unit:watch": "vitest watch"
|
|
45
42
|
},
|
|
46
43
|
"dependencies": {
|
|
47
|
-
"@prismicio/
|
|
44
|
+
"@prismicio/client": "^6.7.1",
|
|
45
|
+
"@prismicio/helpers": "^2.3.3",
|
|
46
|
+
"@prismicio/react": "^2.5.0",
|
|
47
|
+
"@prismicio/types": "^0.2.3",
|
|
48
|
+
"eslint-plugin-react": "^7.31.7",
|
|
49
|
+
"eslint-plugin-react-hooks": "^4.6.0",
|
|
50
|
+
"imgix-url-builder": "^0.0.3"
|
|
48
51
|
},
|
|
49
52
|
"devDependencies": {
|
|
50
|
-
"@prismicio/
|
|
51
|
-
"@
|
|
52
|
-
"@
|
|
53
|
-
"@
|
|
54
|
-
"@
|
|
55
|
-
"@
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"@types/sinon": "^10.0.11",
|
|
59
|
-
"@typescript-eslint/eslint-plugin": "^5.17.0",
|
|
60
|
-
"@typescript-eslint/parser": "^5.17.0",
|
|
61
|
-
"ava": "^4.1.0",
|
|
62
|
-
"eslint": "^8.12.0",
|
|
53
|
+
"@prismicio/mock": "^0.1.1",
|
|
54
|
+
"@size-limit/preset-small-lib": "^8.1.0",
|
|
55
|
+
"@types/react-test-renderer": "^18.0.0",
|
|
56
|
+
"@typescript-eslint/eslint-plugin": "^5.36.2",
|
|
57
|
+
"@typescript-eslint/parser": "^5.36.2",
|
|
58
|
+
"@vitest/coverage-c8": "^0.23.1",
|
|
59
|
+
"chokidar-cli": "^3.0.0",
|
|
60
|
+
"eslint": "^8.23.0",
|
|
63
61
|
"eslint-config-prettier": "^8.5.0",
|
|
64
|
-
"eslint-plugin-prettier": "^4.
|
|
65
|
-
"eslint-plugin-tsdoc": "^0.2.
|
|
66
|
-
"
|
|
67
|
-
"jsdom-global": "^3.0.2",
|
|
68
|
-
"msw": "^0.39.2",
|
|
62
|
+
"eslint-plugin-prettier": "^4.2.1",
|
|
63
|
+
"eslint-plugin-tsdoc": "^0.2.16",
|
|
64
|
+
"happy-dom": "^6.0.4",
|
|
69
65
|
"next": "^12.1.4",
|
|
70
|
-
"node-fetch": "^3.2.3",
|
|
71
66
|
"nyc": "^15.1.0",
|
|
72
|
-
"prettier": "^2.
|
|
73
|
-
"prettier-plugin-jsdoc": "^0.
|
|
74
|
-
"react": "^18.
|
|
75
|
-
"react-
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"typescript": "^4.6.3"
|
|
67
|
+
"prettier": "^2.7.1",
|
|
68
|
+
"prettier-plugin-jsdoc": "^0.4.2",
|
|
69
|
+
"react": "^18.1.0",
|
|
70
|
+
"react-test-renderer": "^18.2.0",
|
|
71
|
+
"size-limit": "^8.1.0",
|
|
72
|
+
"standard-version": "^9.5.0",
|
|
73
|
+
"typescript": "^4.8.3",
|
|
74
|
+
"unbuild": "^0.8.10",
|
|
75
|
+
"vitest": "^0.23.1"
|
|
82
76
|
},
|
|
83
77
|
"peerDependencies": {
|
|
84
78
|
"@prismicio/client": "^6.0.0",
|