@prismicio/next 0.1.0 → 0.2.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/LICENSE +202 -0
- package/README.md +4 -7
- package/dist/index.cjs +66 -71
- package/dist/index.d.ts +87 -83
- package/dist/index.js +153 -0
- package/package.json +36 -38
- package/src/PrismicPreview.tsx +112 -59
- package/src/enableAutoPreviews.ts +4 -6
- package/src/exitPreview.ts +15 -27
- package/src/index.ts +11 -4
- package/src/lib/extractPreviewRefRepositoryName.ts +60 -0
- package/src/lib/getCookie.ts +51 -0
- package/src/redirectToPreviewURL.ts +77 -19
- package/src/setPreviewData.ts +3 -11
- package/src/types.ts +1 -45
- package/dist/index.mjs +0 -156
package/src/PrismicPreview.tsx
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import * as prismic from "@prismicio/client";
|
|
2
3
|
import { PrismicToolbar } from "@prismicio/react";
|
|
3
4
|
import { useRouter } from "next/router";
|
|
4
5
|
|
|
5
|
-
import { getCookie } from "
|
|
6
|
-
import { extractPreviewRefRepositoryName } from "
|
|
6
|
+
import { getCookie } from "./lib/getCookie";
|
|
7
|
+
import { extractPreviewRefRepositoryName } from "./lib/extractPreviewRefRepositoryName";
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Props for `<PrismicPreview>`.
|
|
10
11
|
*/
|
|
11
|
-
type PrismicPreviewProps = {
|
|
12
|
+
export type PrismicPreviewProps = {
|
|
12
13
|
/**
|
|
13
14
|
* The name of your Prismic repository. A Prismic Toolbar will be registered
|
|
14
15
|
* using this repository.
|
|
@@ -18,31 +19,65 @@ type PrismicPreviewProps = {
|
|
|
18
19
|
/**
|
|
19
20
|
* The URL of your app's Prismic preview endpoint (default: `/api/preview`).
|
|
20
21
|
* This URL will be fetched on preview update events.
|
|
22
|
+
*
|
|
23
|
+
* **Note**: If your `next.config.js` file contains a `basePath`, it is
|
|
24
|
+
* automatically included.
|
|
21
25
|
*/
|
|
22
26
|
updatePreviewURL?: string;
|
|
23
27
|
|
|
24
28
|
/**
|
|
25
29
|
* The URL of your app's exit preview endpoint (default: `/api/exit-preview`).
|
|
26
30
|
* This URL will be fetched on preview exit events.
|
|
31
|
+
*
|
|
32
|
+
* **Note**: If your `next.config.js` file contains a `basePath`, it is
|
|
33
|
+
* automatically included.
|
|
27
34
|
*/
|
|
28
35
|
exitPreviewURL?: string;
|
|
29
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Children to render adjacent to the Prismic Toolbar. The Prismic Toolbar
|
|
39
|
+
* will be rendered last.
|
|
40
|
+
*/
|
|
30
41
|
children?: React.ReactNode;
|
|
31
42
|
};
|
|
32
43
|
|
|
33
44
|
/**
|
|
34
|
-
*
|
|
45
|
+
* Updates (or starts) Next.js Preview Mode using a given API endpoint and
|
|
46
|
+
* reloads the page.
|
|
35
47
|
*
|
|
36
|
-
* @param
|
|
48
|
+
* @param updatePreviewURL - The API endpoint that sets Preview Mode data.
|
|
49
|
+
*/
|
|
50
|
+
const updatePreviewMode = async (updatePreviewURL: string): Promise<void> => {
|
|
51
|
+
// Start Next.js Preview Mode via the given preview API endpoint.
|
|
52
|
+
const res = await globalThis.fetch(updatePreviewURL);
|
|
53
|
+
|
|
54
|
+
if (res.ok) {
|
|
55
|
+
// Reload the page with an active Preview Mode.
|
|
56
|
+
window.location.reload();
|
|
57
|
+
} else {
|
|
58
|
+
console.error(
|
|
59
|
+
`[<PrismicPreview>] Failed to start or update Preview Mode using the "${updatePreviewURL}" API endpoint. Does it exist?`,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Exits Next.js Preview Mode using a given API endpoint and reloads the page.
|
|
37
66
|
*
|
|
38
|
-
* @
|
|
67
|
+
* @param exitPreviewURL - The API endpoint that exits Preview Mode.
|
|
39
68
|
*/
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
69
|
+
const exitPreviewMode = async (exitPreviewURL: string): Promise<void> => {
|
|
70
|
+
// Exit Next.js Preview Mode via the given exit preview API endpoint.
|
|
71
|
+
const res = await globalThis.fetch(exitPreviewURL);
|
|
72
|
+
|
|
73
|
+
if (res.ok) {
|
|
74
|
+
// Reload the page with an inactive Preview Mode.
|
|
75
|
+
window.location.reload();
|
|
76
|
+
} else {
|
|
77
|
+
console.error(
|
|
78
|
+
`[<PrismicPreview>] Failed to exit Preview Mode using the "${exitPreviewURL}" API endpoint. Does it exist?`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
46
81
|
};
|
|
47
82
|
|
|
48
83
|
/**
|
|
@@ -56,78 +91,96 @@ const isPrismicUpdateToolbarEvent = (
|
|
|
56
91
|
*/
|
|
57
92
|
export function PrismicPreview({
|
|
58
93
|
repositoryName,
|
|
59
|
-
children,
|
|
60
94
|
updatePreviewURL = "/api/preview",
|
|
61
95
|
exitPreviewURL = "/api/exit-preview",
|
|
96
|
+
children,
|
|
62
97
|
}: PrismicPreviewProps): JSX.Element {
|
|
63
98
|
const router = useRouter();
|
|
64
99
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
100
|
+
const resolvedUpdatePreviewURL = router.basePath + updatePreviewURL;
|
|
101
|
+
const resolvedExitPreviewURL = router.basePath + exitPreviewURL;
|
|
102
|
+
|
|
103
|
+
React.useEffect(() => {
|
|
104
|
+
/**
|
|
105
|
+
* The Prismic preview cookie.
|
|
106
|
+
*/
|
|
107
|
+
const previewCookie = getCookie(prismic.cookie.preview);
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Determines if the current Prismic preview session is for the repository
|
|
111
|
+
* configured for this instance of `<PrismicPreview>`.
|
|
112
|
+
*/
|
|
113
|
+
const prismicPreviewSessionIsForThisRepo = Boolean(
|
|
114
|
+
previewCookie &&
|
|
115
|
+
extractPreviewRefRepositoryName(previewCookie) === repositoryName,
|
|
68
116
|
);
|
|
69
117
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
118
|
+
/**
|
|
119
|
+
* Determines if the current location is a descendant of the app's base path.
|
|
120
|
+
*
|
|
121
|
+
* This is used to prevent infinite refrehes; when `isDescendantOfBasePath`
|
|
122
|
+
* is `false`, `router.isPreview` is also `false`.
|
|
123
|
+
*
|
|
124
|
+
* If the app does not have a base path, this should always be `true`.
|
|
125
|
+
*/
|
|
126
|
+
const locationIsDescendantOfBasePath = window.location.href.startsWith(
|
|
127
|
+
window.location.origin + router.basePath,
|
|
128
|
+
);
|
|
76
129
|
|
|
77
|
-
|
|
130
|
+
/**
|
|
131
|
+
* Determines if the current user loaded the page from a share link.
|
|
132
|
+
*
|
|
133
|
+
* Currently, we can only deduce this by checking if router.isPreview is
|
|
134
|
+
* false (i.e. Preview Mode is inactive) and a Prismic preview cookie is present.
|
|
135
|
+
*/
|
|
136
|
+
const loadedFromShareLink = !router.isPreview && previewCookie;
|
|
137
|
+
|
|
138
|
+
if (
|
|
139
|
+
prismicPreviewSessionIsForThisRepo &&
|
|
140
|
+
locationIsDescendantOfBasePath &&
|
|
141
|
+
loadedFromShareLink
|
|
142
|
+
) {
|
|
143
|
+
updatePreviewMode(resolvedUpdatePreviewURL);
|
|
144
|
+
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
78
147
|
|
|
79
148
|
const handlePrismicPreviewUpdate = async (event: Event) => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
// Start Next.js Preview Mode via the given preview API endpoint.
|
|
85
|
-
await fetch(updatePreviewURL);
|
|
86
|
-
|
|
87
|
-
// Reload the page with an active Preview Mode.
|
|
88
|
-
window.location.reload();
|
|
89
|
-
}
|
|
149
|
+
// Prevent the toolbar from reloading the page.
|
|
150
|
+
event.preventDefault();
|
|
151
|
+
await updatePreviewMode(resolvedUpdatePreviewURL);
|
|
90
152
|
};
|
|
91
153
|
|
|
92
154
|
const handlePrismicPreviewEnd = async (event: Event) => {
|
|
93
155
|
// Prevent the toolbar from reloading the page.
|
|
94
156
|
event.preventDefault();
|
|
95
|
-
|
|
96
|
-
// Exit Next.js Preview Mode via the given preview API endpoint.
|
|
97
|
-
await fetch(exitPreviewURL);
|
|
98
|
-
|
|
99
|
-
// Reload the page with an active Preview Mode.
|
|
100
|
-
window.location.reload();
|
|
157
|
+
await exitPreviewMode(resolvedExitPreviewURL);
|
|
101
158
|
};
|
|
102
159
|
|
|
103
160
|
// Register Prismic Toolbar event handlers.
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
"prismicPreviewUpdate",
|
|
107
|
-
handlePrismicPreviewUpdate,
|
|
108
|
-
);
|
|
109
|
-
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
110
|
-
}
|
|
161
|
+
window.addEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
|
|
162
|
+
window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
111
163
|
|
|
112
164
|
// On cleanup, unregister Prismic Toolbar event handlers.
|
|
113
165
|
return () => {
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
window.removeEventListener(
|
|
120
|
-
"prismicPreviewEnd",
|
|
121
|
-
handlePrismicPreviewEnd,
|
|
122
|
-
);
|
|
123
|
-
}
|
|
166
|
+
window.removeEventListener(
|
|
167
|
+
"prismicPreviewUpdate",
|
|
168
|
+
handlePrismicPreviewUpdate,
|
|
169
|
+
);
|
|
170
|
+
window.removeEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
|
|
124
171
|
};
|
|
125
|
-
}, [
|
|
172
|
+
}, [
|
|
173
|
+
repositoryName,
|
|
174
|
+
resolvedUpdatePreviewURL,
|
|
175
|
+
resolvedExitPreviewURL,
|
|
176
|
+
router.isPreview,
|
|
177
|
+
router.basePath,
|
|
178
|
+
]);
|
|
126
179
|
|
|
127
180
|
return (
|
|
128
181
|
<>
|
|
129
|
-
<PrismicToolbar repositoryName={repositoryName} />
|
|
130
182
|
{children}
|
|
183
|
+
<PrismicToolbar repositoryName={repositoryName} />
|
|
131
184
|
</>
|
|
132
185
|
);
|
|
133
186
|
}
|
|
@@ -60,19 +60,17 @@ export type EnableAutoPreviewsConfig<
|
|
|
60
60
|
export const enableAutoPreviews = <TPreviewData extends PreviewData>(
|
|
61
61
|
config: EnableAutoPreviewsConfig<TPreviewData>,
|
|
62
62
|
): void => {
|
|
63
|
-
/**
|
|
64
|
-
* If preview data is being passed from Next Context then use queryContentFromRef
|
|
65
|
-
*/
|
|
66
63
|
if ("previewData" in config && config.previewData) {
|
|
64
|
+
// If preview data is being passed from Next Context then use queryContentFromRef
|
|
65
|
+
|
|
67
66
|
const { previewData } = config;
|
|
68
67
|
|
|
69
68
|
if (isPrismicNextPreviewData(previewData) && previewData.ref) {
|
|
70
69
|
config.client.queryContentFromRef(previewData.ref);
|
|
71
70
|
}
|
|
72
|
-
/**
|
|
73
|
-
* If the req object is passed then use enableAutoPreviewsFromReq
|
|
74
|
-
*/
|
|
75
71
|
} else if ("req" in config && config.req) {
|
|
72
|
+
// If the req object is passed then use enableAutoPreviewsFromReq
|
|
73
|
+
|
|
76
74
|
config.client.enableAutoPreviewsFromReq(config.req);
|
|
77
75
|
}
|
|
78
76
|
};
|
package/src/exitPreview.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { NextApiRequest, NextApiResponse } from "next";
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Configuration for `exitPreview`.
|
|
@@ -10,11 +10,17 @@ export type ExitPreviewConfig = {
|
|
|
10
10
|
*
|
|
11
11
|
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
12
12
|
*/
|
|
13
|
-
req
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
// `req` is no longer used in `exitPreview()`. It previously would
|
|
14
|
+
// redirect the user to the referring URL, but it no longer has that
|
|
15
|
+
// behavior.
|
|
16
|
+
//
|
|
17
|
+
// `req` is retained as a parameter to make setting up an
|
|
18
|
+
// exit preview API route easier (this eliminates the awkward need to
|
|
19
|
+
// handle an unused `req` param).
|
|
20
|
+
//
|
|
21
|
+
// It is also retained in case it is needed in the future, such as
|
|
22
|
+
// reading headers or metadata about the request.
|
|
23
|
+
req: Pick<NextApiRequest, "headers">;
|
|
18
24
|
|
|
19
25
|
/**
|
|
20
26
|
* The `res` object from a Next.js API route. This is given as a parameter to
|
|
@@ -22,33 +28,15 @@ export type ExitPreviewConfig = {
|
|
|
22
28
|
*
|
|
23
29
|
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
24
30
|
*/
|
|
25
|
-
res:
|
|
26
|
-
clearPreviewData: NextApiResponse["clearPreviewData"];
|
|
27
|
-
redirect: NextApiResponse["redirect"];
|
|
28
|
-
};
|
|
31
|
+
res: Pick<NextApiResponse, "clearPreviewData" | "json">;
|
|
29
32
|
};
|
|
30
33
|
|
|
31
34
|
/**
|
|
32
35
|
* Exits Next.js's Preview Mode from within a Next.js API route.
|
|
33
|
-
*
|
|
34
|
-
* If the user was sent to the endpoint from a page, the user will be redirected
|
|
35
|
-
* back to that page after exiting Preview Mode.
|
|
36
36
|
*/
|
|
37
37
|
export function exitPreview(config: ExitPreviewConfig): void {
|
|
38
|
-
|
|
39
|
-
// Exit the current user from "Preview Mode". This function accepts no args.
|
|
38
|
+
// Exit the current user from Preview Mode.
|
|
40
39
|
config.res.clearPreviewData();
|
|
41
40
|
|
|
42
|
-
|
|
43
|
-
const url = new URL(req.headers.referer);
|
|
44
|
-
|
|
45
|
-
if (url.pathname !== "/api/exit-preview") {
|
|
46
|
-
// Redirect the user to the referrer page.
|
|
47
|
-
config.res.redirect(req.headers.referer);
|
|
48
|
-
|
|
49
|
-
return;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
config.res.redirect("/");
|
|
41
|
+
config.res.json({ success: true });
|
|
54
42
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
-
export type { SetPreviewDataConfig } from "./setPreviewData";
|
|
2
|
-
export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews";
|
|
3
|
-
export type { ExitPreviewConfig as ExitPreviewParams } from "./exitPreview";
|
|
4
1
|
export { setPreviewData } from "./setPreviewData";
|
|
2
|
+
export type { SetPreviewDataConfig } from "./setPreviewData";
|
|
3
|
+
|
|
5
4
|
export { exitPreview } from "./exitPreview";
|
|
5
|
+
export type { ExitPreviewConfig } from "./exitPreview";
|
|
6
|
+
|
|
6
7
|
export { PrismicPreview } from "./PrismicPreview";
|
|
8
|
+
export type { PrismicPreviewProps } from "./PrismicPreview";
|
|
9
|
+
|
|
7
10
|
export { enableAutoPreviews } from "./enableAutoPreviews";
|
|
11
|
+
export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews";
|
|
12
|
+
|
|
8
13
|
export { redirectToPreviewURL } from "./redirectToPreviewURL";
|
|
9
|
-
export type {
|
|
14
|
+
export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL";
|
|
15
|
+
|
|
16
|
+
export type { CreateClientConfig } from "./types";
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns the repository name from an object-style Prismic ref.
|
|
3
|
+
*
|
|
4
|
+
* @param host - Host string
|
|
5
|
+
*/
|
|
6
|
+
const extractFirstSubdomain = (host: string): string => host.split(".")[0];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Parses ref object from cookie and returns proper preview link
|
|
10
|
+
*
|
|
11
|
+
* @param previewRef - Prismic Preview reference
|
|
12
|
+
*/
|
|
13
|
+
const extractRepositoryNameFromObjectRef = (
|
|
14
|
+
previewRef: string,
|
|
15
|
+
): string | undefined => {
|
|
16
|
+
try {
|
|
17
|
+
const parsed = JSON.parse(decodeURIComponent(previewRef));
|
|
18
|
+
const keys = Object.keys(parsed);
|
|
19
|
+
const domainKey = keys.find((key) => /\.prismic\.io$/.test(key));
|
|
20
|
+
|
|
21
|
+
if (domainKey) {
|
|
22
|
+
return extractFirstSubdomain(domainKey);
|
|
23
|
+
} else {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
} catch {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns the repository name from a URL-style Prismic ref.
|
|
33
|
+
*
|
|
34
|
+
* @param previewRef - Preview ref from getCookie()
|
|
35
|
+
*/
|
|
36
|
+
const extractRepositoryNameFromURLRef = (
|
|
37
|
+
previewRef: string,
|
|
38
|
+
): string | undefined => {
|
|
39
|
+
try {
|
|
40
|
+
const url = new URL(previewRef);
|
|
41
|
+
|
|
42
|
+
return extractFirstSubdomain(url.host);
|
|
43
|
+
} catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Extracts preview reference repo name from stringified Prismic preview cookie
|
|
50
|
+
*
|
|
51
|
+
* @param previewRef - Preview Reference
|
|
52
|
+
*/
|
|
53
|
+
export const extractPreviewRefRepositoryName = (
|
|
54
|
+
previewRef: string,
|
|
55
|
+
): string | undefined => {
|
|
56
|
+
return (
|
|
57
|
+
extractRepositoryNameFromObjectRef(previewRef) ||
|
|
58
|
+
extractRepositoryNameFromURLRef(previewRef)
|
|
59
|
+
);
|
|
60
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The following code is a modifed version of `es-cookie` taken from
|
|
3
|
+
* https://github.com/theodorejb/es-cookie
|
|
4
|
+
*
|
|
5
|
+
* It only contains a simplified version of `get()`.
|
|
6
|
+
*
|
|
7
|
+
* Copyright 2017 Theodore Brown
|
|
8
|
+
*
|
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
* in the Software without restriction, including without limitation the rights
|
|
12
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
* furnished to do so, subject to the following conditions:
|
|
15
|
+
*
|
|
16
|
+
* The above copyright notice and this permission notice shall be included in
|
|
17
|
+
* all copies or substantial portions of the Software.
|
|
18
|
+
*
|
|
19
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
* SOFTWARE.*
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
const readValue = (value: string): string => {
|
|
29
|
+
return value.replace(/%3B/g, ";");
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Returns the value of a cookie from a given cookie store.
|
|
34
|
+
*
|
|
35
|
+
* @param name - Of the cookie.
|
|
36
|
+
*
|
|
37
|
+
* @returns The value of the cookie, if it exists.
|
|
38
|
+
*/
|
|
39
|
+
export const getCookie = (name: string): string | undefined => {
|
|
40
|
+
const cookies = document.cookie.split("; ");
|
|
41
|
+
|
|
42
|
+
for (const cookie of cookies) {
|
|
43
|
+
const parts = cookie.split("=");
|
|
44
|
+
const value = parts.slice(1).join("=");
|
|
45
|
+
const thisName = readValue(parts[0]).replace(/%3D/g, "=");
|
|
46
|
+
|
|
47
|
+
if (thisName === name) {
|
|
48
|
+
return readValue(value);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import type { Client } from "@prismicio/client";
|
|
2
|
+
import type { NextApiRequest, NextApiResponse } from "next";
|
|
3
|
+
import type { LinkResolverFunction } from "@prismicio/helpers";
|
|
3
4
|
|
|
4
5
|
type PrismicNextQuery = {
|
|
5
6
|
documentId: string;
|
|
@@ -16,33 +17,90 @@ type PrismicNextQuery = {
|
|
|
16
17
|
*/
|
|
17
18
|
const isPrismicNextQuery = (
|
|
18
19
|
query: NextApiRequest["query"],
|
|
19
|
-
): query is PrismicNextQuery =>
|
|
20
|
-
|
|
20
|
+
): query is PrismicNextQuery => {
|
|
21
|
+
return (
|
|
22
|
+
typeof query.documentId === "string" && typeof query.token === "string"
|
|
23
|
+
);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Preview config for enabling previews with redirectToPreviewURL
|
|
28
|
+
*/
|
|
29
|
+
export type RedirectToPreviewURLConfig<
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
31
|
+
TLinkResolverFunction extends LinkResolverFunction<any> = LinkResolverFunction,
|
|
32
|
+
> = {
|
|
33
|
+
/**
|
|
34
|
+
* The `req` object from a Next.js API route. This is given as a parameter to
|
|
35
|
+
* the API route.
|
|
36
|
+
*
|
|
37
|
+
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
38
|
+
*/
|
|
39
|
+
req: Pick<NextApiRequest, "query">;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The `res` object from a Next.js API route. This is given as a parameter to
|
|
43
|
+
* the API route.
|
|
44
|
+
*
|
|
45
|
+
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
46
|
+
*/
|
|
47
|
+
res: Pick<NextApiResponse, "redirect">;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The Prismic client configured for the preview session's repository.
|
|
51
|
+
*/
|
|
52
|
+
client: Client;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A Link Resolver used to resolve the previewed document's URL.
|
|
56
|
+
*
|
|
57
|
+
* @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
|
|
58
|
+
*/
|
|
59
|
+
linkResolver?: TLinkResolverFunction;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* The default redirect URL if a URL cannot be determined for the previewed document.
|
|
63
|
+
*
|
|
64
|
+
* **Note**: If your `next.config.js` file contains a `basePath`, you must
|
|
65
|
+
* include the `basePath` as part of this option. `redirectToPreviewURL()`
|
|
66
|
+
* cannot read your global `basePath`.
|
|
67
|
+
*/
|
|
68
|
+
defaultURL?: string;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The `basePath` for the Next.js app as it is defined in `next.config.js`.
|
|
72
|
+
* This option can be omitted if the app does not have a `basePath`.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* The API route is unable to detect the app's `basePath` automatically. It
|
|
76
|
+
* must be provided to `redirectToPreviewURL()` manually.
|
|
77
|
+
*/
|
|
78
|
+
basePath?: string;
|
|
79
|
+
};
|
|
21
80
|
|
|
22
81
|
/**
|
|
23
82
|
* Redirects a user to the URL of a previewed Prismic document from within a
|
|
24
83
|
* Next.js API route.
|
|
25
84
|
*/
|
|
26
|
-
export async function redirectToPreviewURL
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (isPrismicNextQuery(req.query)) {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
linkResolver,
|
|
85
|
+
export async function redirectToPreviewURL<
|
|
86
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
87
|
+
TLinkResolverFunction extends LinkResolverFunction<any>,
|
|
88
|
+
>(config: RedirectToPreviewURLConfig<TLinkResolverFunction>): Promise<void> {
|
|
89
|
+
const defaultURL = config.defaultURL || "/";
|
|
90
|
+
const basePath = config.basePath || "";
|
|
91
|
+
|
|
92
|
+
if (isPrismicNextQuery(config.req.query)) {
|
|
93
|
+
const previewUrl = await config.client.resolvePreviewURL({
|
|
94
|
+
linkResolver: config.linkResolver,
|
|
37
95
|
defaultURL,
|
|
38
|
-
documentID: documentId,
|
|
39
|
-
previewToken: token,
|
|
96
|
+
documentID: config.req.query.documentId,
|
|
97
|
+
previewToken: config.req.query.token,
|
|
40
98
|
});
|
|
41
99
|
|
|
42
|
-
res.redirect(previewUrl);
|
|
100
|
+
config.res.redirect(basePath + previewUrl);
|
|
43
101
|
|
|
44
102
|
return;
|
|
45
103
|
}
|
|
46
104
|
|
|
47
|
-
res.redirect(defaultURL);
|
|
105
|
+
config.res.redirect(basePath + defaultURL);
|
|
48
106
|
}
|
package/src/setPreviewData.ts
CHANGED
|
@@ -11,10 +11,7 @@ export type SetPreviewDataConfig = {
|
|
|
11
11
|
*
|
|
12
12
|
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
13
13
|
*/
|
|
14
|
-
req:
|
|
15
|
-
query: NextApiRequest["query"];
|
|
16
|
-
cookies: NextApiRequest["cookies"];
|
|
17
|
-
};
|
|
14
|
+
req: Pick<NextApiRequest, "query" | "cookies">;
|
|
18
15
|
|
|
19
16
|
/**
|
|
20
17
|
* The `res` object from a Next.js API route. This is given as a parameter to
|
|
@@ -22,18 +19,13 @@ export type SetPreviewDataConfig = {
|
|
|
22
19
|
*
|
|
23
20
|
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
24
21
|
*/
|
|
25
|
-
res:
|
|
26
|
-
setPreviewData: NextApiResponse["setPreviewData"];
|
|
27
|
-
};
|
|
22
|
+
res: Pick<NextApiResponse, "setPreviewData">;
|
|
28
23
|
};
|
|
29
24
|
|
|
30
25
|
/**
|
|
31
26
|
* Set Prismic preview data for Next.js's Preview Mode.
|
|
32
27
|
*/
|
|
33
|
-
export
|
|
34
|
-
req,
|
|
35
|
-
res,
|
|
36
|
-
}: SetPreviewDataConfig): Promise<void> {
|
|
28
|
+
export function setPreviewData({ req, res }: SetPreviewDataConfig): void {
|
|
37
29
|
const ref = req.query.token || req.cookies[prismic.cookie.preview];
|
|
38
30
|
|
|
39
31
|
if (ref) {
|
package/src/types.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { PreviewData, NextApiRequest
|
|
2
|
-
import { LinkResolverFunction } from "@prismicio/helpers";
|
|
3
|
-
import { Client } from "@prismicio/client";
|
|
1
|
+
import { PreviewData, NextApiRequest } from "next";
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
4
|
* Configuration for creating a Prismic client with automatic preview support in
|
|
@@ -22,45 +20,3 @@ export type CreateClientConfig = {
|
|
|
22
20
|
*/
|
|
23
21
|
req?: NextApiRequest;
|
|
24
22
|
};
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Preview config for enabling previews with redirectToPreviewURL
|
|
28
|
-
*/
|
|
29
|
-
export type PreviewConfig = {
|
|
30
|
-
/**
|
|
31
|
-
* The `req` object from a Next.js API route. This is given as a parameter to
|
|
32
|
-
* the API route.
|
|
33
|
-
*
|
|
34
|
-
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
35
|
-
*/
|
|
36
|
-
req: {
|
|
37
|
-
query: NextApiRequest["query"];
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* The `res` object from a Next.js API route. This is given as a parameter to
|
|
42
|
-
* the API route.
|
|
43
|
-
*
|
|
44
|
-
* @see Next.js API route docs: {@link https://nextjs.org/docs/api-routes/introduction}
|
|
45
|
-
*/
|
|
46
|
-
res: {
|
|
47
|
-
redirect: NextApiResponse["redirect"];
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* The Prismic client configured for the preview session's repository.
|
|
52
|
-
*/
|
|
53
|
-
client: Client;
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* A Link Resolver used to resolve the previewed document's URL.
|
|
57
|
-
*
|
|
58
|
-
* @see To learn more about Link Resolver: {@link https://prismic.io/docs/core-concepts/link-resolver-route-resolver}
|
|
59
|
-
*/
|
|
60
|
-
linkResolver?: LinkResolverFunction;
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* The default redirect URL if a URL cannot be determined for the previewed document.
|
|
64
|
-
*/
|
|
65
|
-
defaultURL?: string;
|
|
66
|
-
};
|