@prismicio/next 1.7.0 → 2.0.0-alpha.0
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 +5 -3
- package/dist/PrismicNextImage.d.ts +7 -8
- package/dist/PrismicNextImage.js +8 -8
- package/dist/PrismicNextImage.js.map +1 -1
- package/dist/PrismicNextLink.d.ts +99 -100
- package/dist/PrismicNextLink.js +3 -5
- package/dist/PrismicNextLink.js.map +1 -1
- package/dist/PrismicPreview.d.ts +5 -13
- package/dist/PrismicPreview.js +6 -9
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/PrismicPreviewClient.d.ts +5 -3
- package/dist/PrismicPreviewClient.js +58 -69
- package/dist/PrismicPreviewClient.js.map +1 -1
- package/dist/createLocaleRedirect.d.ts +8 -15
- package/dist/createLocaleRedirect.js +4 -28
- package/dist/createLocaleRedirect.js.map +1 -1
- package/dist/enableAutoPreviews.d.ts +7 -31
- package/dist/enableAutoPreviews.js +24 -35
- package/dist/enableAutoPreviews.js.map +1 -1
- package/dist/exitPreview.d.ts +5 -47
- package/dist/exitPreview.js +8 -15
- package/dist/exitPreview.js.map +1 -1
- package/dist/imgixLoader.js.map +1 -1
- package/dist/index.d.ts +15 -18
- package/dist/index.js +2 -4
- package/dist/index.js.map +1 -1
- package/dist/lib/devMsg.d.ts +1 -1
- package/dist/lib/devMsg.js.map +1 -1
- package/dist/package.json.js +1 -1
- package/dist/pages/PrismicPreview.d.ts +31 -0
- package/dist/pages/PrismicPreview.js +64 -0
- package/dist/pages/PrismicPreview.js.map +1 -0
- package/dist/pages/enableAutoPreviews.d.ts +31 -0
- package/dist/pages/enableAutoPreviews.js +19 -0
- package/dist/pages/enableAutoPreviews.js.map +1 -0
- package/dist/pages/exitPreview.d.ts +33 -0
- package/dist/pages/exitPreview.js +10 -0
- package/dist/pages/exitPreview.js.map +1 -0
- package/dist/pages/index.d.ts +18 -0
- package/dist/pages/redirectToPreviewURL.d.ts +43 -0
- package/dist/pages/redirectToPreviewURL.js +16 -0
- package/dist/pages/redirectToPreviewURL.js.map +1 -0
- package/dist/{setPreviewData.d.ts → pages/setPreviewData.d.ts} +3 -9
- package/dist/{setPreviewData.js → pages/setPreviewData.js} +2 -2
- package/dist/pages/setPreviewData.js.map +1 -0
- package/dist/pages/types.d.ts +30 -0
- package/dist/pages.js +21 -0
- package/dist/pages.js.map +1 -0
- package/dist/redirectToPreviewURL.d.ts +11 -41
- package/dist/redirectToPreviewURL.js +16 -22
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/dist/types.d.ts +2 -51
- package/package.json +61 -54
- package/src/PrismicNextImage.tsx +19 -16
- package/src/PrismicNextLink.tsx +17 -13
- package/src/PrismicPreview.tsx +18 -30
- package/src/PrismicPreviewClient.tsx +111 -138
- package/src/createLocaleRedirect.ts +14 -56
- package/src/enableAutoPreviews.ts +38 -110
- package/src/exitPreview.ts +18 -79
- package/src/index.ts +15 -26
- package/src/lib/devMsg.ts +1 -1
- package/src/pages/PrismicPreview.tsx +145 -0
- package/src/pages/enableAutoPreviews.ts +62 -0
- package/src/pages/exitPreview.ts +45 -0
- package/src/pages/index.ts +27 -0
- package/src/pages/redirectToPreviewURL.ts +77 -0
- package/src/{setPreviewData.ts → pages/setPreviewData.ts} +5 -11
- package/src/pages/types.ts +36 -0
- package/src/redirectToPreviewURL.ts +44 -95
- package/src/types.ts +2 -56
- package/dist/PrismicNextImage.cjs +0 -64
- package/dist/PrismicNextImage.cjs.map +0 -1
- package/dist/PrismicNextLink.cjs +0 -40
- package/dist/PrismicNextLink.cjs.map +0 -1
- package/dist/PrismicPreview.cjs +0 -18
- package/dist/PrismicPreview.cjs.map +0 -1
- package/dist/PrismicPreviewClient.cjs +0 -81
- package/dist/PrismicPreviewClient.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/cookie.cjs +0 -5
- package/dist/_node_modules/@prismicio/client/dist/cookie.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/cookie.js +0 -5
- package/dist/_node_modules/@prismicio/client/dist/cookie.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.cjs +0 -16
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.js +0 -16
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.cjs +0 -13
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.js +0 -13
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs +0 -47
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js +0 -47
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.cjs +0 -28
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.js +0 -28
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.cjs +0 -27
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.js +0 -27
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.cjs +0 -14
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.js +0 -14
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.cjs +0 -7
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.js +0 -7
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.cjs +0 -9
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.js +0 -9
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.js.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.cjs +0 -10
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.cjs.map +0 -1
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.js +0 -10
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.js.map +0 -1
- package/dist/createLocaleRedirect.cjs +0 -32
- package/dist/createLocaleRedirect.cjs.map +0 -1
- package/dist/enableAutoPreviews.cjs +0 -40
- package/dist/enableAutoPreviews.cjs.map +0 -1
- package/dist/exitPreview.cjs +0 -20
- package/dist/exitPreview.cjs.map +0 -1
- package/dist/imgixLoader.cjs +0 -17
- package/dist/imgixLoader.cjs.map +0 -1
- package/dist/index.cjs +0 -21
- package/dist/index.cjs.map +0 -1
- package/dist/lib/devMsg.cjs +0 -8
- package/dist/lib/devMsg.cjs.map +0 -1
- package/dist/lib/getPreviewCookieRepositoryName.cjs +0 -7
- package/dist/lib/getPreviewCookieRepositoryName.cjs.map +0 -1
- package/dist/lib/getPreviewCookieRepositoryName.d.ts +0 -9
- package/dist/lib/getPreviewCookieRepositoryName.js +0 -7
- package/dist/lib/getPreviewCookieRepositoryName.js.map +0 -1
- package/dist/lib/getPrismicPreviewCookie.cjs +0 -21
- package/dist/lib/getPrismicPreviewCookie.cjs.map +0 -1
- package/dist/lib/getPrismicPreviewCookie.d.ts +0 -9
- package/dist/lib/getPrismicPreviewCookie.js +0 -21
- package/dist/lib/getPrismicPreviewCookie.js.map +0 -1
- package/dist/package.json.cjs +0 -5
- package/dist/package.json.cjs.map +0 -1
- package/dist/redirectToPreviewURL.cjs +0 -30
- package/dist/redirectToPreviewURL.cjs.map +0 -1
- package/dist/setPreviewData.cjs +0 -11
- package/dist/setPreviewData.cjs.map +0 -1
- package/dist/setPreviewData.js.map +0 -1
- package/src/lib/getPreviewCookieRepositoryName.ts +0 -14
- package/src/lib/getPrismicPreviewCookie.ts +0 -33
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export { PrismicNextLink } from "../PrismicNextLink.js";
|
|
2
|
+
export { type PrismicNextLinkProps } from "../PrismicNextLink.js";
|
|
3
|
+
|
|
4
|
+
export { PrismicNextImage } from "../PrismicNextImage.js";
|
|
5
|
+
export { type PrismicNextImageProps } from "../PrismicNextImage.js";
|
|
6
|
+
|
|
7
|
+
export { imgixLoader } from "../imgixLoader.js";
|
|
8
|
+
|
|
9
|
+
export { PrismicPreview } from "./PrismicPreview.js";
|
|
10
|
+
export type { PrismicPreviewProps } from "./PrismicPreview.js";
|
|
11
|
+
|
|
12
|
+
export { enableAutoPreviews } from "./enableAutoPreviews.js";
|
|
13
|
+
export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews.js";
|
|
14
|
+
|
|
15
|
+
export { redirectToPreviewURL } from "./redirectToPreviewURL.js";
|
|
16
|
+
export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL.js";
|
|
17
|
+
|
|
18
|
+
export { exitPreview } from "./exitPreview.js";
|
|
19
|
+
export type { ExitPreviewAPIRouteConfig } from "./exitPreview.js";
|
|
20
|
+
|
|
21
|
+
export { setPreviewData } from "./setPreviewData.js";
|
|
22
|
+
export type { SetPreviewDataConfig } from "./setPreviewData.js";
|
|
23
|
+
|
|
24
|
+
export type { CreateClientConfig } from "./types.js";
|
|
25
|
+
|
|
26
|
+
export { createLocaleRedirect } from "../createLocaleRedirect.js";
|
|
27
|
+
export type { CreateLocaleRedirectConfig } from "../createLocaleRedirect.js";
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { LinkResolverFunction, Client } from "@prismicio/client";
|
|
2
|
+
|
|
3
|
+
import type { NextApiRequestLike, NextApiResponseLike } from "./types.js";
|
|
4
|
+
|
|
5
|
+
export type RedirectToPreviewURLConfig = {
|
|
6
|
+
/** The Prismic client configured for the preview session's repository. */
|
|
7
|
+
// `Pick` is used to use the smallest possible subset of
|
|
8
|
+
// `prismic.Client`. Doing this reduces the surface area for breaking
|
|
9
|
+
// type changes.
|
|
10
|
+
client: Pick<Client, "resolvePreviewURL">;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The `req` object from a Next.js API route.
|
|
14
|
+
*
|
|
15
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\>
|
|
16
|
+
*/
|
|
17
|
+
req: NextApiRequestLike;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The `res` object from a Next.js API route.
|
|
21
|
+
*
|
|
22
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\>
|
|
23
|
+
*/
|
|
24
|
+
res: NextApiResponseLike;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A Link Resolver used to resolve the previewed document's URL.
|
|
28
|
+
*
|
|
29
|
+
* @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
|
|
30
|
+
*/
|
|
31
|
+
linkResolver?: LinkResolverFunction;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The default redirect URL if a URL cannot be determined for the previewed
|
|
35
|
+
* document.
|
|
36
|
+
*
|
|
37
|
+
* **Note**: If you `next.config.js` file contains a `basePath`, the
|
|
38
|
+
* `defaultURL` option must _not_ include it. Instead, provide the `basePath`
|
|
39
|
+
* property using the `basePath` option.
|
|
40
|
+
*/
|
|
41
|
+
defaultURL?: string;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The `basePath` for the Next.js app as it is defined in `next.config.js`.
|
|
45
|
+
* This option can be omitted if the app does not have a `basePath`.
|
|
46
|
+
*
|
|
47
|
+
* @remarks
|
|
48
|
+
* The Router Handler or API route is unable to detect the app's `basePath`
|
|
49
|
+
* automatically. It must be provided to `redirectToPreviewURL()` manually.
|
|
50
|
+
*/
|
|
51
|
+
basePath?: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export async function redirectToPreviewURL(
|
|
55
|
+
config: RedirectToPreviewURLConfig,
|
|
56
|
+
): Promise<void> {
|
|
57
|
+
const {
|
|
58
|
+
client,
|
|
59
|
+
req,
|
|
60
|
+
res,
|
|
61
|
+
linkResolver,
|
|
62
|
+
defaultURL = "/",
|
|
63
|
+
basePath = "",
|
|
64
|
+
} = config;
|
|
65
|
+
|
|
66
|
+
const previewToken = req.query.token?.toString();
|
|
67
|
+
|
|
68
|
+
const previewURL = await client.resolvePreviewURL({
|
|
69
|
+
linkResolver,
|
|
70
|
+
defaultURL,
|
|
71
|
+
previewToken,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
res.redirect(basePath + previewURL);
|
|
75
|
+
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { cookie } from "@prismicio/client";
|
|
2
2
|
|
|
3
|
-
import { NextApiRequestLike, NextApiResponseLike } from "./types";
|
|
3
|
+
import type { NextApiRequestLike, NextApiResponseLike } from "./types.js";
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* Configuration for `setPreviewData`.
|
|
7
|
-
*/
|
|
5
|
+
/** Configuration for `setPreviewData`. */
|
|
8
6
|
export type SetPreviewDataConfig = {
|
|
9
7
|
/**
|
|
10
8
|
* The `req` object from a Next.js API route.
|
|
@@ -21,13 +19,9 @@ export type SetPreviewDataConfig = {
|
|
|
21
19
|
res: NextApiResponseLike;
|
|
22
20
|
};
|
|
23
21
|
|
|
24
|
-
/**
|
|
25
|
-
* **Only use this function in the Pages Directory (/pages).**
|
|
26
|
-
*
|
|
27
|
-
* Set Prismic preview data for Next.js's Preview Mode.
|
|
28
|
-
*/
|
|
22
|
+
/** Set Prismic preview data for Next.js's Preview Mode. */
|
|
29
23
|
export function setPreviewData({ req, res }: SetPreviewDataConfig): void {
|
|
30
|
-
const ref = req.query.token || req.cookies[
|
|
24
|
+
const ref = req.query.token || req.cookies[cookie.preview];
|
|
31
25
|
|
|
32
26
|
if (ref) {
|
|
33
27
|
res.setPreviewData({ ref });
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ClientConfig } from "@prismicio/client";
|
|
2
|
+
|
|
3
|
+
import { EnableAutoPreviewsConfig } from "./enableAutoPreviews.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for creating a Prismic client with automatic preview support in
|
|
7
|
+
* Next.js apps.
|
|
8
|
+
*/
|
|
9
|
+
export type CreateClientConfig = ClientConfig &
|
|
10
|
+
Pick<EnableAutoPreviewsConfig, "previewData" | "req">;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The minimal set of properties needed from `next`'s `NextApiRequest` type.
|
|
14
|
+
*
|
|
15
|
+
* This request type is only compatible with API routes defined in the `pages`
|
|
16
|
+
* directory.
|
|
17
|
+
*/
|
|
18
|
+
export type NextApiRequestLike = {
|
|
19
|
+
query: Partial<Record<string, string | string[]>>;
|
|
20
|
+
cookies: Partial<Record<string, string>>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The minimal set of properties needed from `next`'s `NextApiResponse` type.
|
|
25
|
+
*
|
|
26
|
+
* This request type is only compatible with API routes defined in the `pages`
|
|
27
|
+
* directory.
|
|
28
|
+
*/
|
|
29
|
+
export type NextApiResponseLike = {
|
|
30
|
+
redirect(url: string): NextApiResponseLike;
|
|
31
|
+
clearPreviewData(): NextApiResponseLike;
|
|
32
|
+
setHeader(name: string, value: string | string[]): NextApiResponseLike;
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
34
|
+
json(body: any): void;
|
|
35
|
+
setPreviewData(data: object | string): NextApiResponseLike;
|
|
36
|
+
};
|
|
@@ -1,31 +1,32 @@
|
|
|
1
1
|
import { redirect } from "next/navigation";
|
|
2
|
-
import { cookies, draftMode } from "next/headers";
|
|
3
|
-
import * as prismic from "@prismicio/client";
|
|
4
|
-
|
|
5
2
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
} from "
|
|
3
|
+
cookie as prismicCookie,
|
|
4
|
+
type Client,
|
|
5
|
+
type LinkResolverFunction,
|
|
6
|
+
} from "@prismicio/client";
|
|
10
7
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
import { NextRequestLike } from "./types.js";
|
|
9
|
+
|
|
10
|
+
export type RedirectToPreviewURLConfig = {
|
|
11
|
+
/** The Prismic client configured for the preview session's repository. */
|
|
15
12
|
// `Pick` is used to use the smallest possible subset of
|
|
16
13
|
// `prismic.Client`. Doing this reduces the surface area for breaking
|
|
17
14
|
// type changes.
|
|
18
|
-
client: Pick<
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
client: Pick<Client, "resolvePreviewURL">;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The `request` object from a Next.js Route Handler.
|
|
19
|
+
*
|
|
20
|
+
* @see Next.js Route Handler docs: \<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\>
|
|
21
|
+
*/
|
|
22
|
+
request: NextRequestLike;
|
|
22
23
|
|
|
23
24
|
/**
|
|
24
25
|
* A Link Resolver used to resolve the previewed document's URL.
|
|
25
26
|
*
|
|
26
27
|
* @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
|
|
27
28
|
*/
|
|
28
|
-
linkResolver?:
|
|
29
|
+
linkResolver?: LinkResolverFunction;
|
|
29
30
|
|
|
30
31
|
/**
|
|
31
32
|
* The default redirect URL if a URL cannot be determined for the previewed
|
|
@@ -36,91 +37,39 @@ type RedirectToPreviewURLConfigBase = {
|
|
|
36
37
|
* property using the `basePath` option.
|
|
37
38
|
*/
|
|
38
39
|
defaultURL?: string;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* The `basePath` for the Next.js app as it is defined in `next.config.js`.
|
|
42
|
-
* This option can be omitted if the app does not have a `basePath`.
|
|
43
|
-
*
|
|
44
|
-
* @remarks
|
|
45
|
-
* The Router Handler or API route is unable to detect the app's `basePath`
|
|
46
|
-
* automatically. It must be provided to `redirectToPreviewURL()` manually.
|
|
47
|
-
*/
|
|
48
|
-
basePath?: string;
|
|
49
40
|
};
|
|
50
41
|
|
|
51
|
-
export type RedirectToPreviewURLRouteHandlerConfig =
|
|
52
|
-
RedirectToPreviewURLConfigBase & {
|
|
53
|
-
/**
|
|
54
|
-
* The `request` object from a Next.js Route Handler.
|
|
55
|
-
*
|
|
56
|
-
* @see Next.js Route Handler docs: \<https://nextjs.org/docs/app/building-your-application/routing/route-handlers\>
|
|
57
|
-
*/
|
|
58
|
-
request: NextRequestLike;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export type RedirectToPreviewURLAPIEndpointConfig =
|
|
62
|
-
RedirectToPreviewURLConfigBase & {
|
|
63
|
-
/**
|
|
64
|
-
* The `req` object from a Next.js API route.
|
|
65
|
-
*
|
|
66
|
-
* @see Next.js API route docs: \<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\>
|
|
67
|
-
*/
|
|
68
|
-
req: NextApiRequestLike;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* The `res` object from a Next.js API route.
|
|
72
|
-
*
|
|
73
|
-
* @see Next.js API route docs: \<https://nextjs.org/docs/pages/building-your-application/routing/api-routes\>
|
|
74
|
-
*/
|
|
75
|
-
res: NextApiResponseLike;
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
export type RedirectToPreviewURLConfig =
|
|
79
|
-
| RedirectToPreviewURLRouteHandlerConfig
|
|
80
|
-
| RedirectToPreviewURLAPIEndpointConfig;
|
|
81
|
-
|
|
82
|
-
export async function redirectToPreviewURL(
|
|
83
|
-
config: RedirectToPreviewURLRouteHandlerConfig,
|
|
84
|
-
): Promise<never>;
|
|
85
|
-
export async function redirectToPreviewURL(
|
|
86
|
-
config: RedirectToPreviewURLAPIEndpointConfig,
|
|
87
|
-
): Promise<void>;
|
|
88
42
|
export async function redirectToPreviewURL(
|
|
89
43
|
config: RedirectToPreviewURLConfig,
|
|
90
|
-
): Promise<never
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
44
|
+
): Promise<never> {
|
|
45
|
+
const { client, request, linkResolver, defaultURL = "/" } = config;
|
|
46
|
+
|
|
47
|
+
// Need this to avoid the following Next.js build-time error:
|
|
48
|
+
// You're importing a component that needs next/headers. That only works
|
|
49
|
+
// in a Server Component which is not supported in the pages/ directory.
|
|
50
|
+
const { cookies, draftMode } = await import("next/headers");
|
|
51
|
+
|
|
52
|
+
const documentID =
|
|
53
|
+
request.nextUrl.searchParams.get("documentId") ?? undefined;
|
|
54
|
+
|
|
55
|
+
// Set the initial preview cookie. Setting the cookie here is necessary
|
|
56
|
+
// to support unpublished previews. Without setting it here, the page
|
|
57
|
+
// will try to render without the preview cookie, leading to a
|
|
58
|
+
// PrismicNotFound error.
|
|
59
|
+
const previewToken = request.nextUrl.searchParams.get("token") ?? undefined;
|
|
60
|
+
if (previewToken) {
|
|
61
|
+
const cookieJar = await cookies();
|
|
62
|
+
cookieJar.set(prismicCookie.preview, previewToken);
|
|
63
|
+
}
|
|
95
64
|
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
65
|
+
const previewURL = await client.resolvePreviewURL({
|
|
66
|
+
documentID,
|
|
67
|
+
previewToken,
|
|
68
|
+
defaultURL,
|
|
69
|
+
linkResolver,
|
|
99
70
|
});
|
|
100
71
|
|
|
101
|
-
|
|
102
|
-
draftMode().enable();
|
|
103
|
-
|
|
104
|
-
// Set the initial preview cookie, if available.
|
|
105
|
-
// Setting the cookie here is necessary to support unpublished
|
|
106
|
-
// previews. Without setting it here, the page will try to
|
|
107
|
-
// render without the preview cookie, leading to a
|
|
108
|
-
// PrismicNotFound error.
|
|
109
|
-
const previewCookie = request.nextUrl.searchParams.get("token");
|
|
110
|
-
if (previewCookie) {
|
|
111
|
-
cookies().set(prismic.cookie.preview, previewCookie);
|
|
112
|
-
}
|
|
72
|
+
(await draftMode()).enable();
|
|
113
73
|
|
|
114
|
-
|
|
115
|
-
} else {
|
|
116
|
-
if (!("res" in config)) {
|
|
117
|
-
throw new Error(
|
|
118
|
-
"[redirectToPreviewURL] The `res` object from the API route must be provided to `redirectToPreviewURL()`.",
|
|
119
|
-
);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
config.res.redirect(basePath + previewUrl);
|
|
123
|
-
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
74
|
+
redirect(previewURL);
|
|
126
75
|
}
|
package/src/types.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { PreviewData } from "next";
|
|
2
1
|
import type { ClientConfig } from "@prismicio/client";
|
|
3
2
|
|
|
4
3
|
// Add Next.js-specific fetchOptions to `@prismicio/client`.
|
|
@@ -8,35 +7,8 @@ declare module "@prismicio/client" {
|
|
|
8
7
|
}
|
|
9
8
|
}
|
|
10
9
|
|
|
11
|
-
/**
|
|
12
|
-
|
|
13
|
-
*/
|
|
14
|
-
export type PrismicPreviewData = {
|
|
15
|
-
ref: string;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Configuration for creating a Prismic client with automatic preview support in
|
|
20
|
-
* Next.js apps.
|
|
21
|
-
*/
|
|
22
|
-
export type CreateClientConfig = ClientConfig & {
|
|
23
|
-
/**
|
|
24
|
-
* **Only used in the Pages Directory (/pages).**
|
|
25
|
-
*
|
|
26
|
-
* The `previewData` object provided in the `getStaticProps()` or
|
|
27
|
-
* `getServerSideProps()` context object.
|
|
28
|
-
*/
|
|
29
|
-
previewData?: PreviewData;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* **Only used in the Pages Directory (/pages).**
|
|
33
|
-
*
|
|
34
|
-
* The `req` object from a Next.js API route.
|
|
35
|
-
*
|
|
36
|
-
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
37
|
-
*/
|
|
38
|
-
req?: NextApiRequestLike;
|
|
39
|
-
};
|
|
10
|
+
/** @deprecated Use `@prismicio/client`'s `ClientConfig`. */
|
|
11
|
+
export type CreateClientConfig = ClientConfig;
|
|
40
12
|
|
|
41
13
|
/**
|
|
42
14
|
* The minimal set of properties needed from `next`'s `NextRequest` type.
|
|
@@ -56,29 +28,3 @@ export type NextRequestLike = {
|
|
|
56
28
|
};
|
|
57
29
|
};
|
|
58
30
|
};
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* The minimal set of properties needed from `next`'s `NextApiRequest` type.
|
|
62
|
-
*
|
|
63
|
-
* This request type is only compatible with API routes defined in the `pages`
|
|
64
|
-
* directory.
|
|
65
|
-
*/
|
|
66
|
-
export type NextApiRequestLike = {
|
|
67
|
-
query: Partial<Record<string, string | string[]>>;
|
|
68
|
-
cookies: Partial<Record<string, string>>;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* The minimal set of properties needed from `next`'s `NextApiResponse` type.
|
|
73
|
-
*
|
|
74
|
-
* This request type is only compatible with API routes defined in the `pages`
|
|
75
|
-
* directory.
|
|
76
|
-
*/
|
|
77
|
-
export type NextApiResponseLike = {
|
|
78
|
-
redirect(url: string): NextApiResponseLike;
|
|
79
|
-
clearPreviewData(): NextApiResponseLike;
|
|
80
|
-
setHeader(name: string, value: string | string[]): NextApiResponseLike;
|
|
81
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
82
|
-
json(body: any): void;
|
|
83
|
-
setPreviewData(data: object | string): NextApiResponseLike;
|
|
84
|
-
};
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
'use client';
|
|
3
|
-
"use strict";
|
|
4
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
5
|
-
const jsxRuntime = require("react/jsx-runtime");
|
|
6
|
-
const Image = require("next/image");
|
|
7
|
-
const imgixUrlBuilder = require("imgix-url-builder");
|
|
8
|
-
const devMsg = require("./lib/devMsg.cjs");
|
|
9
|
-
const imgixLoader = require("./imgixLoader.cjs");
|
|
10
|
-
const isFilled = require('./_node_modules/@prismicio/client/dist/helpers/isFilled.cjs');
|
|
11
|
-
const castInt = (input) => {
|
|
12
|
-
if (typeof input === "number" || typeof input === "undefined") {
|
|
13
|
-
return input;
|
|
14
|
-
} else {
|
|
15
|
-
const parsed = Number.parseInt(input);
|
|
16
|
-
if (Number.isNaN(parsed)) {
|
|
17
|
-
return void 0;
|
|
18
|
-
} else {
|
|
19
|
-
return parsed;
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
const PrismicNextImage = ({ field, imgixParams = {}, alt, fallbackAlt, fill, width, height, fallback = null, ...restProps }) => {
|
|
24
|
-
if (process.env.NODE_ENV !== "production") {
|
|
25
|
-
if (typeof alt === "string" && alt !== "") {
|
|
26
|
-
console.warn(`[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.devMsg("alt-must-be-an-empty-string")}`);
|
|
27
|
-
}
|
|
28
|
-
if (typeof fallbackAlt === "string" && fallbackAlt !== "") {
|
|
29
|
-
console.warn(`[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.devMsg("alt-must-be-an-empty-string")}`);
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
if (isFilled.imageThumbnail(field)) {
|
|
33
|
-
const resolvedImgixParams = imgixParams;
|
|
34
|
-
for (const x in imgixParams) {
|
|
35
|
-
if (resolvedImgixParams[x] === null) {
|
|
36
|
-
resolvedImgixParams[x] = void 0;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
const src = imgixUrlBuilder.buildURL(field.url, imgixParams);
|
|
40
|
-
const ar = field.dimensions.width / field.dimensions.height;
|
|
41
|
-
const castedWidth = castInt(width);
|
|
42
|
-
const castedHeight = castInt(height);
|
|
43
|
-
let resolvedWidth = castedWidth ?? field.dimensions.width;
|
|
44
|
-
let resolvedHeight = castedHeight ?? field.dimensions.height;
|
|
45
|
-
if (castedWidth != null && castedHeight == null) {
|
|
46
|
-
resolvedHeight = castedWidth / ar;
|
|
47
|
-
} else if (castedWidth == null && castedHeight != null) {
|
|
48
|
-
resolvedWidth = castedHeight * ar;
|
|
49
|
-
}
|
|
50
|
-
const resolvedAlt = alt ?? (field.alt || fallbackAlt);
|
|
51
|
-
if (process.env.NODE_ENV !== "production" && typeof resolvedAlt !== "string") {
|
|
52
|
-
console.error(`[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=""\`.`, src);
|
|
53
|
-
}
|
|
54
|
-
let ResolvedImage = Image;
|
|
55
|
-
if ("default" in ResolvedImage) {
|
|
56
|
-
ResolvedImage = ResolvedImage.default;
|
|
57
|
-
}
|
|
58
|
-
return jsxRuntime.jsx(ResolvedImage, { src, width: fill ? void 0 : resolvedWidth, height: fill ? void 0 : resolvedHeight, alt: resolvedAlt, fill, loader: imgixLoader.imgixLoader, ...restProps });
|
|
59
|
-
} else {
|
|
60
|
-
return jsxRuntime.jsx(jsxRuntime.Fragment, { children: fallback });
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
exports.PrismicNextImage = PrismicNextImage;
|
|
64
|
-
//# sourceMappingURL=PrismicNextImage.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicNextImage.cjs","sources":["../../src/PrismicNextImage.tsx"],"sourcesContent":["\"use client\";\n\nimport Image, { ImageProps } from \"next/image\";\nimport { buildURL, ImgixURLParams } from \"imgix-url-builder\";\nimport * as prismic from \"@prismicio/client\";\n\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: prismic.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?: { [P in keyof ImgixURLParams]: ImgixURLParams[P] | null };\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 *\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 (process.env.NODE_ENV !== \"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 (prismic.isFilled.imageThumbnail(field)) {\n\t\tconst resolvedImgixParams = imgixParams;\n\t\tfor (const x in imgixParams) {\n\t\t\tif (resolvedImgixParams[x as keyof typeof resolvedImgixParams] === null) {\n\t\t\t\tresolvedImgixParams[x as keyof typeof resolvedImgixParams] = undefined;\n\t\t\t}\n\t\t}\n\n\t\tconst src = buildURL(field.url, imgixParams as ImgixURLParams);\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.height;\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\t// A non-null assertion is required since we can't statically\n\t\t// know if an alt attribute is available.\n\t\t// eslint-disable-next-line @typescript-eslint/no-non-null-assertion\n\t\tconst resolvedAlt = (alt ?? (field.alt || fallbackAlt))!;\n\n\t\tif (\n\t\t\tprocess.env.NODE_ENV !== \"production\" &&\n\t\t\ttypeof resolvedAlt !== \"string\"\n\t\t) {\n\t\t\tconsole.error(\n\t\t\t\t`[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=\"\"\\`.`,\n\t\t\t\tsrc,\n\t\t\t);\n\t\t}\n\n\t\t// TODO: Remove once https://github.com/vercel/next.js/issues/52216 is resolved.\n\t\t// `next/image` seems to be affected by a default + named export bundling bug.\n\t\tlet ResolvedImage = Image;\n\t\tif (\"default\" in ResolvedImage) {\n\t\t\tResolvedImage = (ResolvedImage as unknown as { default: typeof Image })\n\t\t\t\t.default;\n\t\t}\n\n\t\treturn (\n\t\t\t<ResolvedImage\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\talt={resolvedAlt}\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":[],"mappings":";;;;;;;;;;AAUA;AACC;AACQ;AAAA;AAED;AAEF;AACI;AAAA;AAEA;AAAA;AACP;AAEH;AAuDO;AAWF;AACH;AACC;AAGI;AAIL;AACC;AAGI;AAEJ;AAGF;AACC;AACA;AACK;AACH;AAA6D;AAC7D;AAGF;AAEA;AAEM;AACA;AAEF;AACA;AAEA;AACH;AAA+B;AAE/B;AAA+B;AAM1B;AAEN;AAIS;AAEJ;AAML;AACA;AACC;AACE;AAGH;AASG;AAGH;AAAkB;AAEpB;;"}
|
package/dist/PrismicNextLink.cjs
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
'use client';
|
|
3
|
-
"use strict";
|
|
4
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
5
|
-
const jsxRuntime = require("react/jsx-runtime");
|
|
6
|
-
const React = require("react");
|
|
7
|
-
const Link = require("next/link");
|
|
8
|
-
const asLinkAttrs = require('./_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.cjs');
|
|
9
|
-
function _interopNamespaceDefault(e) {
|
|
10
|
-
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
11
|
-
if (e) {
|
|
12
|
-
for (const k in e) {
|
|
13
|
-
if (k !== "default") {
|
|
14
|
-
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
15
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
16
|
-
enumerable: true,
|
|
17
|
-
get: () => e[k]
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
n.default = e;
|
|
23
|
-
return Object.freeze(n);
|
|
24
|
-
}
|
|
25
|
-
const React__namespace = /* @__PURE__ */ _interopNamespaceDefault(React);
|
|
26
|
-
const PrismicNextLink = React__namespace.forwardRef(function PrismicNextLink2(props, ref) {
|
|
27
|
-
const { field, document, linkResolver, children, ...restProps } = props;
|
|
28
|
-
const { href: computedHref, rel: computedRel, ...attrs } = asLinkAttrs.asLinkAttrs(field ?? document, {
|
|
29
|
-
linkResolver,
|
|
30
|
-
rel: typeof restProps.rel === "function" ? restProps.rel : void 0
|
|
31
|
-
});
|
|
32
|
-
const href = ("href" in restProps ? restProps.href : computedHref) || "";
|
|
33
|
-
let rel = computedRel;
|
|
34
|
-
if ("rel" in restProps && typeof restProps.rel !== "function") {
|
|
35
|
-
rel = restProps.rel;
|
|
36
|
-
}
|
|
37
|
-
return jsxRuntime.jsx(Link, { ref, ...attrs, ...restProps, href, rel, children: "children" in props ? children : field == null ? void 0 : field.text });
|
|
38
|
-
});
|
|
39
|
-
exports.PrismicNextLink = PrismicNextLink;
|
|
40
|
-
//# sourceMappingURL=PrismicNextLink.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicNextLink.cjs","sources":["../../src/PrismicNextLink.tsx"],"sourcesContent":["\"use client\";\n\nimport * as React from \"react\";\nimport * as prismic from \"@prismicio/client\";\nimport Link from \"next/link\";\n\nexport type PrismicNextLinkProps = Omit<\n\tReact.ComponentProps<typeof Link>,\n\t\"field\" | \"document\" | \"href\" | \"rel\"\n> & {\n\tlinkResolver?: prismic.LinkResolverFunction;\n\trel?: string | prismic.AsLinkAttrsConfig[\"rel\"];\n} & (\n\t\t| {\n\t\t\t\tfield: prismic.LinkField | null | undefined;\n\t\t\t\tdocument?: never;\n\t\t\t\thref?: never;\n\t\t }\n\t\t| {\n\t\t\t\tfield?: never;\n\t\t\t\tdocument: prismic.PrismicDocument | null | undefined;\n\t\t\t\thref?: never;\n\t\t }\n\t\t| {\n\t\t\t\tfield?: never;\n\t\t\t\tdocument?: never;\n\t\t\t\thref: React.ComponentProps<typeof Link>[\"href\"];\n\t\t }\n\t);\n\nexport const PrismicNextLink = React.forwardRef<\n\tHTMLAnchorElement,\n\tPrismicNextLinkProps\n>(function PrismicNextLink(props, ref): JSX.Element | null {\n\tconst { field, document, linkResolver, children, ...restProps } = props;\n\tconst {\n\t\thref: computedHref,\n\t\trel: computedRel,\n\t\t...attrs\n\t} = prismic.asLinkAttrs(field ?? document, {\n\t\tlinkResolver,\n\t\trel: typeof restProps.rel === \"function\" ? restProps.rel : undefined,\n\t});\n\n\tconst href = (\"href\" in restProps ? restProps.href : computedHref) || \"\";\n\n\tlet rel = computedRel;\n\tif (\"rel\" in restProps && typeof restProps.rel !== \"function\") {\n\t\trel = restProps.rel;\n\t}\n\n\treturn (\n\t\t<Link ref={ref} {...attrs} {...restProps} href={href} rel={rel}>\n\t\t\t{\"children\" in props ? children : field?.text}\n\t\t</Link>\n\t);\n});\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AA8BO;AAIN;AACM;AAIqC;AAC1C;AAC2D;AAG5D;AAEA;AACA;AACC;AAAgB;AAGjB;AAKD;;"}
|
package/dist/PrismicPreview.cjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const jsxRuntime = require("react/jsx-runtime");
|
|
4
|
-
const Script = require("next/script");
|
|
5
|
-
const headers = require("next/headers");
|
|
6
|
-
const PrismicPreviewClient = require("./PrismicPreviewClient.cjs");
|
|
7
|
-
const getToolbarSrc = require('./_node_modules/@prismicio/client/dist/getToolbarSrc.cjs');
|
|
8
|
-
function PrismicPreview({ repositoryName, children, ...props }) {
|
|
9
|
-
const toolbarSrc = getToolbarSrc.getToolbarSrc(repositoryName);
|
|
10
|
-
let isDraftMode = false;
|
|
11
|
-
try {
|
|
12
|
-
isDraftMode = headers.draftMode().isEnabled;
|
|
13
|
-
} catch {
|
|
14
|
-
}
|
|
15
|
-
return jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [children, jsxRuntime.jsx(PrismicPreviewClient.PrismicPreviewClient, { repositoryName, isDraftMode, ...props }), jsxRuntime.jsx(Script, { src: toolbarSrc, strategy: "lazyOnload" })] });
|
|
16
|
-
}
|
|
17
|
-
exports.PrismicPreview = PrismicPreview;
|
|
18
|
-
//# sourceMappingURL=PrismicPreview.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"PrismicPreview.cjs","sources":["../../src/PrismicPreview.tsx"],"sourcesContent":["import Script from \"next/script\";\nimport { draftMode } from \"next/headers\";\nimport * as React from \"react\";\nimport * as prismic from \"@prismicio/client\";\n\nimport { PrismicPreviewClient } from \"./PrismicPreviewClient\";\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 * **Only used in the Pages Directory (/pages).**\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\tupdatePreviewURL?: string;\n\n\t/**\n\t * **Only used in the Pages Directory (/pages).**\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\texitPreviewURL?: string;\n\n\t/**\n\t * Children to render adjacent to the Prismic Toolbar.\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 refresh the page with the\n * changes.\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\t...props\n}: PrismicPreviewProps): JSX.Element {\n\tconst toolbarSrc = prismic.getToolbarSrc(repositoryName);\n\n\tlet isDraftMode = false;\n\ttry {\n\t\tisDraftMode = draftMode().isEnabled;\n\t} catch {\n\t\t// noop - `requestAsyncStorage` propbably doesn't exist, such as\n\t\t// in the Pages Router, which causes `draftMode()` to throw. We\n\t\t// can ignore this case and assume Draft Mode is disabled.\n\t}\n\n\treturn (\n\t\t<>\n\t\t\t{children}\n\t\t\t<PrismicPreviewClient\n\t\t\t\trepositoryName={repositoryName}\n\t\t\t\tisDraftMode={isDraftMode}\n\t\t\t\t{...props}\n\t\t\t/>\n\t\t\t<Script src={toolbarSrc} strategy=\"lazyOnload\" />\n\t\t</>\n\t);\n}\n"],"names":["prismic.getToolbarSrc","draftMode","_jsxs","_Fragment","_jsx","PrismicPreviewClient"],"mappings":";;;;;;;AAgDM,SAAU,eAAe,EAC9B,gBACA,UACA,GAAG,SACkB;AACf,QAAA,aAAaA,4BAAsB,cAAc;AAEvD,MAAI,cAAc;AACd,MAAA;AACH,kBAAcC,QAAAA,UAAY,EAAA;AAAA,EAAA,QACnB;AAAA,EAIP;AAGA,SAAAC,WAAA,KAAAC,qBAAA,EAAA,UAAA,CACE,UACDC,WAAAA,IAACC,qBAAA,sBACA,EAAA,gBACA,aAAwB,GACpB,OAAK,GAEVD,WAAAA,IAAC,QAAM,EAAC,KAAK,YAAY,UAAS,aAAe,CAAA,CAAA,EAAA,CAAA;AAGpD;;"}
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
'use client';
|
|
3
|
-
"use strict";
|
|
4
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
5
|
-
const React = require("react");
|
|
6
|
-
const router = require("next/router");
|
|
7
|
-
const navigation = require("next/navigation");
|
|
8
|
-
const getPrismicPreviewCookie = require("./lib/getPrismicPreviewCookie.cjs");
|
|
9
|
-
const getPreviewCookieRepositoryName = require("./lib/getPreviewCookieRepositoryName.cjs");
|
|
10
|
-
function PrismicPreviewClient({ repositoryName, updatePreviewURL = "/api/preview", exitPreviewURL = "/api/exit-preview", isDraftMode }) {
|
|
11
|
-
let isPreviewActive = isDraftMode;
|
|
12
|
-
let isAppRouter = true;
|
|
13
|
-
let basePath = "";
|
|
14
|
-
let refresh;
|
|
15
|
-
try {
|
|
16
|
-
const router$1 = router.useRouter();
|
|
17
|
-
isAppRouter = false;
|
|
18
|
-
basePath = router$1.basePath;
|
|
19
|
-
isPreviewActive || (isPreviewActive = router$1.isPreview);
|
|
20
|
-
refresh = () => router$1.replace(router$1.asPath, void 0, { scroll: false });
|
|
21
|
-
} catch {
|
|
22
|
-
const router2 = navigation.useRouter();
|
|
23
|
-
refresh = router2.refresh;
|
|
24
|
-
}
|
|
25
|
-
React.useEffect(() => {
|
|
26
|
-
const startPreviewMode = async () => {
|
|
27
|
-
const resolvedUpdatePreviewURL = basePath + updatePreviewURL;
|
|
28
|
-
const res = await globalThis.fetch(resolvedUpdatePreviewURL);
|
|
29
|
-
if (res.redirected) {
|
|
30
|
-
refresh();
|
|
31
|
-
} else {
|
|
32
|
-
console.error(`[<PrismicPreview>] Failed to start or update Preview Mode using the "${resolvedUpdatePreviewURL}" API endpoint. Does it exist?`);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
const handlePrismicPreviewUpdate = async (event) => {
|
|
36
|
-
event.preventDefault();
|
|
37
|
-
if (isAppRouter) {
|
|
38
|
-
refresh();
|
|
39
|
-
} else {
|
|
40
|
-
await startPreviewMode();
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
const handlePrismicPreviewEnd = async (event) => {
|
|
44
|
-
event.preventDefault();
|
|
45
|
-
const resolvedExitPreviewURL = basePath + exitPreviewURL;
|
|
46
|
-
const res = await globalThis.fetch(resolvedExitPreviewURL);
|
|
47
|
-
if (res.ok) {
|
|
48
|
-
refresh();
|
|
49
|
-
} else {
|
|
50
|
-
console.error(`[<PrismicPreview>] Failed to exit Preview Mode using the "${resolvedExitPreviewURL}" API endpoint. Does it exist?`);
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
window.addEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
|
|
54
|
-
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
55
|
-
if (!isPreviewActive) {
|
|
56
|
-
const prismicPreviewCookie = getPrismicPreviewCookie.getPrismicPreviewCookie(globalThis.document.cookie);
|
|
57
|
-
if (prismicPreviewCookie) {
|
|
58
|
-
const locationIsDescendantOfBasePath = window.location.href.startsWith(window.location.origin + basePath);
|
|
59
|
-
const prismicPreviewCookieRepositoryName = getPreviewCookieRepositoryName.getPreviewCookieRepositoryName(prismicPreviewCookie);
|
|
60
|
-
if (locationIsDescendantOfBasePath && prismicPreviewCookieRepositoryName === repositoryName) {
|
|
61
|
-
startPreviewMode();
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
return () => {
|
|
66
|
-
window.removeEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
|
|
67
|
-
window.removeEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
68
|
-
};
|
|
69
|
-
}, [
|
|
70
|
-
basePath,
|
|
71
|
-
exitPreviewURL,
|
|
72
|
-
isAppRouter,
|
|
73
|
-
isPreviewActive,
|
|
74
|
-
refresh,
|
|
75
|
-
repositoryName,
|
|
76
|
-
updatePreviewURL
|
|
77
|
-
]);
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
exports.PrismicPreviewClient = PrismicPreviewClient;
|
|
81
|
-
//# sourceMappingURL=PrismicPreviewClient.cjs.map
|