@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
package/package.json
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
{
|
|
2
|
+
"workspaces": [
|
|
3
|
+
".",
|
|
4
|
+
"e2e-projects/*"
|
|
5
|
+
],
|
|
2
6
|
"name": "@prismicio/next",
|
|
3
|
-
"version": "
|
|
7
|
+
"version": "2.0.0-alpha.0",
|
|
4
8
|
"description": "Helpers to integrate Prismic into Next.js apps",
|
|
5
9
|
"keywords": [
|
|
6
10
|
"typescript",
|
|
@@ -11,26 +15,33 @@
|
|
|
11
15
|
],
|
|
12
16
|
"repository": {
|
|
13
17
|
"type": "git",
|
|
14
|
-
"url": "https://github.com/prismicio/prismic-next"
|
|
18
|
+
"url": "git+https://github.com/prismicio/prismic-next.git"
|
|
15
19
|
},
|
|
16
20
|
"license": "Apache-2.0",
|
|
17
21
|
"author": "Prismic <contact@prismic.io> (https://prismic.io)",
|
|
18
22
|
"sideEffects": false,
|
|
23
|
+
"type": "module",
|
|
19
24
|
"exports": {
|
|
20
25
|
".": {
|
|
21
|
-
"
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
"
|
|
26
|
-
|
|
27
|
-
"default": "./dist/index.js"
|
|
28
|
-
}
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"default": "./dist/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./pages": {
|
|
30
|
+
"types": "./dist/pages/index.d.ts",
|
|
31
|
+
"default": "./dist/pages.js"
|
|
29
32
|
},
|
|
30
33
|
"./package.json": "./package.json"
|
|
31
34
|
},
|
|
32
|
-
"
|
|
33
|
-
|
|
35
|
+
"typesVersions": {
|
|
36
|
+
"*": {
|
|
37
|
+
"*": [
|
|
38
|
+
"./dist/index.d.ts"
|
|
39
|
+
],
|
|
40
|
+
"pages": [
|
|
41
|
+
"./dist/pages/index.d.ts"
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
},
|
|
34
45
|
"files": [
|
|
35
46
|
"dist",
|
|
36
47
|
"src"
|
|
@@ -39,63 +50,59 @@
|
|
|
39
50
|
"build": "vite build",
|
|
40
51
|
"dev": "vite build --watch",
|
|
41
52
|
"format": "prettier --write .",
|
|
42
|
-
"lint": "eslint
|
|
43
|
-
"types": "tsc --noEmit",
|
|
53
|
+
"lint": "eslint",
|
|
44
54
|
"prepare": "npm run build",
|
|
45
55
|
"release": "npm run build && npm run test && standard-version && git push --follow-tags && npm run build && npm publish",
|
|
46
|
-
"release:alpha": "npm run build && npm run test && standard-version --release-as
|
|
47
|
-
"release:alpha:dry": "standard-version --release-as
|
|
56
|
+
"release:alpha": "npm run build && npm run test && standard-version --release-as major --prerelease alpha && git push --follow-tags && npm run build && npm publish --tag alpha",
|
|
57
|
+
"release:alpha:dry": "standard-version --release-as major --prerelease alpha --dry-run",
|
|
48
58
|
"release:dry": "standard-version --dry-run",
|
|
49
|
-
"unit": "vitest run --coverage",
|
|
50
|
-
"unit:watch": "vitest watch",
|
|
51
59
|
"size": "size-limit",
|
|
52
|
-
"test": "npm run lint && npm run types && npm run
|
|
60
|
+
"test": "npm run lint && npm run types && npm run build && npm run e2e && npm run size && npm run e2e",
|
|
61
|
+
"types": "tsc --noEmit",
|
|
62
|
+
"e2e": "playwright test",
|
|
63
|
+
"e2e:ui": "npm run e2e -- --ui"
|
|
53
64
|
},
|
|
54
65
|
"dependencies": {
|
|
55
|
-
"
|
|
56
|
-
"imgix-url-builder": "^0.0.
|
|
57
|
-
"negotiator": "^0.6.3"
|
|
66
|
+
"esm-env": "^1.2.1",
|
|
67
|
+
"imgix-url-builder": "^0.0.5"
|
|
58
68
|
},
|
|
59
69
|
"devDependencies": {
|
|
60
|
-
"@
|
|
61
|
-
"@
|
|
62
|
-
"@
|
|
63
|
-
"@types
|
|
64
|
-
"@
|
|
65
|
-
"@
|
|
66
|
-
"@
|
|
67
|
-
"@
|
|
68
|
-
"@
|
|
69
|
-
"
|
|
70
|
-
"eslint
|
|
71
|
-
"eslint-
|
|
72
|
-
"eslint-plugin-
|
|
73
|
-
"eslint-plugin-react
|
|
74
|
-
"eslint-plugin-
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"prettier": "^3.0
|
|
80
|
-
"
|
|
81
|
-
"react": "^
|
|
82
|
-
"
|
|
83
|
-
"
|
|
84
|
-
"rollup-plugin-preserve-directives": "^0.2.0",
|
|
85
|
-
"size-limit": "^9.0.0",
|
|
70
|
+
"@eslint/js": "^9.17.0",
|
|
71
|
+
"@playwright/test": "^1.49.1",
|
|
72
|
+
"@prismicio/client": "^7.12.0",
|
|
73
|
+
"@prismicio/types-internal": "^3.3.0",
|
|
74
|
+
"@rollup/plugin-typescript": "^12.1.2",
|
|
75
|
+
"@size-limit/preset-small-lib": "^11.1.6",
|
|
76
|
+
"@types/node": "^22.10.2",
|
|
77
|
+
"@types/react": "^19.0.2",
|
|
78
|
+
"@vitejs/plugin-react": "^4.3.4",
|
|
79
|
+
"dotenv": "^16.4.7",
|
|
80
|
+
"eslint": "^9.17.0",
|
|
81
|
+
"eslint-config-prettier": "^9.1.0",
|
|
82
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
83
|
+
"eslint-plugin-react": "^7.37.2",
|
|
84
|
+
"eslint-plugin-react-hooks": "^5.1.0",
|
|
85
|
+
"eslint-plugin-tsdoc": "^0.4.0",
|
|
86
|
+
"next": "^15.1.0",
|
|
87
|
+
"playwright": "^1.49.1",
|
|
88
|
+
"prettier": "^3.4.2",
|
|
89
|
+
"prettier-plugin-jsdoc": "^1.3.0",
|
|
90
|
+
"react": "^19.0.0",
|
|
91
|
+
"react-dom": "^19.0.0",
|
|
92
|
+
"rollup-preserve-directives": "^1.1.3",
|
|
93
|
+
"size-limit": "^11.1.6",
|
|
86
94
|
"standard-version": "^9.5.0",
|
|
87
|
-
"typescript": "^5.
|
|
88
|
-
"
|
|
89
|
-
"vite
|
|
90
|
-
"vitest": "^0.34.5"
|
|
95
|
+
"typescript": "^5.7.2",
|
|
96
|
+
"typescript-eslint": "^8.18.1",
|
|
97
|
+
"vite": "^6.0.4"
|
|
91
98
|
},
|
|
92
99
|
"peerDependencies": {
|
|
93
|
-
"@prismicio/client": "^
|
|
100
|
+
"@prismicio/client": "^7",
|
|
94
101
|
"next": "^13.4.5 || ^14 || ^15.0.0-rc.0",
|
|
95
102
|
"react": "^18 || ^19.0.0-rc.0"
|
|
96
103
|
},
|
|
97
104
|
"engines": {
|
|
98
|
-
"node": ">=
|
|
105
|
+
"node": ">=18"
|
|
99
106
|
},
|
|
100
107
|
"publishConfig": {
|
|
101
108
|
"access": "public"
|
package/src/PrismicNextImage.tsx
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
+
import { JSX } from "react";
|
|
3
4
|
import Image, { ImageProps } from "next/image";
|
|
4
5
|
import { buildURL, ImgixURLParams } from "imgix-url-builder";
|
|
5
|
-
import
|
|
6
|
+
import { ImageFieldImage, isFilled } from "@prismicio/client";
|
|
7
|
+
import { DEV } from "esm-env";
|
|
6
8
|
|
|
7
|
-
import { devMsg } from "./lib/devMsg";
|
|
9
|
+
import { devMsg } from "./lib/devMsg.js";
|
|
8
10
|
|
|
9
|
-
import { imgixLoader } from "./imgixLoader";
|
|
11
|
+
import { imgixLoader } from "./imgixLoader.js";
|
|
10
12
|
|
|
11
13
|
const castInt = (input: string | number | undefined): number | undefined => {
|
|
12
14
|
if (typeof input === "number" || typeof input === "undefined") {
|
|
@@ -22,11 +24,12 @@ const castInt = (input: string | number | undefined): number | undefined => {
|
|
|
22
24
|
}
|
|
23
25
|
};
|
|
24
26
|
|
|
25
|
-
export type PrismicNextImageProps = Omit<
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
field
|
|
27
|
+
export type PrismicNextImageProps = Omit<
|
|
28
|
+
ImageProps,
|
|
29
|
+
"src" | "alt" | "loader"
|
|
30
|
+
> & {
|
|
31
|
+
/** The Prismic Image field or thumbnail to render. */
|
|
32
|
+
field: ImageFieldImage | null | undefined;
|
|
30
33
|
|
|
31
34
|
/**
|
|
32
35
|
* An object of Imgix URL API parameters to transform the image.
|
|
@@ -57,6 +60,8 @@ export type PrismicNextImageProps = Omit<ImageProps, "src" | "alt"> & {
|
|
|
57
60
|
* be rendered.
|
|
58
61
|
*/
|
|
59
62
|
fallback?: React.ReactNode;
|
|
63
|
+
|
|
64
|
+
loader?: ImageProps["loader"] | null;
|
|
60
65
|
};
|
|
61
66
|
|
|
62
67
|
/**
|
|
@@ -84,9 +89,10 @@ export const PrismicNextImage = ({
|
|
|
84
89
|
width,
|
|
85
90
|
height,
|
|
86
91
|
fallback = null,
|
|
92
|
+
loader = imgixLoader,
|
|
87
93
|
...restProps
|
|
88
94
|
}: PrismicNextImageProps): JSX.Element => {
|
|
89
|
-
if (
|
|
95
|
+
if (DEV) {
|
|
90
96
|
if (typeof alt === "string" && alt !== "") {
|
|
91
97
|
console.warn(
|
|
92
98
|
`[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(
|
|
@@ -104,7 +110,7 @@ export const PrismicNextImage = ({
|
|
|
104
110
|
}
|
|
105
111
|
}
|
|
106
112
|
|
|
107
|
-
if (
|
|
113
|
+
if (isFilled.imageThumbnail(field)) {
|
|
108
114
|
const resolvedImgixParams = imgixParams;
|
|
109
115
|
for (const x in imgixParams) {
|
|
110
116
|
if (resolvedImgixParams[x as keyof typeof resolvedImgixParams] === null) {
|
|
@@ -130,13 +136,10 @@ export const PrismicNextImage = ({
|
|
|
130
136
|
|
|
131
137
|
// A non-null assertion is required since we can't statically
|
|
132
138
|
// know if an alt attribute is available.
|
|
133
|
-
|
|
139
|
+
|
|
134
140
|
const resolvedAlt = (alt ?? (field.alt || fallbackAlt))!;
|
|
135
141
|
|
|
136
|
-
if (
|
|
137
|
-
process.env.NODE_ENV !== "production" &&
|
|
138
|
-
typeof resolvedAlt !== "string"
|
|
139
|
-
) {
|
|
142
|
+
if (DEV && typeof resolvedAlt !== "string") {
|
|
140
143
|
console.error(
|
|
141
144
|
`[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=""\`.`,
|
|
142
145
|
src,
|
|
@@ -158,7 +161,7 @@ export const PrismicNextImage = ({
|
|
|
158
161
|
height={fill ? undefined : resolvedHeight}
|
|
159
162
|
alt={resolvedAlt}
|
|
160
163
|
fill={fill}
|
|
161
|
-
loader={
|
|
164
|
+
loader={loader === null ? undefined : loader}
|
|
162
165
|
{...restProps}
|
|
163
166
|
/>
|
|
164
167
|
);
|
package/src/PrismicNextLink.tsx
CHANGED
|
@@ -1,43 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import * as React from "react";
|
|
4
|
-
import * as prismic from "@prismicio/client";
|
|
1
|
+
import { ComponentProps, forwardRef } from "react";
|
|
5
2
|
import Link from "next/link";
|
|
3
|
+
import {
|
|
4
|
+
AsLinkAttrsConfig,
|
|
5
|
+
LinkField,
|
|
6
|
+
LinkResolverFunction,
|
|
7
|
+
PrismicDocument,
|
|
8
|
+
asLinkAttrs,
|
|
9
|
+
} from "@prismicio/client";
|
|
6
10
|
|
|
7
11
|
export type PrismicNextLinkProps = Omit<
|
|
8
|
-
|
|
12
|
+
ComponentProps<typeof Link>,
|
|
9
13
|
"field" | "document" | "href" | "rel"
|
|
10
14
|
> & {
|
|
11
|
-
linkResolver?:
|
|
12
|
-
rel?: string |
|
|
15
|
+
linkResolver?: LinkResolverFunction;
|
|
16
|
+
rel?: string | AsLinkAttrsConfig["rel"];
|
|
13
17
|
} & (
|
|
14
18
|
| {
|
|
15
|
-
field:
|
|
19
|
+
field: LinkField | null | undefined;
|
|
16
20
|
document?: never;
|
|
17
21
|
href?: never;
|
|
18
22
|
}
|
|
19
23
|
| {
|
|
20
24
|
field?: never;
|
|
21
|
-
document:
|
|
25
|
+
document: PrismicDocument | null | undefined;
|
|
22
26
|
href?: never;
|
|
23
27
|
}
|
|
24
28
|
| {
|
|
25
29
|
field?: never;
|
|
26
30
|
document?: never;
|
|
27
|
-
href:
|
|
31
|
+
href: ComponentProps<typeof Link>["href"];
|
|
28
32
|
}
|
|
29
33
|
);
|
|
30
34
|
|
|
31
|
-
export const PrismicNextLink =
|
|
35
|
+
export const PrismicNextLink = forwardRef<
|
|
32
36
|
HTMLAnchorElement,
|
|
33
37
|
PrismicNextLinkProps
|
|
34
|
-
>(function PrismicNextLink(props, ref)
|
|
38
|
+
>(function PrismicNextLink(props, ref) {
|
|
35
39
|
const { field, document, linkResolver, children, ...restProps } = props;
|
|
36
40
|
const {
|
|
37
41
|
href: computedHref,
|
|
38
42
|
rel: computedRel,
|
|
39
43
|
...attrs
|
|
40
|
-
} =
|
|
44
|
+
} = asLinkAttrs(field ?? document, {
|
|
41
45
|
linkResolver,
|
|
42
46
|
rel: typeof restProps.rel === "function" ? restProps.rel : undefined,
|
|
43
47
|
});
|
package/src/PrismicPreview.tsx
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
+
import type { ReactNode, JSX } from "react";
|
|
1
2
|
import Script from "next/script";
|
|
2
|
-
import {
|
|
3
|
-
import * as React from "react";
|
|
4
|
-
import * as prismic from "@prismicio/client";
|
|
3
|
+
import { getToolbarSrc } from "@prismicio/client";
|
|
5
4
|
|
|
6
|
-
import { PrismicPreviewClient } from "./PrismicPreviewClient";
|
|
5
|
+
import { PrismicPreviewClient } from "./PrismicPreviewClient.js";
|
|
7
6
|
|
|
8
|
-
/**
|
|
9
|
-
* Props for `<PrismicPreview>`.
|
|
10
|
-
*/
|
|
7
|
+
/** Props for `<PrismicPreview>`. */
|
|
11
8
|
export type PrismicPreviewProps = {
|
|
12
9
|
/**
|
|
13
10
|
* The name of your Prismic repository. A Prismic Toolbar will be registered
|
|
@@ -16,25 +13,19 @@ export type PrismicPreviewProps = {
|
|
|
16
13
|
repositoryName: string;
|
|
17
14
|
|
|
18
15
|
/**
|
|
19
|
-
* **Only used in the Pages Directory (/pages).**
|
|
20
|
-
*
|
|
21
16
|
* The URL of your app's Prismic preview endpoint (default: `/api/preview`).
|
|
22
17
|
* This URL will be fetched on preview update events.
|
|
23
18
|
*/
|
|
24
19
|
updatePreviewURL?: string;
|
|
25
20
|
|
|
26
21
|
/**
|
|
27
|
-
* **Only used in the Pages Directory (/pages).**
|
|
28
|
-
*
|
|
29
22
|
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`).
|
|
30
23
|
* This URL will be fetched on preview exit events.
|
|
31
24
|
*/
|
|
32
25
|
exitPreviewURL?: string;
|
|
33
26
|
|
|
34
|
-
/**
|
|
35
|
-
|
|
36
|
-
*/
|
|
37
|
-
children?: React.ReactNode;
|
|
27
|
+
/** Children to render adjacent to the Prismic Toolbar. */
|
|
28
|
+
children?: ReactNode;
|
|
38
29
|
};
|
|
39
30
|
|
|
40
31
|
/**
|
|
@@ -46,21 +37,18 @@ export type PrismicPreviewProps = {
|
|
|
46
37
|
* This component can be wrapped around your app or added anywhere in your app's
|
|
47
38
|
* tree. It must be rendered on every page.
|
|
48
39
|
*/
|
|
49
|
-
export function PrismicPreview(
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
...props
|
|
53
|
-
|
|
54
|
-
|
|
40
|
+
export async function PrismicPreview(
|
|
41
|
+
props: PrismicPreviewProps,
|
|
42
|
+
): Promise<JSX.Element> {
|
|
43
|
+
const { repositoryName, children, ...otherProps } = props;
|
|
44
|
+
|
|
45
|
+
// Need this to avoid the following Next.js build-time error:
|
|
46
|
+
// You're importing a component that needs next/headers. That only works
|
|
47
|
+
// in a Server Component which is not supported in the pages/ directory.
|
|
48
|
+
const { draftMode } = await import("next/headers");
|
|
55
49
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
isDraftMode = draftMode().isEnabled;
|
|
59
|
-
} catch {
|
|
60
|
-
// noop - `requestAsyncStorage` propbably doesn't exist, such as
|
|
61
|
-
// in the Pages Router, which causes `draftMode()` to throw. We
|
|
62
|
-
// can ignore this case and assume Draft Mode is disabled.
|
|
63
|
-
}
|
|
50
|
+
const toolbarSrc = getToolbarSrc(repositoryName);
|
|
51
|
+
const isDraftMode = (await draftMode()).isEnabled;
|
|
64
52
|
|
|
65
53
|
return (
|
|
66
54
|
<>
|
|
@@ -68,7 +56,7 @@ export function PrismicPreview({
|
|
|
68
56
|
<PrismicPreviewClient
|
|
69
57
|
repositoryName={repositoryName}
|
|
70
58
|
isDraftMode={isDraftMode}
|
|
71
|
-
{...
|
|
59
|
+
{...otherProps}
|
|
72
60
|
/>
|
|
73
61
|
<Script src={toolbarSrc} strategy="lazyOnload" />
|
|
74
62
|
</>
|
|
@@ -1,156 +1,129 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
3
|
import { useEffect } from "react";
|
|
4
|
-
import {
|
|
4
|
+
import { cookie as prismicCookie } from "@prismicio/client";
|
|
5
5
|
import { useRouter } from "next/navigation";
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
import { PrismicPreviewProps } from "./PrismicPreview";
|
|
11
|
-
|
|
12
|
-
type PrismicPreviewClientProps = Omit<PrismicPreviewProps, "children"> & {
|
|
7
|
+
type PrismicPreviewClientProps = {
|
|
8
|
+
repositoryName: string;
|
|
13
9
|
isDraftMode: boolean;
|
|
10
|
+
updatePreviewURL?: string;
|
|
11
|
+
exitPreviewURL?: string;
|
|
14
12
|
};
|
|
15
13
|
|
|
16
|
-
export function PrismicPreviewClient({
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
let refresh: () => void;
|
|
26
|
-
|
|
27
|
-
try {
|
|
28
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
29
|
-
const router = usePagesRouter();
|
|
30
|
-
|
|
31
|
-
isAppRouter = false;
|
|
32
|
-
basePath = router.basePath;
|
|
33
|
-
isPreviewActive ||= router.isPreview;
|
|
34
|
-
refresh = () => router.replace(router.asPath, undefined, { scroll: false });
|
|
35
|
-
} catch {
|
|
36
|
-
// Assume we are in App Router. Ignore the error.
|
|
37
|
-
|
|
38
|
-
// eslint-disable-next-line react-hooks/rules-of-hooks
|
|
39
|
-
const router = useRouter();
|
|
40
|
-
|
|
41
|
-
refresh = router.refresh;
|
|
42
|
-
}
|
|
14
|
+
export function PrismicPreviewClient(props: PrismicPreviewClientProps): null {
|
|
15
|
+
const {
|
|
16
|
+
repositoryName,
|
|
17
|
+
isDraftMode,
|
|
18
|
+
updatePreviewURL = "/api/preview",
|
|
19
|
+
exitPreviewURL = "/api/exit-preview",
|
|
20
|
+
} = props;
|
|
21
|
+
|
|
22
|
+
const { refresh } = useRouter();
|
|
43
23
|
|
|
44
24
|
useEffect(() => {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
//
|
|
70
|
-
|
|
25
|
+
const controller = new AbortController();
|
|
26
|
+
|
|
27
|
+
window.addEventListener("prismicPreviewUpdate", onUpdate, {
|
|
28
|
+
signal: controller.signal,
|
|
29
|
+
});
|
|
30
|
+
window.addEventListener("prismicPreviewEnd", onEnd, {
|
|
31
|
+
signal: controller.signal,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const cookie = getPrismicPreviewCookie(window.document.cookie);
|
|
35
|
+
const cookieRepositoryName = cookie
|
|
36
|
+
? (decodeURIComponent(cookie).match(/"([^"]+)\.prismic\.io"/) || [])[1]
|
|
37
|
+
: undefined;
|
|
38
|
+
const hasCookieForRepository = cookieRepositoryName === repositoryName;
|
|
39
|
+
|
|
40
|
+
// Start the preview for preview share links. Previews from
|
|
41
|
+
// share links do not go to the `updatePreviewURL` like a normal
|
|
42
|
+
// preview.
|
|
43
|
+
if (hasCookieForRepository && !isDraftMode) {
|
|
44
|
+
console.log("starting preview link");
|
|
45
|
+
|
|
46
|
+
// We check `opaqueredirect` because we don't care if
|
|
47
|
+
// the redirect was successful or not. As long as it
|
|
48
|
+
// redirects, we know the endpoint exists and draft mode
|
|
49
|
+
// is active.
|
|
50
|
+
globalThis
|
|
51
|
+
.fetch(updatePreviewURL, {
|
|
52
|
+
redirect: "manual",
|
|
53
|
+
signal: controller.signal,
|
|
54
|
+
})
|
|
55
|
+
.then((res) => {
|
|
56
|
+
if (res.type !== "opaqueredirect") {
|
|
57
|
+
console.error(
|
|
58
|
+
`[<PrismicPreview>] Failed to start the preview using "${updatePreviewURL}". Does it exist?`,
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
refresh();
|
|
65
|
+
})
|
|
66
|
+
.catch(() => {
|
|
67
|
+
// noop
|
|
68
|
+
});
|
|
69
|
+
}
|
|
71
70
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
await startPreviewMode();
|
|
76
|
-
}
|
|
77
|
-
};
|
|
71
|
+
function onUpdate() {
|
|
72
|
+
refresh();
|
|
73
|
+
}
|
|
78
74
|
|
|
79
|
-
|
|
80
|
-
// Prevent the toolbar from reloading the page.
|
|
75
|
+
function onEnd(event: Event) {
|
|
81
76
|
event.preventDefault();
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
99
|
-
|
|
100
|
-
if (!isPreviewActive) {
|
|
101
|
-
const prismicPreviewCookie = getPrismicPreviewCookie(
|
|
102
|
-
globalThis.document.cookie,
|
|
103
|
-
);
|
|
104
|
-
|
|
105
|
-
if (prismicPreviewCookie) {
|
|
106
|
-
// If a Prismic preview cookie is present, but Next.js Preview
|
|
107
|
-
// Mode is not active, we must activate Preview Mode manually.
|
|
108
|
-
//
|
|
109
|
-
// This will happen when a visitor accesses the page using a
|
|
110
|
-
// Prismic preview share link.
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Determines if the current location is a descendant of the app's base
|
|
114
|
-
* path.
|
|
115
|
-
*
|
|
116
|
-
* This is used to prevent infinite refrehes; when
|
|
117
|
-
* `isDescendantOfBasePath` is `false`, `router.isPreview` is also
|
|
118
|
-
* `false`.
|
|
119
|
-
*
|
|
120
|
-
* If the app does not have a base path, this should always be `true`.
|
|
121
|
-
*/
|
|
122
|
-
const locationIsDescendantOfBasePath = window.location.href.startsWith(
|
|
123
|
-
window.location.origin + basePath,
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
const prismicPreviewCookieRepositoryName =
|
|
127
|
-
getPreviewCookieRepositoryName(prismicPreviewCookie);
|
|
128
|
-
|
|
129
|
-
if (
|
|
130
|
-
locationIsDescendantOfBasePath &&
|
|
131
|
-
prismicPreviewCookieRepositoryName === repositoryName
|
|
132
|
-
) {
|
|
133
|
-
startPreviewMode();
|
|
134
|
-
}
|
|
135
|
-
}
|
|
77
|
+
globalThis
|
|
78
|
+
.fetch(exitPreviewURL, { signal: controller.signal })
|
|
79
|
+
.then((res) => {
|
|
80
|
+
if (!res.ok) {
|
|
81
|
+
console.error(
|
|
82
|
+
`[<PrismicPreview>] Failed to exit Preview Mode using the "${exitPreviewURL}" API endpoint. Does it exist?`,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
refresh();
|
|
89
|
+
})
|
|
90
|
+
.catch(() => {
|
|
91
|
+
// noop
|
|
92
|
+
});
|
|
136
93
|
}
|
|
137
94
|
|
|
138
|
-
return () =>
|
|
139
|
-
|
|
140
|
-
"prismicPreviewUpdate",
|
|
141
|
-
handlePrismicPreviewUpdate,
|
|
142
|
-
);
|
|
143
|
-
window.removeEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
144
|
-
};
|
|
145
|
-
}, [
|
|
146
|
-
basePath,
|
|
147
|
-
exitPreviewURL,
|
|
148
|
-
isAppRouter,
|
|
149
|
-
isPreviewActive,
|
|
150
|
-
refresh,
|
|
151
|
-
repositoryName,
|
|
152
|
-
updatePreviewURL,
|
|
153
|
-
]);
|
|
95
|
+
return () => controller.abort();
|
|
96
|
+
}, [repositoryName, isDraftMode, updatePreviewURL, exitPreviewURL, refresh]);
|
|
154
97
|
|
|
155
98
|
return null;
|
|
156
99
|
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Returns the value of a cookie from a given cookie store.
|
|
103
|
+
*
|
|
104
|
+
* @param cookieJar - The stringified cookie store from which to read the
|
|
105
|
+
* cookie.
|
|
106
|
+
*
|
|
107
|
+
* @returns The value of the cookie, if it exists.
|
|
108
|
+
*/
|
|
109
|
+
function getPrismicPreviewCookie(cookieJar: string): string | undefined {
|
|
110
|
+
function readValue(value: string): string {
|
|
111
|
+
return value.replace(/%3B/g, ";");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const cookies = cookieJar.split("; ");
|
|
115
|
+
|
|
116
|
+
let value: string | undefined;
|
|
117
|
+
|
|
118
|
+
for (const cookie of cookies) {
|
|
119
|
+
const parts = cookie.split("=");
|
|
120
|
+
const name = readValue(parts[0]).replace(/%3D/g, "=");
|
|
121
|
+
|
|
122
|
+
if (name === prismicCookie.preview) {
|
|
123
|
+
value = readValue(parts.slice(1).join("="));
|
|
124
|
+
continue;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return value;
|
|
129
|
+
}
|