@prismicio/next 1.1.0-alpha.0 → 1.1.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 +2 -19
- package/dist/PrismicNextImage.cjs.map +1 -1
- package/dist/PrismicNextImage.js +2 -2
- package/dist/PrismicNextLink.cjs +3 -4
- package/dist/PrismicNextLink.cjs.map +1 -1
- package/dist/PrismicNextLink.d.ts +16 -16
- package/dist/PrismicNextLink.js +3 -3
- package/dist/PrismicNextLink.js.map +1 -1
- package/dist/PrismicPreview.cjs +5 -74
- package/dist/PrismicPreview.cjs.map +1 -1
- package/dist/PrismicPreview.d.ts +8 -11
- package/dist/PrismicPreview.js +5 -57
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/PrismicPreviewClient.cjs +85 -0
- package/dist/PrismicPreviewClient.cjs.map +1 -0
- package/dist/PrismicPreviewClient.d.ts +4 -0
- package/dist/PrismicPreviewClient.js +85 -0
- package/dist/PrismicPreviewClient.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/cookie.cjs +5 -0
- package/dist/_node_modules/@prismicio/client/dist/cookie.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/cookie.js +5 -0
- package/dist/_node_modules/@prismicio/client/dist/cookie.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.cjs +19 -0
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.js +19 -0
- package/dist/_node_modules/@prismicio/client/dist/errors/PrismicError.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.cjs +13 -0
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.js +13 -0
- package/dist/_node_modules/@prismicio/client/dist/getToolbarSrc.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs +38 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js +38 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLink.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.cjs +28 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.js +28 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/asLinkAttrs.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.cjs +26 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.js +26 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/documentToLinkField.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.cjs +14 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.js +14 -0
- package/dist/_node_modules/@prismicio/client/dist/helpers/isFilled.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.cjs +7 -0
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.js +7 -0
- package/dist/_node_modules/@prismicio/client/dist/isRepositoryName.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.cjs +9 -0
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.js +9 -0
- package/dist/_node_modules/@prismicio/client/dist/lib/isInternalURL.js.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.cjs +10 -0
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.cjs.map +1 -0
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.js +10 -0
- package/dist/_node_modules/@prismicio/client/dist/types/value/link.js.map +1 -0
- package/dist/enableAutoPreviews.cjs +12 -6
- package/dist/enableAutoPreviews.cjs.map +1 -1
- package/dist/enableAutoPreviews.d.ts +11 -12
- package/dist/enableAutoPreviews.js +12 -6
- package/dist/enableAutoPreviews.js.map +1 -1
- package/dist/exitPreview.cjs +1 -1
- package/dist/exitPreview.cjs.map +1 -1
- package/dist/exitPreview.d.ts +11 -16
- package/dist/exitPreview.js +1 -1
- package/dist/exitPreview.js.map +1 -1
- package/dist/lib/getPrismicPreviewCookie.cjs +4 -21
- package/dist/lib/getPrismicPreviewCookie.cjs.map +1 -1
- package/dist/lib/getPrismicPreviewCookie.js +2 -2
- package/dist/lib/getPrismicPreviewCookie.js.map +1 -1
- package/dist/package.json.cjs +1 -1
- package/dist/package.json.js +1 -1
- package/dist/redirectToPreviewURL.cjs +13 -12
- package/dist/redirectToPreviewURL.cjs.map +1 -1
- package/dist/redirectToPreviewURL.d.ts +23 -21
- package/dist/redirectToPreviewURL.js +13 -12
- package/dist/redirectToPreviewURL.js.map +1 -1
- package/dist/setPreviewData.cjs +2 -19
- package/dist/setPreviewData.cjs.map +1 -1
- package/dist/setPreviewData.d.ts +9 -14
- package/dist/setPreviewData.js +2 -2
- package/dist/setPreviewData.js.map +1 -1
- package/dist/types.d.ts +52 -9
- package/package.json +5 -11
- package/src/PrismicNextLink.tsx +10 -10
- package/src/PrismicPreview.tsx +13 -131
- package/src/PrismicPreviewClient.tsx +159 -0
- package/src/enableAutoPreviews.ts +56 -45
- package/src/exitPreview.ts +14 -26
- package/src/redirectToPreviewURL.ts +58 -64
- package/src/setPreviewData.ts +10 -14
- package/src/types.ts +58 -9
- package/dist/react-server/PrismicPreview.d.ts +0 -22
- package/dist/react-server/index.d.ts +0 -2
- package/dist/react-server/unsupported.cjs +0 -26
- package/dist/react-server/unsupported.cjs.map +0 -1
- package/dist/react-server/unsupported.d.ts +0 -6
- package/dist/react-server/unsupported.js +0 -26
- package/dist/react-server/unsupported.js.map +0 -1
- package/dist/react-server.cjs +0 -15
- package/dist/react-server.cjs.map +0 -1
- package/dist/react-server.js +0 -15
- package/dist/react-server.js.map +0 -1
- package/src/react-server/PrismicPreview.tsx +0 -74
- package/src/react-server/index.ts +0 -11
- package/src/react-server/unsupported.ts +0 -26
package/dist/setPreviewData.cjs
CHANGED
|
@@ -1,25 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
4
|
-
function _interopNamespaceDefault(e) {
|
|
5
|
-
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
6
|
-
if (e) {
|
|
7
|
-
for (const k in e) {
|
|
8
|
-
if (k !== "default") {
|
|
9
|
-
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
10
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
11
|
-
enumerable: true,
|
|
12
|
-
get: () => e[k]
|
|
13
|
-
});
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
n.default = e;
|
|
18
|
-
return Object.freeze(n);
|
|
19
|
-
}
|
|
20
|
-
const prismic__namespace = /* @__PURE__ */ _interopNamespaceDefault(prismic);
|
|
3
|
+
const cookie = require('./_node_modules/@prismicio/client/dist/cookie.cjs');
|
|
21
4
|
function setPreviewData({ req, res }) {
|
|
22
|
-
const ref = req.query.token || req.cookies[
|
|
5
|
+
const ref = req.query.token || req.cookies[cookie.preview];
|
|
23
6
|
if (ref) {
|
|
24
7
|
res.setPreviewData({ ref });
|
|
25
8
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setPreviewData.cjs","sources":["../../src/setPreviewData.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"setPreviewData.cjs","sources":["../../src/setPreviewData.ts"],"sourcesContent":["import * as prismic from \"@prismicio/client\";\n\nimport { NextApiRequestLike, NextApiResponseLike } from \"./types\";\n\n/**\n * Configuration for `setPreviewData`.\n */\nexport type SetPreviewDataConfig = {\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\treq: NextApiRequestLike;\n\n\t/**\n\t * The `res` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\tres: NextApiResponseLike;\n};\n\n/**\n * **Only use this function in the Pages Directory (/pages).**\n *\n * Set Prismic preview data for Next.js's Preview Mode.\n */\nexport function setPreviewData({ req, res }: SetPreviewDataConfig): void {\n\tconst ref = req.query.token || req.cookies[prismic.cookie.preview];\n\n\tif (ref) {\n\t\tres.setPreviewData({ ref });\n\t}\n}\n"],"names":["prismic.cookie.preview"],"mappings":";;;SA4BgB,eAAe,EAAE,KAAK,OAA2B;AAC1D,QAAA,MAAM,IAAI,MAAM,SAAS,IAAI,QAAQA,OAAAA,OAAsB;AAEjE,MAAI,KAAK;AACJ,QAAA,eAAe,EAAE,IAAA,CAAK;AAAA,EAC1B;AACF;;"}
|
package/dist/setPreviewData.d.ts
CHANGED
|
@@ -1,29 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NextApiRequestLike, NextApiResponseLike } from "./types";
|
|
2
2
|
/**
|
|
3
3
|
* Configuration for `setPreviewData`.
|
|
4
4
|
*/
|
|
5
5
|
export type SetPreviewDataConfig = {
|
|
6
6
|
/**
|
|
7
|
-
* The `req` object from a Next.js API route.
|
|
8
|
-
* the API route.
|
|
7
|
+
* The `req` object from a Next.js API route.
|
|
9
8
|
*
|
|
10
|
-
* @see Next.js API route docs:
|
|
9
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
11
10
|
*/
|
|
12
|
-
req:
|
|
13
|
-
query: NextApiRequest["query"];
|
|
14
|
-
cookies: NextApiRequest["cookies"];
|
|
15
|
-
};
|
|
11
|
+
req: NextApiRequestLike;
|
|
16
12
|
/**
|
|
17
|
-
* The `res` object from a Next.js API route.
|
|
18
|
-
* the API route.
|
|
13
|
+
* The `res` object from a Next.js API route.
|
|
19
14
|
*
|
|
20
|
-
* @see Next.js API route docs:
|
|
15
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
21
16
|
*/
|
|
22
|
-
res:
|
|
23
|
-
setPreviewData: NextApiResponse["setPreviewData"];
|
|
24
|
-
};
|
|
17
|
+
res: NextApiResponseLike;
|
|
25
18
|
};
|
|
26
19
|
/**
|
|
20
|
+
* **Only use this function in the Pages Directory (/pages).**
|
|
21
|
+
*
|
|
27
22
|
* Set Prismic preview data for Next.js's Preview Mode.
|
|
28
23
|
*/
|
|
29
24
|
export declare function setPreviewData({ req, res }: SetPreviewDataConfig): void;
|
package/dist/setPreviewData.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { preview } from './_node_modules/@prismicio/client/dist/cookie.js';
|
|
2
2
|
function setPreviewData({ req, res }) {
|
|
3
|
-
const ref = req.query.token || req.cookies[
|
|
3
|
+
const ref = req.query.token || req.cookies[preview];
|
|
4
4
|
if (ref) {
|
|
5
5
|
res.setPreviewData({ ref });
|
|
6
6
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"setPreviewData.js","sources":["../../src/setPreviewData.ts"],"sourcesContent":["import
|
|
1
|
+
{"version":3,"file":"setPreviewData.js","sources":["../../src/setPreviewData.ts"],"sourcesContent":["import * as prismic from \"@prismicio/client\";\n\nimport { NextApiRequestLike, NextApiResponseLike } from \"./types\";\n\n/**\n * Configuration for `setPreviewData`.\n */\nexport type SetPreviewDataConfig = {\n\t/**\n\t * The `req` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\treq: NextApiRequestLike;\n\n\t/**\n\t * The `res` object from a Next.js API route.\n\t *\n\t * @see Next.js API route docs: \\<https://nextjs.org/docs/api-routes/introduction\\>\n\t */\n\tres: NextApiResponseLike;\n};\n\n/**\n * **Only use this function in the Pages Directory (/pages).**\n *\n * Set Prismic preview data for Next.js's Preview Mode.\n */\nexport function setPreviewData({ req, res }: SetPreviewDataConfig): void {\n\tconst ref = req.query.token || req.cookies[prismic.cookie.preview];\n\n\tif (ref) {\n\t\tres.setPreviewData({ ref });\n\t}\n}\n"],"names":["prismic.cookie.preview"],"mappings":";SA4BgB,eAAe,EAAE,KAAK,OAA2B;AAC1D,QAAA,MAAM,IAAI,MAAM,SAAS,IAAI,QAAQA,OAAsB;AAEjE,MAAI,KAAK;AACJ,QAAA,eAAe,EAAE,IAAA,CAAK;AAAA,EAC1B;AACF;"}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,21 +1,64 @@
|
|
|
1
|
-
import type { PreviewData
|
|
1
|
+
import type { PreviewData } from "next";
|
|
2
2
|
import type { ClientConfig } from "@prismicio/client";
|
|
3
|
+
declare module "@prismicio/client" {
|
|
4
|
+
interface RequestInitLike {
|
|
5
|
+
next?: RequestInit["next"];
|
|
6
|
+
}
|
|
7
|
+
}
|
|
3
8
|
/**
|
|
4
9
|
* Configuration for creating a Prismic client with automatic preview support in
|
|
5
10
|
* Next.js apps.
|
|
6
11
|
*/
|
|
7
|
-
export type CreateClientConfig = {
|
|
12
|
+
export type CreateClientConfig = ClientConfig & {
|
|
8
13
|
/**
|
|
9
|
-
*
|
|
10
|
-
* from `getStaticProps` or `getServerSideProps`.
|
|
14
|
+
* **Only used in the Pages Directory (/pages).**
|
|
11
15
|
*
|
|
12
|
-
*
|
|
16
|
+
* The `previewData` object provided in the `getStaticProps()` or
|
|
17
|
+
* `getServerSideProps()` context object.
|
|
13
18
|
*/
|
|
14
19
|
previewData?: PreviewData;
|
|
15
20
|
/**
|
|
16
|
-
*
|
|
21
|
+
* **Only used in the Pages Directory (/pages).**
|
|
17
22
|
*
|
|
18
|
-
*
|
|
23
|
+
* The `req` object from a Next.js API route.
|
|
24
|
+
*
|
|
25
|
+
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
19
26
|
*/
|
|
20
|
-
req?:
|
|
21
|
-
}
|
|
27
|
+
req?: NextApiRequestLike;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* The minimal set of properties needed from `next`'s `NextRequest` type.
|
|
31
|
+
*
|
|
32
|
+
* This request type is only compatible with Route Handlers defined in the `app`
|
|
33
|
+
* directory.
|
|
34
|
+
*/
|
|
35
|
+
export type NextRequestLike = {
|
|
36
|
+
headers: {
|
|
37
|
+
get(name: string): string | null;
|
|
38
|
+
};
|
|
39
|
+
url: string;
|
|
40
|
+
nextUrl: unknown;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* The minimal set of properties needed from `next`'s `NextApiRequest` type.
|
|
44
|
+
*
|
|
45
|
+
* This request type is only compatible with API routes defined in the `pages`
|
|
46
|
+
* directory.
|
|
47
|
+
*/
|
|
48
|
+
export type NextApiRequestLike = {
|
|
49
|
+
query: Partial<Record<string, string | string[]>>;
|
|
50
|
+
cookies: Partial<Record<string, string>>;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* The minimal set of properties needed from `next`'s `NextApiResponse` type.
|
|
54
|
+
*
|
|
55
|
+
* This request type is only compatible with API routes defined in the `pages`
|
|
56
|
+
* directory.
|
|
57
|
+
*/
|
|
58
|
+
export type NextApiResponseLike = {
|
|
59
|
+
redirect(url: string): NextApiResponseLike;
|
|
60
|
+
clearPreviewData(): NextApiResponseLike;
|
|
61
|
+
status(statusCode: number): NextApiResponseLike;
|
|
62
|
+
json(body: any): void;
|
|
63
|
+
setPreviewData(data: object | string): NextApiResponseLike;
|
|
64
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismicio/next",
|
|
3
|
-
"version": "1.1.0-alpha.
|
|
3
|
+
"version": "1.1.0-alpha.2",
|
|
4
4
|
"description": "Helpers to integrate Prismic into Next.js apps",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -18,14 +18,8 @@
|
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"exports": {
|
|
20
20
|
".": {
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
"require": "./dist/react-server.cjs"
|
|
24
|
-
},
|
|
25
|
-
"default": {
|
|
26
|
-
"import": "./dist/index.js",
|
|
27
|
-
"require": "./dist/index.cjs"
|
|
28
|
-
}
|
|
21
|
+
"import": "./dist/index.js",
|
|
22
|
+
"require": "./dist/index.cjs"
|
|
29
23
|
},
|
|
30
24
|
"./package.json": "./package.json"
|
|
31
25
|
},
|
|
@@ -52,10 +46,10 @@
|
|
|
52
46
|
"test": "npm run lint && npm run types && npm run unit && npm run build && npm run size"
|
|
53
47
|
},
|
|
54
48
|
"dependencies": {
|
|
55
|
-
"@prismicio/client": "^7.0.0-alpha.2",
|
|
56
49
|
"imgix-url-builder": "^0.0.3"
|
|
57
50
|
},
|
|
58
51
|
"devDependencies": {
|
|
52
|
+
"@prismicio/client": "^7.0.0-alpha.3",
|
|
59
53
|
"@prismicio/mock": "^0.2.0",
|
|
60
54
|
"@size-limit/preset-small-lib": "^8.2.4",
|
|
61
55
|
"@types/react-test-renderer": "^18.0.0",
|
|
@@ -70,7 +64,7 @@
|
|
|
70
64
|
"eslint-plugin-react-hooks": "^4.6.0",
|
|
71
65
|
"eslint-plugin-tsdoc": "^0.2.17",
|
|
72
66
|
"happy-dom": "^9.9.2",
|
|
73
|
-
"next": "^13.
|
|
67
|
+
"next": "^13.4.0",
|
|
74
68
|
"prettier": "^2.8.8",
|
|
75
69
|
"prettier-plugin-jsdoc": "^0.4.2",
|
|
76
70
|
"react": "^18.1.0",
|
package/src/PrismicNextLink.tsx
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import * as prismic from "@prismicio/client";
|
|
5
|
-
import Link
|
|
5
|
+
import Link from "next/link";
|
|
6
6
|
|
|
7
7
|
export type PrismicNextLinkProps = Omit<
|
|
8
8
|
React.ComponentProps<typeof Link>,
|
|
@@ -12,19 +12,19 @@ export type PrismicNextLinkProps = Omit<
|
|
|
12
12
|
rel?: string | prismic.AsLinkAttrsConfig["rel"];
|
|
13
13
|
} & (
|
|
14
14
|
| {
|
|
15
|
-
|
|
15
|
+
field: prismic.LinkField | null | undefined;
|
|
16
|
+
document?: never;
|
|
16
17
|
href?: never;
|
|
17
|
-
field?: never;
|
|
18
18
|
}
|
|
19
19
|
| {
|
|
20
|
-
field
|
|
20
|
+
field?: never;
|
|
21
|
+
document: prismic.PrismicDocument | null | undefined;
|
|
21
22
|
href?: never;
|
|
22
|
-
document?: never;
|
|
23
23
|
}
|
|
24
24
|
| {
|
|
25
|
-
|
|
26
|
-
field?: prismic.LinkField | null | undefined;
|
|
25
|
+
field?: never;
|
|
27
26
|
document?: never;
|
|
27
|
+
href: React.ComponentProps<typeof Link>["href"];
|
|
28
28
|
}
|
|
29
29
|
);
|
|
30
30
|
|
|
@@ -44,12 +44,12 @@ export const PrismicNextLink = React.forwardRef<
|
|
|
44
44
|
rel: typeof restProps.rel === "function" ? restProps.rel : undefined,
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
const href = ("href" in restProps ? restProps.href : computedHref) || "";
|
|
48
|
+
|
|
49
|
+
let rel = computedRel;
|
|
48
50
|
if ("rel" in restProps && typeof restProps.rel !== "function") {
|
|
49
51
|
rel = restProps.rel;
|
|
50
52
|
}
|
|
51
53
|
|
|
52
|
-
const href = ("href" in restProps ? restProps.href : computedHref) || "";
|
|
53
|
-
|
|
54
54
|
return <Link ref={ref} {...attrs} {...restProps} href={href} rel={rel} />;
|
|
55
55
|
});
|
package/src/PrismicPreview.tsx
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
|
|
3
1
|
import * as React from "react";
|
|
4
|
-
import
|
|
2
|
+
import * as prismic from "@prismicio/client";
|
|
5
3
|
import Script from "next/script";
|
|
6
4
|
|
|
7
|
-
import {
|
|
8
|
-
import { getPreviewCookieRepositoryName } from "./lib/getPreviewCookieRepositoryName";
|
|
5
|
+
import { PrismicPreviewClient } from "./PrismicPreviewClient";
|
|
9
6
|
|
|
10
7
|
/**
|
|
11
8
|
* Props for `<PrismicPreview>`.
|
|
@@ -18,26 +15,23 @@ export type PrismicPreviewProps = {
|
|
|
18
15
|
repositoryName: string;
|
|
19
16
|
|
|
20
17
|
/**
|
|
18
|
+
* **Only used in the Pages Directory (/pages).**
|
|
19
|
+
*
|
|
21
20
|
* The URL of your app's Prismic preview endpoint (default: `/api/preview`).
|
|
22
21
|
* This URL will be fetched on preview update events.
|
|
23
|
-
*
|
|
24
|
-
* **Note**: If your `next.config.js` file contains a `basePath`, it is
|
|
25
|
-
* automatically included.
|
|
26
22
|
*/
|
|
27
23
|
updatePreviewURL?: string;
|
|
28
24
|
|
|
29
25
|
/**
|
|
26
|
+
* **Only used in the Pages Directory (/pages).**
|
|
27
|
+
*
|
|
30
28
|
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`).
|
|
31
29
|
* This URL will be fetched on preview exit events.
|
|
32
|
-
*
|
|
33
|
-
* **Note**: If your `next.config.js` file contains a `basePath`, it is
|
|
34
|
-
* automatically included.
|
|
35
30
|
*/
|
|
36
31
|
exitPreviewURL?: string;
|
|
37
32
|
|
|
38
33
|
/**
|
|
39
|
-
* Children to render adjacent to the Prismic Toolbar.
|
|
40
|
-
* will be rendered last.
|
|
34
|
+
* Children to render adjacent to the Prismic Toolbar.
|
|
41
35
|
*/
|
|
42
36
|
children?: React.ReactNode;
|
|
43
37
|
};
|
|
@@ -45,8 +39,8 @@ export type PrismicPreviewProps = {
|
|
|
45
39
|
/**
|
|
46
40
|
* React component that sets up Prismic Previews using the Prismic Toolbar. When
|
|
47
41
|
* the Prismic Toolbar send events to the browser, such as on preview updates
|
|
48
|
-
* and exiting, this component will automatically
|
|
49
|
-
*
|
|
42
|
+
* and exiting, this component will automatically refresh the page with the
|
|
43
|
+
* changes.
|
|
50
44
|
*
|
|
51
45
|
* This component can be wrapped around your app or added anywhere in your app's
|
|
52
46
|
* tree. It must be rendered on every page.
|
|
@@ -54,127 +48,15 @@ export type PrismicPreviewProps = {
|
|
|
54
48
|
export function PrismicPreview({
|
|
55
49
|
repositoryName,
|
|
56
50
|
children,
|
|
57
|
-
|
|
58
|
-
exitPreviewURL = "/api/exit-preview",
|
|
51
|
+
...props
|
|
59
52
|
}: PrismicPreviewProps): JSX.Element {
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
const resolvedUpdatePreviewURL = router.basePath + updatePreviewURL;
|
|
63
|
-
const resolvedExitPreviewURL = router.basePath + exitPreviewURL;
|
|
64
|
-
|
|
65
|
-
React.useEffect(() => {
|
|
66
|
-
/**
|
|
67
|
-
* Starts Preview Mode and refreshes the page's props.
|
|
68
|
-
*/
|
|
69
|
-
const startPreviewMode = async () => {
|
|
70
|
-
// Start Next.js Preview Mode via the given preview API endpoint.
|
|
71
|
-
const res = await globalThis.fetch(resolvedUpdatePreviewURL);
|
|
72
|
-
|
|
73
|
-
// We check for `res.redirected` rather than `res.ok`
|
|
74
|
-
// since the update preview endpoint may redirect to a
|
|
75
|
-
// 404 page. As long as it redirects, we know the
|
|
76
|
-
// endpoint exists and at least attempted to set
|
|
77
|
-
// preview data.
|
|
78
|
-
if (res.redirected) {
|
|
79
|
-
globalThis.location.reload();
|
|
80
|
-
} else {
|
|
81
|
-
console.error(
|
|
82
|
-
`[<PrismicPreview>] Failed to start or update Preview Mode using the "${resolvedUpdatePreviewURL}" API endpoint. Does it exist?`,
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
const handlePrismicPreviewUpdate = async (event: Event) => {
|
|
88
|
-
// Prevent the toolbar from reloading the page.
|
|
89
|
-
event.preventDefault();
|
|
90
|
-
|
|
91
|
-
await startPreviewMode();
|
|
92
|
-
};
|
|
93
|
-
|
|
94
|
-
const handlePrismicPreviewEnd = async (event: Event) => {
|
|
95
|
-
// Prevent the toolbar from reloading the page.
|
|
96
|
-
event.preventDefault();
|
|
97
|
-
|
|
98
|
-
// Exit Next.js Preview Mode via the given preview API endpoint.
|
|
99
|
-
const res = await globalThis.fetch(resolvedExitPreviewURL);
|
|
100
|
-
|
|
101
|
-
if (res.ok) {
|
|
102
|
-
globalThis.location.reload();
|
|
103
|
-
} else {
|
|
104
|
-
console.error(
|
|
105
|
-
`[<PrismicPreview>] Failed to exit Preview Mode using the "${resolvedExitPreviewURL}" API endpoint. Does it exist?`,
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
if (router.isPreview) {
|
|
111
|
-
// Register Prismic Toolbar event handlers.
|
|
112
|
-
window.addEventListener(
|
|
113
|
-
"prismicPreviewUpdate",
|
|
114
|
-
handlePrismicPreviewUpdate,
|
|
115
|
-
);
|
|
116
|
-
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
117
|
-
} else {
|
|
118
|
-
const prismicPreviewCookie = getPrismicPreviewCookie(
|
|
119
|
-
globalThis.document.cookie,
|
|
120
|
-
);
|
|
121
|
-
|
|
122
|
-
if (prismicPreviewCookie) {
|
|
123
|
-
// If a Prismic preview cookie is present, but Next.js Preview
|
|
124
|
-
// Mode is not active, we must activate Preview Mode manually.
|
|
125
|
-
//
|
|
126
|
-
// This will happen when a visitor accesses the page using a
|
|
127
|
-
// Prismic preview share link.
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Determines if the current location is a descendant of the app's base
|
|
131
|
-
* path.
|
|
132
|
-
*
|
|
133
|
-
* This is used to prevent infinite refrehes; when
|
|
134
|
-
* `isDescendantOfBasePath` is `false`, `router.isPreview` is also
|
|
135
|
-
* `false`.
|
|
136
|
-
*
|
|
137
|
-
* If the app does not have a base path, this should always be `true`.
|
|
138
|
-
*/
|
|
139
|
-
const locationIsDescendantOfBasePath = window.location.href.startsWith(
|
|
140
|
-
window.location.origin + router.basePath,
|
|
141
|
-
);
|
|
142
|
-
|
|
143
|
-
const prismicPreviewCookieRepositoryName =
|
|
144
|
-
getPreviewCookieRepositoryName(prismicPreviewCookie);
|
|
145
|
-
|
|
146
|
-
if (
|
|
147
|
-
locationIsDescendantOfBasePath &&
|
|
148
|
-
prismicPreviewCookieRepositoryName === repositoryName
|
|
149
|
-
) {
|
|
150
|
-
startPreviewMode();
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// On cleanup, unregister Prismic Toolbar event handlers.
|
|
156
|
-
return () => {
|
|
157
|
-
window.removeEventListener(
|
|
158
|
-
"prismicPreviewUpdate",
|
|
159
|
-
handlePrismicPreviewUpdate,
|
|
160
|
-
);
|
|
161
|
-
window.removeEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
162
|
-
};
|
|
163
|
-
}, [
|
|
164
|
-
repositoryName,
|
|
165
|
-
resolvedExitPreviewURL,
|
|
166
|
-
resolvedUpdatePreviewURL,
|
|
167
|
-
router.isPreview,
|
|
168
|
-
router.basePath,
|
|
169
|
-
]);
|
|
53
|
+
const toolbarSrc = prismic.getToolbarSrc(repositoryName);
|
|
170
54
|
|
|
171
55
|
return (
|
|
172
56
|
<>
|
|
173
57
|
{children}
|
|
174
|
-
<
|
|
175
|
-
|
|
176
|
-
strategy="lazyOnload"
|
|
177
|
-
/>
|
|
58
|
+
<PrismicPreviewClient repositoryName={repositoryName} {...props} />
|
|
59
|
+
<Script src={toolbarSrc} strategy="lazyOnload" />
|
|
178
60
|
</>
|
|
179
61
|
);
|
|
180
62
|
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useEffect } from "react";
|
|
4
|
+
import { useRouter as usePagesRouter } from "next/router";
|
|
5
|
+
import { useRouter } from "next/navigation";
|
|
6
|
+
|
|
7
|
+
import { getPrismicPreviewCookie } from "./lib/getPrismicPreviewCookie";
|
|
8
|
+
import { getPreviewCookieRepositoryName } from "./lib/getPreviewCookieRepositoryName";
|
|
9
|
+
|
|
10
|
+
import { PrismicPreviewProps } from "./PrismicPreview";
|
|
11
|
+
|
|
12
|
+
type PrismicPreviewClientProps = Omit<PrismicPreviewProps, "children">;
|
|
13
|
+
|
|
14
|
+
export function PrismicPreviewClient({
|
|
15
|
+
repositoryName,
|
|
16
|
+
updatePreviewURL = "/api/preview",
|
|
17
|
+
exitPreviewURL = "/api/exit-preview",
|
|
18
|
+
}: PrismicPreviewClientProps): null {
|
|
19
|
+
let isAppRouter = true;
|
|
20
|
+
let isPreviewMode = false;
|
|
21
|
+
let basePath = "";
|
|
22
|
+
let refresh: () => void;
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
26
|
+
const router = usePagesRouter();
|
|
27
|
+
|
|
28
|
+
isAppRouter = false;
|
|
29
|
+
basePath = router.basePath;
|
|
30
|
+
isPreviewMode = router.isPreview;
|
|
31
|
+
refresh = () => router.replace(router.asPath, undefined, { scroll: false });
|
|
32
|
+
} catch {
|
|
33
|
+
// Assume we are in App Router. Ignore the error.
|
|
34
|
+
|
|
35
|
+
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
36
|
+
const router = useRouter();
|
|
37
|
+
|
|
38
|
+
refresh = router.refresh;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
/**
|
|
43
|
+
* Starts Preview Mode and refreshes the page's props.
|
|
44
|
+
*/
|
|
45
|
+
const startPreviewMode = async () => {
|
|
46
|
+
const resolvedUpdatePreviewURL = basePath + updatePreviewURL;
|
|
47
|
+
|
|
48
|
+
// Start Next.js Preview Mode via the given preview API endpoint.
|
|
49
|
+
const res = await globalThis.fetch(resolvedUpdatePreviewURL);
|
|
50
|
+
|
|
51
|
+
// We check for `res.redirected` rather than `res.ok`
|
|
52
|
+
// since the update preview endpoint may redirect to a
|
|
53
|
+
// 404 page. As long as it redirects, we know the
|
|
54
|
+
// endpoint exists and at least attempted to set
|
|
55
|
+
// preview data.
|
|
56
|
+
if (res.redirected) {
|
|
57
|
+
refresh();
|
|
58
|
+
} else {
|
|
59
|
+
console.error(
|
|
60
|
+
`[<PrismicPreview>] Failed to start or update Preview Mode using the "${resolvedUpdatePreviewURL}" API endpoint. Does it exist?`,
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const handlePrismicPreviewUpdate = async (event: Event) => {
|
|
66
|
+
// Prevent the toolbar from reloading the page.
|
|
67
|
+
event.preventDefault();
|
|
68
|
+
|
|
69
|
+
if (isAppRouter) {
|
|
70
|
+
refresh();
|
|
71
|
+
} else {
|
|
72
|
+
await startPreviewMode();
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const handlePrismicPreviewEnd = async (event: Event) => {
|
|
77
|
+
// Prevent the toolbar from reloading the page.
|
|
78
|
+
event.preventDefault();
|
|
79
|
+
|
|
80
|
+
if (isAppRouter) {
|
|
81
|
+
refresh();
|
|
82
|
+
} else {
|
|
83
|
+
const resolvedExitPreviewURL = basePath + exitPreviewURL;
|
|
84
|
+
|
|
85
|
+
// Exit Next.js Preview Mode via the given preview API endpoint.
|
|
86
|
+
const res = await globalThis.fetch(resolvedExitPreviewURL);
|
|
87
|
+
|
|
88
|
+
if (res.ok) {
|
|
89
|
+
// TODO: Can we do better than a full page reload?
|
|
90
|
+
// globalThis.location.reload();
|
|
91
|
+
refresh();
|
|
92
|
+
} else {
|
|
93
|
+
console.error(
|
|
94
|
+
`[<PrismicPreview>] Failed to exit Preview Mode using the "${resolvedExitPreviewURL}" API endpoint. Does it exist?`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
window.addEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
|
|
101
|
+
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
102
|
+
|
|
103
|
+
if (!isPreviewMode) {
|
|
104
|
+
const prismicPreviewCookie = getPrismicPreviewCookie(
|
|
105
|
+
globalThis.document.cookie,
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
if (prismicPreviewCookie) {
|
|
109
|
+
// If a Prismic preview cookie is present, but Next.js Preview
|
|
110
|
+
// Mode is not active, we must activate Preview Mode manually.
|
|
111
|
+
//
|
|
112
|
+
// This will happen when a visitor accesses the page using a
|
|
113
|
+
// Prismic preview share link.
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Determines if the current location is a descendant of the app's base
|
|
117
|
+
* path.
|
|
118
|
+
*
|
|
119
|
+
* This is used to prevent infinite refrehes; when
|
|
120
|
+
* `isDescendantOfBasePath` is `false`, `router.isPreview` is also
|
|
121
|
+
* `false`.
|
|
122
|
+
*
|
|
123
|
+
* If the app does not have a base path, this should always be `true`.
|
|
124
|
+
*/
|
|
125
|
+
const locationIsDescendantOfBasePath = window.location.href.startsWith(
|
|
126
|
+
window.location.origin + basePath,
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const prismicPreviewCookieRepositoryName =
|
|
130
|
+
getPreviewCookieRepositoryName(prismicPreviewCookie);
|
|
131
|
+
|
|
132
|
+
if (
|
|
133
|
+
locationIsDescendantOfBasePath &&
|
|
134
|
+
prismicPreviewCookieRepositoryName === repositoryName
|
|
135
|
+
) {
|
|
136
|
+
startPreviewMode();
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return () => {
|
|
142
|
+
window.removeEventListener(
|
|
143
|
+
"prismicPreviewUpdate",
|
|
144
|
+
handlePrismicPreviewUpdate,
|
|
145
|
+
);
|
|
146
|
+
window.removeEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
147
|
+
};
|
|
148
|
+
}, [
|
|
149
|
+
basePath,
|
|
150
|
+
exitPreviewURL,
|
|
151
|
+
isAppRouter,
|
|
152
|
+
isPreviewMode,
|
|
153
|
+
refresh,
|
|
154
|
+
repositoryName,
|
|
155
|
+
updatePreviewURL,
|
|
156
|
+
]);
|
|
157
|
+
|
|
158
|
+
return null;
|
|
159
|
+
}
|