@prismicio/next 1.7.1 → 2.0.0-alpha.1
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 -19
- package/dist/PrismicPreview.js.map +1 -1
- package/dist/PrismicPreviewClient.d.ts +5 -3
- package/dist/PrismicPreviewClient.js +58 -68
- 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 +4 -46
- 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 +20 -50
- package/src/PrismicPreviewClient.tsx +112 -138
- package/src/createLocaleRedirect.ts +14 -56
- package/src/enableAutoPreviews.ts +38 -110
- package/src/exitPreview.ts +18 -81
- 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 -28
- 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
|
@@ -1,156 +1,130 @@
|
|
|
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
|
-
let isAppRouter = true;
|
|
24
|
-
let basePath = "";
|
|
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;
|
|
43
21
|
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Starts Preview Mode and refreshes the page's props.
|
|
47
|
-
*/
|
|
48
|
-
const startPreviewMode = async () => {
|
|
49
|
-
const resolvedUpdatePreviewURL = basePath + updatePreviewURL;
|
|
50
|
-
|
|
51
|
-
// Start Next.js Preview Mode via the given preview API endpoint.
|
|
52
|
-
const res = await globalThis.fetch(resolvedUpdatePreviewURL);
|
|
53
|
-
|
|
54
|
-
// We check for `res.redirected` rather than `res.ok`
|
|
55
|
-
// since the update preview endpoint may redirect to a
|
|
56
|
-
// 404 page. As long as it redirects, we know the
|
|
57
|
-
// endpoint exists and at least attempted to set
|
|
58
|
-
// preview data.
|
|
59
|
-
if (res.redirected) {
|
|
60
|
-
refresh();
|
|
61
|
-
} else {
|
|
62
|
-
console.error(
|
|
63
|
-
`[<PrismicPreview>] Failed to start or update Preview Mode using the "${resolvedUpdatePreviewURL}" API endpoint. Does it exist?`,
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const handlePrismicPreviewUpdate = async (event: Event) => {
|
|
69
|
-
// Prevent the toolbar from reloading the page.
|
|
70
|
-
event.preventDefault();
|
|
22
|
+
const { refresh } = useRouter();
|
|
71
23
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
};
|
|
24
|
+
useEffect(() => {
|
|
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
|
+
}
|
|
78
70
|
|
|
79
|
-
|
|
80
|
-
// Prevent the toolbar from reloading the page.
|
|
71
|
+
function onUpdate(event: Event) {
|
|
81
72
|
event.preventDefault();
|
|
73
|
+
refresh();
|
|
74
|
+
}
|
|
82
75
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
}
|
|
76
|
+
function onEnd(event: Event) {
|
|
77
|
+
event.preventDefault();
|
|
78
|
+
globalThis
|
|
79
|
+
.fetch(exitPreviewURL, { signal: controller.signal })
|
|
80
|
+
.then((res) => {
|
|
81
|
+
if (!res.ok) {
|
|
82
|
+
console.error(
|
|
83
|
+
`[<PrismicPreview>] Failed to exit Preview Mode using the "${exitPreviewURL}" API endpoint. Does it exist?`,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
refresh();
|
|
90
|
+
})
|
|
91
|
+
.catch(() => {
|
|
92
|
+
// noop
|
|
93
|
+
});
|
|
136
94
|
}
|
|
137
95
|
|
|
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
|
-
]);
|
|
96
|
+
return () => controller.abort();
|
|
97
|
+
}, [repositoryName, isDraftMode, updatePreviewURL, exitPreviewURL, refresh]);
|
|
154
98
|
|
|
155
99
|
return null;
|
|
156
100
|
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Returns the value of a cookie from a given cookie store.
|
|
104
|
+
*
|
|
105
|
+
* @param cookieJar - The stringified cookie store from which to read the
|
|
106
|
+
* cookie.
|
|
107
|
+
*
|
|
108
|
+
* @returns The value of the cookie, if it exists.
|
|
109
|
+
*/
|
|
110
|
+
function getPrismicPreviewCookie(cookieJar: string): string | undefined {
|
|
111
|
+
function readValue(value: string): string {
|
|
112
|
+
return value.replace(/%3B/g, ";");
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const cookies = cookieJar.split("; ");
|
|
116
|
+
|
|
117
|
+
let value: string | undefined;
|
|
118
|
+
|
|
119
|
+
for (const cookie of cookies) {
|
|
120
|
+
const parts = cookie.split("=");
|
|
121
|
+
const name = readValue(parts[0]).replace(/%3D/g, "=");
|
|
122
|
+
|
|
123
|
+
if (name === prismicCookie.preview) {
|
|
124
|
+
value = readValue(parts.slice(1).join("="));
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return value;
|
|
130
|
+
}
|
|
@@ -1,60 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { match } from "@formatjs/intl-localematcher";
|
|
3
|
-
import * as prismic from "@prismicio/client";
|
|
4
|
-
|
|
5
|
-
import { NextRequestLike } from "./types";
|
|
6
|
-
|
|
7
|
-
export type CreateLocaleRedirectConfig = {
|
|
8
|
-
request: NextRequestLike;
|
|
9
|
-
client: Pick<prismic.Client, "getRepository">;
|
|
10
|
-
localeOverrides?: Record<string, string>;
|
|
11
|
-
omitDefaultLocale?: boolean;
|
|
12
|
-
};
|
|
1
|
+
import { devMsg } from "./lib/devMsg.js";
|
|
13
2
|
|
|
14
3
|
/**
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* locale.
|
|
18
|
-
*
|
|
19
|
-
* @returns A `Response` if the request should be redirected, `undefined`
|
|
20
|
-
* otherwise.
|
|
4
|
+
* @deprecated `createLocaleRedirect()` has been removed due to performance
|
|
5
|
+
* issues.
|
|
21
6
|
*/
|
|
22
|
-
export
|
|
23
|
-
|
|
24
|
-
)
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const locales = repository.languages.map((language) => {
|
|
28
|
-
return config.localeOverrides?.[language.id] ?? language.id;
|
|
29
|
-
});
|
|
30
|
-
const defaultLocale = locales[0];
|
|
31
|
-
|
|
32
|
-
const { pathname } = config.request.nextUrl;
|
|
33
|
-
const pathnameHasLocale = locales.some(
|
|
34
|
-
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,
|
|
7
|
+
export function createLocaleRedirect() {
|
|
8
|
+
throw new Error(
|
|
9
|
+
`createLocaleRedirect() has been removed due to performance issues. See ${devMsg(
|
|
10
|
+
"replace-createLocaleRedirect",
|
|
11
|
+
)} for more details.`,
|
|
35
12
|
);
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const headers = {
|
|
44
|
-
"accept-language":
|
|
45
|
-
config.request.headers.get("accept-language") ?? undefined,
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
if (headers["accept-language"]) {
|
|
49
|
-
const languages = new Negotiator({ headers }).languages();
|
|
50
|
-
locale = match(languages, locales, defaultLocale);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (locale === defaultLocale && config.omitDefaultLocale) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
config.request.nextUrl.pathname = `/${locale}${pathname}`;
|
|
58
|
-
|
|
59
|
-
return Response.redirect(config.request.nextUrl as URL);
|
|
60
|
-
};
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated `createLocaleRedirect()` has been removed due to performance
|
|
16
|
+
* issues.
|
|
17
|
+
*/
|
|
18
|
+
export type CreateLocaleRedirectConfig = never;
|
|
@@ -1,125 +1,53 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { PreviewData } from "next";
|
|
3
|
-
import * as prismic from "@prismicio/client";
|
|
1
|
+
import { type Client, cookie as prismicCookie } from "@prismicio/client";
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Configuration for `enableAutoPreviews`.
|
|
9
|
-
*
|
|
10
|
-
* @typeParam TPreviewData - Next.js preview data object.
|
|
11
|
-
*/
|
|
12
|
-
export type EnableAutoPreviewsConfig<
|
|
13
|
-
TPreviewData extends PreviewData = PreviewData,
|
|
14
|
-
> = {
|
|
15
|
-
/**
|
|
16
|
-
* Prismic client with which automatic previews will be enabled.
|
|
17
|
-
*/
|
|
3
|
+
/** Configuration for `enableAutoPreviews`. */
|
|
4
|
+
export type EnableAutoPreviewsConfig = {
|
|
5
|
+
/** Prismic client with which automatic previews will be enabled. */
|
|
18
6
|
// `Pick` is used to use the smallest possible subset of
|
|
19
7
|
// `prismic.Client`. Doing this reduces the surface area for breaking
|
|
20
8
|
// type changes.
|
|
21
|
-
client: Pick<
|
|
22
|
-
prismic.Client,
|
|
23
|
-
"queryContentFromRef" | "enableAutoPreviewsFromReq"
|
|
24
|
-
>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* **Only used in the Pages Directory (/pages).**
|
|
28
|
-
*
|
|
29
|
-
* The `previewData` object provided in the `getStaticProps()` or
|
|
30
|
-
* `getServerSideProps()` context object.
|
|
31
|
-
*/
|
|
32
|
-
previewData?: TPreviewData;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* **Only used in the Pages Directory (/pages).**
|
|
36
|
-
*
|
|
37
|
-
* The `req` object from a Next.js API route.
|
|
38
|
-
*
|
|
39
|
-
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
40
|
-
*/
|
|
41
|
-
req?: NextApiRequestLike;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const isPrismicPreviewData = (input: unknown): input is PrismicPreviewData => {
|
|
45
|
-
return typeof input === "object" && input !== null && "ref" in input;
|
|
9
|
+
client: Pick<Client, "queryContentFromRef" | "enableAutoPreviewsFromReq">;
|
|
46
10
|
};
|
|
47
11
|
|
|
48
12
|
/**
|
|
49
13
|
* Configures a Prismic client to automatically query draft content during a
|
|
50
|
-
* preview session.
|
|
51
|
-
* or a Next.js API endpoint request object.
|
|
14
|
+
* preview session.
|
|
52
15
|
*
|
|
53
16
|
* @param config - Configuration for the function.
|
|
54
17
|
*/
|
|
55
|
-
export
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
//
|
|
60
|
-
//
|
|
61
|
-
// directory
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
18
|
+
export function enableAutoPreviews(config: EnableAutoPreviewsConfig): void {
|
|
19
|
+
// We use a function value so the cookie is checked on every
|
|
20
|
+
// request. We don't have a static value to read from.
|
|
21
|
+
config.client.queryContentFromRef(async () => {
|
|
22
|
+
// Need this to avoid the following Next.js build-time error:
|
|
23
|
+
// You're importing a component that needs next/headers. That only works
|
|
24
|
+
// in a Server Component which is not supported in the pages/ directory.
|
|
25
|
+
const { cookies, draftMode } = await import("next/headers");
|
|
26
|
+
|
|
27
|
+
let isDraftModeEnabled = false;
|
|
28
|
+
try {
|
|
29
|
+
isDraftModeEnabled = (await draftMode()).isEnabled;
|
|
30
|
+
} catch {
|
|
31
|
+
// `draftMode()` may have been called in a palce that
|
|
32
|
+
// does not have access to its async storage. This
|
|
33
|
+
// occurs in places like `generateStaticParams()`. We
|
|
34
|
+
// can ignore this case.
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (!isDraftModeEnabled) {
|
|
38
|
+
return;
|
|
65
39
|
}
|
|
66
|
-
} else if ("req" in config && config.req) {
|
|
67
|
-
// Assume we are in an API Route (`pages` directory).
|
|
68
|
-
|
|
69
|
-
config.client.enableAutoPreviewsFromReq(config.req);
|
|
70
|
-
} else {
|
|
71
|
-
// Assume we are in App Router (`app` directory) OR
|
|
72
|
-
// `getStaticProps()`/`getServerSideProps()` with an inactive
|
|
73
|
-
// Preview Mode (`pages` directory).
|
|
74
|
-
|
|
75
|
-
// We use a function value so the cookie is checked on every
|
|
76
|
-
// request. We don't have a static value to read from.
|
|
77
|
-
config.client.queryContentFromRef(async () => {
|
|
78
|
-
let isDraftModeEnabled = false;
|
|
79
|
-
try {
|
|
80
|
-
isDraftModeEnabled = (await draftMode()).isEnabled;
|
|
81
|
-
} catch {
|
|
82
|
-
// This catch block may be reached if
|
|
83
|
-
// `draftMode()` is called in a place that does
|
|
84
|
-
// not have access to its async storage. We can
|
|
85
|
-
// ignore this case.
|
|
86
|
-
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
if (!isDraftModeEnabled) {
|
|
91
|
-
return;
|
|
92
|
-
}
|
|
93
40
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
// We are probably in `getStaticProps()` or
|
|
99
|
-
// `getServerSideProps()` with inactive Preview
|
|
100
|
-
// Mode where `cookies()` does not work. We
|
|
101
|
-
// don't need to do any preview handling.
|
|
41
|
+
const cookie = (await cookies()).get(prismicCookie.preview)?.value;
|
|
42
|
+
if (!cookie) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
102
45
|
|
|
103
|
-
|
|
104
|
-
|
|
46
|
+
const isActiveCookie = cookie.includes("websitePreviewId=");
|
|
47
|
+
if (!isActiveCookie) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
105
50
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// {
|
|
110
|
-
// "_tracker": "abc123"
|
|
111
|
-
// }
|
|
112
|
-
//
|
|
113
|
-
// An active cookie looks like this (URL encoded):
|
|
114
|
-
// {
|
|
115
|
-
// "_tracker": "abc123",
|
|
116
|
-
// "example-prismic-repo.prismic.io": {
|
|
117
|
-
// preview: "https://example-prismic-repo.prismic.io/previews/abc:123?websitePreviewId=xyz"
|
|
118
|
-
// }
|
|
119
|
-
// }
|
|
120
|
-
if (cookie && /\.prismic\.io/.test(cookie)) {
|
|
121
|
-
return cookie;
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
};
|
|
51
|
+
return cookie;
|
|
52
|
+
});
|
|
53
|
+
}
|
package/src/exitPreview.ts
CHANGED
|
@@ -1,94 +1,31 @@
|
|
|
1
|
-
import { draftMode } from "next/headers";
|
|
2
|
-
|
|
3
|
-
import { NextApiRequestLike, NextApiResponseLike } from "./types";
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @deprecated Use `ExitPreviewAPIRouteConfig` instead when `exitPreview()` is
|
|
7
|
-
* used in a Pages Router API endpoint. `exitPreview()` does not require any
|
|
8
|
-
* configuration when used in an App Router Route Handler.
|
|
9
|
-
*/
|
|
10
|
-
export type ExitPreviewConfig = ExitPreviewAPIRouteConfig;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Configuration for `exitPreview()` when used in a Pages Router API route.
|
|
14
|
-
*/
|
|
15
|
-
export type ExitPreviewAPIRouteConfig = {
|
|
16
|
-
/**
|
|
17
|
-
* **Only use this parameter in the Pages Directory (/pages).**
|
|
18
|
-
*
|
|
19
|
-
* The `req` object from a Next.js API route.
|
|
20
|
-
*
|
|
21
|
-
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
22
|
-
*/
|
|
23
|
-
req?: NextApiRequestLike;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* **Only use this parameter in the Pages Directory (/pages).**
|
|
27
|
-
*
|
|
28
|
-
* The `res` object from a Next.js API route.
|
|
29
|
-
*
|
|
30
|
-
* @see Next.js API route docs: \<https://nextjs.org/docs/api-routes/introduction\>
|
|
31
|
-
*/
|
|
32
|
-
res?: NextApiResponseLike;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
1
|
/**
|
|
36
2
|
* Ends a Prismic preview session within a Next.js app. This function should be
|
|
37
|
-
* used in a Router Handler
|
|
38
|
-
* the App Router or Pages Router.
|
|
3
|
+
* used in a Router Handler.
|
|
39
4
|
*
|
|
40
|
-
* @example
|
|
5
|
+
* @example
|
|
41
6
|
*
|
|
42
7
|
* ```typescript
|
|
43
8
|
* // src/app/api/exit-preview/route.js
|
|
44
9
|
*
|
|
45
10
|
* import { exitPreview } from "@prismicio/next";
|
|
46
11
|
*
|
|
47
|
-
* export function GET() {
|
|
48
|
-
* return exitPreview();
|
|
49
|
-
* }
|
|
50
|
-
* ```
|
|
51
|
-
*
|
|
52
|
-
* @example Usage within a Pages Router API Route.
|
|
53
|
-
*
|
|
54
|
-
* ```typescript
|
|
55
|
-
* // src/pages/api/exit-preview.js
|
|
56
|
-
*
|
|
57
|
-
* import { exitPreview } from "@prismicio/next";
|
|
58
|
-
*
|
|
59
|
-
* export default function handler(req, res) {
|
|
60
|
-
* exitPreview({ req, res });
|
|
12
|
+
* export async function GET() {
|
|
13
|
+
* return await exitPreview();
|
|
61
14
|
* }
|
|
62
15
|
* ```
|
|
63
16
|
*/
|
|
64
|
-
export async function exitPreview(): Promise<Response
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
)
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
config.res.json({ success: true });
|
|
80
|
-
|
|
81
|
-
return;
|
|
82
|
-
} else {
|
|
83
|
-
// Assume Draft Mode is being used.
|
|
84
|
-
|
|
85
|
-
(await draftMode()).disable();
|
|
86
|
-
|
|
87
|
-
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
88
|
-
return new Response(JSON.stringify({ success: true }), {
|
|
89
|
-
headers: {
|
|
90
|
-
"Cache-Control": "no-store",
|
|
91
|
-
},
|
|
92
|
-
});
|
|
93
|
-
}
|
|
17
|
+
export async function exitPreview(): Promise<Response> {
|
|
18
|
+
// Need this to avoid the following Next.js build-time error:
|
|
19
|
+
// You're importing a component that needs next/headers. That only works
|
|
20
|
+
// in a Server Component which is not supported in the pages/ directory.
|
|
21
|
+
const { draftMode } = await import("next/headers");
|
|
22
|
+
|
|
23
|
+
(await draftMode()).disable();
|
|
24
|
+
|
|
25
|
+
// `Cache-Control` header is used to prevent CDN-level caching.
|
|
26
|
+
return new Response(JSON.stringify({ success: true }), {
|
|
27
|
+
headers: {
|
|
28
|
+
"Cache-Control": "no-store",
|
|
29
|
+
},
|
|
30
|
+
});
|
|
94
31
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,34 +1,23 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export type { SetPreviewDataConfig } from "./setPreviewData";
|
|
1
|
+
export { exitPreview } from "./exitPreview.js";
|
|
3
2
|
|
|
4
|
-
export {
|
|
5
|
-
export type {
|
|
6
|
-
ExitPreviewConfig,
|
|
7
|
-
ExitPreviewAPIRouteConfig,
|
|
8
|
-
} from "./exitPreview";
|
|
3
|
+
export { PrismicPreview } from "./PrismicPreview.js";
|
|
4
|
+
export type { PrismicPreviewProps } from "./PrismicPreview.js";
|
|
9
5
|
|
|
10
|
-
export {
|
|
11
|
-
export type {
|
|
6
|
+
export { PrismicNextLink } from "./PrismicNextLink.js";
|
|
7
|
+
export type { PrismicNextLinkProps } from "./PrismicNextLink.js";
|
|
12
8
|
|
|
13
|
-
export {
|
|
14
|
-
export type {
|
|
9
|
+
export { enableAutoPreviews } from "./enableAutoPreviews.js";
|
|
10
|
+
export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews.js";
|
|
15
11
|
|
|
16
|
-
export {
|
|
17
|
-
export type {
|
|
12
|
+
export { redirectToPreviewURL } from "./redirectToPreviewURL.js";
|
|
13
|
+
export type { RedirectToPreviewURLConfig } from "./redirectToPreviewURL.js";
|
|
18
14
|
|
|
19
|
-
export {
|
|
20
|
-
export type {
|
|
21
|
-
RedirectToPreviewURLConfig,
|
|
22
|
-
RedirectToPreviewURLRouteHandlerConfig,
|
|
23
|
-
RedirectToPreviewURLAPIEndpointConfig,
|
|
24
|
-
} from "./redirectToPreviewURL";
|
|
15
|
+
export { PrismicNextImage } from "./PrismicNextImage.js";
|
|
16
|
+
export type { PrismicNextImageProps } from "./PrismicNextImage.js";
|
|
25
17
|
|
|
26
|
-
export {
|
|
27
|
-
export type { CreateLocaleRedirectConfig } from "./createLocaleRedirect";
|
|
18
|
+
export { imgixLoader } from "./imgixLoader.js";
|
|
28
19
|
|
|
29
|
-
export {
|
|
30
|
-
export type { PrismicNextImageProps } from "./PrismicNextImage";
|
|
20
|
+
export type { CreateClientConfig } from "./types.js";
|
|
31
21
|
|
|
32
|
-
export {
|
|
33
|
-
|
|
34
|
-
export type { CreateClientConfig, PrismicPreviewData } from "./types";
|
|
22
|
+
export { createLocaleRedirect } from "./createLocaleRedirect.js";
|
|
23
|
+
export type { CreateLocaleRedirectConfig } from "./createLocaleRedirect.js";
|
package/src/lib/devMsg.ts
CHANGED
|
@@ -7,7 +7,7 @@ import { version } from "../../package.json";
|
|
|
7
7
|
*
|
|
8
8
|
* ```ts
|
|
9
9
|
* devMsg("missing-param");
|
|
10
|
-
* // => "https://prismic.dev/msg/next/v1.2.3/missing-param
|
|
10
|
+
* // => "https://prismic.dev/msg/next/v1.2.3/missing-param"
|
|
11
11
|
* ```
|
|
12
12
|
*
|
|
13
13
|
* @param slug - Slug for the message. This corresponds to a Markdown file in
|