/
PrismicPreview.tsx
168 lines (147 loc) · 4.82 KB
/
PrismicPreview.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import * as React from "react";
import { PrismicToolbar } from "@prismicio/react";
import { useRouter } from "next/router";
import { getPrismicPreviewCookie } from "./lib/getPrismicPreviewCookie";
import { getPreviewCookieRepositoryName } from "./lib/getPreviewCookieRepositoryName";
/**
* Props for `<PrismicPreview>`.
*/
export type PrismicPreviewProps = {
/**
* The name of your Prismic repository. A Prismic Toolbar will be registered
* using this repository.
*/
repositoryName: string;
/**
* The URL of your app's Prismic preview endpoint (default: `/api/preview`).
* This URL will be fetched on preview update events.
*
* **Note**: If your `next.config.js` file contains a `basePath`, it is
* automatically included.
*/
updatePreviewURL?: string;
/**
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`).
* This URL will be fetched on preview exit events.
*
* **Note**: If your `next.config.js` file contains a `basePath`, it is
* automatically included.
*/
exitPreviewURL?: string;
/**
* Children to render adjacent to the Prismic Toolbar. The Prismic Toolbar
* will be rendered last.
*/
children?: React.ReactNode;
};
/**
* React component that sets up Prismic Previews using the Prismic Toolbar. When
* the Prismic Toolbar send events to the browser, such as on preview updates
* and exiting, this component will automatically update the Next.js preview
* cookie and refresh the page.
*
* This component can be wrapped around your app or added anywhere in your app's
* tree. It must be rendered on every page.
*/
export function PrismicPreview({
repositoryName,
children,
updatePreviewURL = "/api/preview",
exitPreviewURL = "/api/exit-preview",
}: PrismicPreviewProps): JSX.Element {
const router = useRouter();
const resolvedUpdatePreviewURL = router.basePath + updatePreviewURL;
const resolvedExitPreviewURL = router.basePath + exitPreviewURL;
React.useEffect(() => {
/**
* Starts Preview Mode and refreshes the page's props.
*/
const startPreviewMode = async () => {
// Start Next.js Preview Mode via the given preview API endpoint.
const res = await globalThis.fetch(resolvedUpdatePreviewURL);
if (res.ok) {
globalThis.location.reload();
} else {
console.error(
`[<PrismicPreview>] Failed to start or update Preview Mode using the "${resolvedUpdatePreviewURL}" API endpoint. Does it exist?`,
);
}
};
const handlePrismicPreviewUpdate = async (event: Event) => {
// Prevent the toolbar from reloading the page.
event.preventDefault();
await startPreviewMode();
};
const handlePrismicPreviewEnd = async (event: Event) => {
// Prevent the toolbar from reloading the page.
event.preventDefault();
// Exit Next.js Preview Mode via the given preview API endpoint.
const res = await globalThis.fetch(resolvedExitPreviewURL);
if (res.ok) {
globalThis.location.reload();
} else {
console.error(
`[<PrismicPreview>] Failed to exit Preview Mode using the "${resolvedExitPreviewURL}" API endpoint. Does it exist?`,
);
}
};
if (router.isPreview) {
// Register Prismic Toolbar event handlers.
window.addEventListener(
"prismicPreviewUpdate",
handlePrismicPreviewUpdate,
);
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
} else {
const prismicPreviewCookie = getPrismicPreviewCookie(
globalThis.document.cookie,
);
if (prismicPreviewCookie) {
// If a Prismic preview cookie is present, but Next.js Preview
// Mode is not active, we must activate Preview Mode manually.
//
// This will happen when a visitor accesses the page using a
// Prismic preview share link.
/**
* Determines if the current location is a descendant of the app's base path.
*
* This is used to prevent infinite refrehes; when
* `isDescendantOfBasePath` is `false`, `router.isPreview` is also `false`.
*
* If the app does not have a base path, this should always be `true`.
*/
const locationIsDescendantOfBasePath = window.location.href.startsWith(
window.location.origin + router.basePath,
);
const prismicPreviewCookieRepositoryName =
getPreviewCookieRepositoryName(prismicPreviewCookie);
if (
locationIsDescendantOfBasePath &&
prismicPreviewCookieRepositoryName === repositoryName
) {
startPreviewMode();
}
}
}
// On cleanup, unregister Prismic Toolbar event handlers.
return () => {
window.removeEventListener(
"prismicPreviewUpdate",
handlePrismicPreviewUpdate,
);
window.removeEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
};
}, [
repositoryName,
resolvedExitPreviewURL,
resolvedUpdatePreviewURL,
router.isPreview,
router.basePath,
]);
return (
<>
{children}
<PrismicToolbar repositoryName={repositoryName} />
</>
);
}