@prismicio/next 1.0.3 → 1.1.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.
Files changed (54) hide show
  1. package/dist/PrismicNextImage.cjs +9 -3
  2. package/dist/PrismicNextImage.cjs.map +1 -1
  3. package/dist/PrismicNextImage.d.ts +2 -2
  4. package/dist/PrismicNextImage.js +8 -2
  5. package/dist/PrismicNextImage.js.map +1 -1
  6. package/dist/PrismicNextLink.cjs +40 -0
  7. package/dist/PrismicNextLink.cjs.map +1 -0
  8. package/dist/PrismicNextLink.d.ts +126 -0
  9. package/dist/PrismicNextLink.js +22 -0
  10. package/dist/PrismicNextLink.js.map +1 -0
  11. package/dist/PrismicPreview.cjs +2 -0
  12. package/dist/PrismicPreview.cjs.map +1 -1
  13. package/dist/PrismicPreview.js +2 -0
  14. package/dist/PrismicPreview.js.map +1 -1
  15. package/dist/enableAutoPreviews.cjs.map +1 -1
  16. package/dist/enableAutoPreviews.d.ts +3 -3
  17. package/dist/enableAutoPreviews.js.map +1 -1
  18. package/dist/index.cjs +2 -0
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.ts +15 -7
  21. package/dist/index.js +2 -0
  22. package/dist/index.js.map +1 -1
  23. package/dist/lib/getPreviewCookieRepositoryName.cjs.map +1 -1
  24. package/dist/lib/getPreviewCookieRepositoryName.js.map +1 -1
  25. package/dist/lib/getPrismicPreviewCookie.cjs.map +1 -1
  26. package/dist/lib/getPrismicPreviewCookie.js.map +1 -1
  27. package/dist/package.json.cjs +1 -1
  28. package/dist/package.json.js +1 -1
  29. package/dist/react-server/PrismicPreview.d.ts +22 -0
  30. package/dist/react-server/index.d.ts +2 -0
  31. package/dist/react-server/unsupported.cjs +26 -0
  32. package/dist/react-server/unsupported.cjs.map +1 -0
  33. package/dist/react-server/unsupported.d.ts +6 -0
  34. package/dist/react-server/unsupported.js +26 -0
  35. package/dist/react-server/unsupported.js.map +1 -0
  36. package/dist/react-server.cjs +15 -0
  37. package/dist/react-server.cjs.map +1 -0
  38. package/dist/react-server.js +15 -0
  39. package/dist/react-server.js.map +1 -0
  40. package/dist/redirectToPreviewURL.cjs.map +1 -1
  41. package/dist/redirectToPreviewURL.d.ts +4 -5
  42. package/dist/redirectToPreviewURL.js.map +1 -1
  43. package/dist/setPreviewData.cjs.map +1 -1
  44. package/dist/setPreviewData.js.map +1 -1
  45. package/package.json +37 -32
  46. package/src/PrismicNextImage.tsx +5 -4
  47. package/src/PrismicNextLink.tsx +55 -0
  48. package/src/PrismicPreview.tsx +2 -0
  49. package/src/enableAutoPreviews.ts +9 -3
  50. package/src/index.ts +16 -13
  51. package/src/react-server/PrismicPreview.tsx +74 -0
  52. package/src/react-server/index.ts +11 -0
  53. package/src/react-server/unsupported.ts +26 -0
  54. package/src/redirectToPreviewURL.ts +7 -5
@@ -0,0 +1,74 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+ import Script from "next/script";
5
+ import * as prismic from "@prismicio/client";
6
+
7
+ /**
8
+ * Props for `<PrismicPreview>`.
9
+ */
10
+ export type PrismicPreviewProps = {
11
+ /**
12
+ * The name of your Prismic repository. A Prismic Toolbar will be registered
13
+ * using this repository.
14
+ */
15
+ repositoryName: string;
16
+
17
+ children?: React.ReactNode;
18
+ };
19
+
20
+ /**
21
+ * React component that sets up Prismic Previews using the Prismic Toolbar. When
22
+ * the Prismic Toolbar send events to the browser, such as on preview updates
23
+ * and exiting, this component will automatically update Next.js Preview Mode
24
+ * and refresh the page.
25
+ *
26
+ * This component can be wrapped around your app or added anywhere in your app's
27
+ * tree. It must be rendered on every page.
28
+ */
29
+ export function PrismicPreview({
30
+ repositoryName,
31
+ children,
32
+ }: PrismicPreviewProps): JSX.Element {
33
+ const prismicToolbarSrc = prismic.getToolbarSrc(repositoryName);
34
+
35
+ React.useEffect(() => {
36
+ const handlePrismicPreviewUpdate = async (event: Event) => {
37
+ // Prevent the toolbar from reloading the page.
38
+ event.preventDefault();
39
+
40
+ // TODO: Handle preview updates.
41
+
42
+ // eslint-disable-next-line no-console
43
+ console.log("[PrismicPreview]: Update event called.");
44
+ };
45
+
46
+ const handlePrismicPreviewEnd = async (event: Event) => {
47
+ // Prevent the toolbar from reloading the page.
48
+ event.preventDefault();
49
+
50
+ // TODO: Handle preview exit.
51
+
52
+ // eslint-disable-next-line no-console
53
+ console.log("[PrismicPreview]: End event called.");
54
+ };
55
+
56
+ window.addEventListener("prismicPreviewUpdate", handlePrismicPreviewUpdate);
57
+ window.addEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
58
+
59
+ return () => {
60
+ window.removeEventListener(
61
+ "prismicPreviewUpdate",
62
+ handlePrismicPreviewUpdate,
63
+ );
64
+ window.removeEventListener("prismicPreviewEnd", handlePrismicPreviewEnd);
65
+ };
66
+ }, []);
67
+
68
+ return (
69
+ <>
70
+ {children}
71
+ <Script src={prismicToolbarSrc} strategy="lazyOnload" />
72
+ </>
73
+ );
74
+ }
@@ -0,0 +1,11 @@
1
+ export {
2
+ PrismicPreview,
3
+ enableAutoPreviews,
4
+ redirectToPreviewURL,
5
+ exitPreview,
6
+ setPreviewData,
7
+ } from "./unsupported";
8
+
9
+ // These exports do not have RSC-specific implementations.
10
+ // They are aliases for the root-level exports.
11
+ export { PrismicNextImage, imgixLoader, PrismicNextLink } from "..";
@@ -0,0 +1,26 @@
1
+ // Exports in this file are unsupported in the react-server environment.
2
+ // All environments should export the same functions to maintian API compatability.
3
+
4
+ function buildErrorMessage(fnName: string) {
5
+ return `${fnName} is currently incompatible with App Router. Preview Mode is not supported in App Router at this time. Please remove all uses of ${fnName}.`;
6
+ }
7
+
8
+ export function PrismicPreview(): JSX.Element {
9
+ throw new Error(buildErrorMessage("<PrismicPreview>"));
10
+ }
11
+
12
+ export function enableAutoPreviews() {
13
+ throw new Error(buildErrorMessage("enableAutoPreviews()"));
14
+ }
15
+
16
+ export function exitPreview() {
17
+ throw new Error(buildErrorMessage("exitPreview()"));
18
+ }
19
+
20
+ export function redirectToPreviewURL() {
21
+ throw new Error(buildErrorMessage("redirectToPreviewURL()"));
22
+ }
23
+
24
+ export function setPreviewData() {
25
+ throw new Error(buildErrorMessage("setPreviewData()"));
26
+ }
@@ -1,6 +1,5 @@
1
- import type { Client } from "@prismicio/client";
2
1
  import type { NextApiRequest, NextApiResponse } from "next";
3
- import type { LinkResolverFunction } from "@prismicio/helpers";
2
+ import type * as prismic from "@prismicio/client";
4
3
 
5
4
  type PrismicNextQuery = {
6
5
  documentId: string;
@@ -28,7 +27,7 @@ const isPrismicNextQuery = (
28
27
  */
29
28
  export type RedirectToPreviewURLConfig<
30
29
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
31
- TLinkResolverFunction extends LinkResolverFunction<any> = LinkResolverFunction,
30
+ TLinkResolverFunction extends prismic.LinkResolverFunction<any> = prismic.LinkResolverFunction,
32
31
  > = {
33
32
  /**
34
33
  * The `req` object from a Next.js API route. This is given as a parameter to
@@ -53,7 +52,10 @@ export type RedirectToPreviewURLConfig<
53
52
  /**
54
53
  * The Prismic client configured for the preview session's repository.
55
54
  */
56
- client: Client;
55
+ // `Pick` is used to use the smallest possible subset of
56
+ // `prismic.Client`. Doing this reduces the surface area for breaking
57
+ // type changes.
58
+ client: Pick<prismic.Client, "resolvePreviewURL">;
57
59
 
58
60
  /**
59
61
  * A Link Resolver used to resolve the previewed document's URL.
@@ -89,7 +91,7 @@ export type RedirectToPreviewURLConfig<
89
91
  */
90
92
  export async function redirectToPreviewURL<
91
93
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
92
- TLinkResolverFunction extends LinkResolverFunction<any>,
94
+ TLinkResolverFunction extends prismic.LinkResolverFunction<any>,
93
95
  >(config: RedirectToPreviewURLConfig<TLinkResolverFunction>): Promise<void> {
94
96
  const defaultURL = config.defaultURL || "/";
95
97
  const basePath = config.basePath || "";