@prismetic/next-preview-core 1.0.14 → 1.0.15

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.
@@ -0,0 +1,43 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React$1 from 'react';
3
+
4
+ /**
5
+ * React Query provider for Preview Mode.
6
+ * Part of @prismetic/next-preview-core.
7
+ *
8
+ * Wrap the preview build's component tree in this provider to enable
9
+ * client-side caching of Strapi data. Layout data (Navbar, Footer)
10
+ * fetched once will be served from the cache on subsequent page navigations.
11
+ *
12
+ * @param {number} [staleTime=300000] - How long (ms) cached data is considered fresh. Default: 5 minutes.
13
+ * @param {boolean} [refetchOnWindowFocus=false] - Whether to refetch when the browser tab regains focus. Set to true in preview to auto-refresh after editing in Strapi.
14
+ */
15
+ declare function PreviewProviders({ children, staleTime, refetchOnWindowFocus, }: {
16
+ children: React.ReactNode;
17
+ staleTime?: number;
18
+ refetchOnWindowFocus?: boolean;
19
+ }): react_jsx_runtime.JSX.Element;
20
+
21
+ /**
22
+ * A generic Higher-Order Component for Preview Mode data fetching.
23
+ * Part of @prismetic/next-preview-core.
24
+ *
25
+ * @param {React.Component} WrappedComponent - The pure UI component to render once data is fetched.
26
+ * @param {Function} fetcherFn - An async function that fetches data (receives component props as argument).
27
+ * @param {Function} cacheKeyGenerator - A function that returns a React Query queryKey array (receives component props).
28
+ *
29
+ * @example
30
+ * // In your project's preview wrapper:
31
+ * import { withLivePreview } from "@prismetic/next-preview-core";
32
+ * import HomeUI from "../HomeUI";
33
+ * import { fetchHomepageContent } from "@/api/queryModules";
34
+ *
35
+ * export const PreviewHome = withLivePreview(
36
+ * HomeUI,
37
+ * ({ locale }) => fetchHomepageContent(locale),
38
+ * ({ locale }) => ["homepage", locale]
39
+ * );
40
+ */
41
+ declare function withLivePreview<P extends object>(WrappedComponent: React$1.ComponentType<P>, fetcherFn: (props: P) => Promise<any>, cacheKeyGenerator: (props: P) => any[]): (props: P) => react_jsx_runtime.JSX.Element;
42
+
43
+ export { PreviewProviders, withLivePreview };
@@ -0,0 +1,43 @@
1
+ import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import React$1 from 'react';
3
+
4
+ /**
5
+ * React Query provider for Preview Mode.
6
+ * Part of @prismetic/next-preview-core.
7
+ *
8
+ * Wrap the preview build's component tree in this provider to enable
9
+ * client-side caching of Strapi data. Layout data (Navbar, Footer)
10
+ * fetched once will be served from the cache on subsequent page navigations.
11
+ *
12
+ * @param {number} [staleTime=300000] - How long (ms) cached data is considered fresh. Default: 5 minutes.
13
+ * @param {boolean} [refetchOnWindowFocus=false] - Whether to refetch when the browser tab regains focus. Set to true in preview to auto-refresh after editing in Strapi.
14
+ */
15
+ declare function PreviewProviders({ children, staleTime, refetchOnWindowFocus, }: {
16
+ children: React.ReactNode;
17
+ staleTime?: number;
18
+ refetchOnWindowFocus?: boolean;
19
+ }): react_jsx_runtime.JSX.Element;
20
+
21
+ /**
22
+ * A generic Higher-Order Component for Preview Mode data fetching.
23
+ * Part of @prismetic/next-preview-core.
24
+ *
25
+ * @param {React.Component} WrappedComponent - The pure UI component to render once data is fetched.
26
+ * @param {Function} fetcherFn - An async function that fetches data (receives component props as argument).
27
+ * @param {Function} cacheKeyGenerator - A function that returns a React Query queryKey array (receives component props).
28
+ *
29
+ * @example
30
+ * // In your project's preview wrapper:
31
+ * import { withLivePreview } from "@prismetic/next-preview-core";
32
+ * import HomeUI from "../HomeUI";
33
+ * import { fetchHomepageContent } from "@/api/queryModules";
34
+ *
35
+ * export const PreviewHome = withLivePreview(
36
+ * HomeUI,
37
+ * ({ locale }) => fetchHomepageContent(locale),
38
+ * ({ locale }) => ["homepage", locale]
39
+ * );
40
+ */
41
+ declare function withLivePreview<P extends object>(WrappedComponent: React$1.ComponentType<P>, fetcherFn: (props: P) => Promise<any>, cacheKeyGenerator: (props: P) => any[]): (props: P) => react_jsx_runtime.JSX.Element;
42
+
43
+ export { PreviewProviders, withLivePreview };
package/dist/index.js ADDED
@@ -0,0 +1,114 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ PreviewProviders: () => PreviewProviders,
25
+ withLivePreview: () => withLivePreview
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/PreviewProviders.tsx
30
+ var import_react_query = require("@tanstack/react-query");
31
+ var import_react = require("react");
32
+ var import_jsx_runtime = require("react/jsx-runtime");
33
+ function PreviewProviders({
34
+ children,
35
+ staleTime = 5 * 60 * 1e3,
36
+ refetchOnWindowFocus = false
37
+ }) {
38
+ const [queryClient] = (0, import_react.useState)(
39
+ () => new import_react_query.QueryClient({
40
+ defaultOptions: {
41
+ queries: {
42
+ staleTime,
43
+ refetchOnWindowFocus,
44
+ retry: 1
45
+ }
46
+ }
47
+ })
48
+ );
49
+ (0, import_react.useEffect)(() => {
50
+ if (!refetchOnWindowFocus) return;
51
+ const handleFocus = () => {
52
+ if (document.visibilityState === "visible") {
53
+ queryClient.invalidateQueries({ refetchType: "active" });
54
+ }
55
+ };
56
+ document.addEventListener("visibilitychange", handleFocus);
57
+ window.addEventListener("focus", handleFocus);
58
+ return () => {
59
+ document.removeEventListener("visibilitychange", handleFocus);
60
+ window.removeEventListener("focus", handleFocus);
61
+ };
62
+ }, [queryClient, refetchOnWindowFocus]);
63
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_query.QueryClientProvider, { client: queryClient, children });
64
+ }
65
+
66
+ // src/withLivePreview.tsx
67
+ var import_react_query2 = require("@tanstack/react-query");
68
+ var import_jsx_runtime2 = require("react/jsx-runtime");
69
+ function withLivePreview(WrappedComponent, fetcherFn, cacheKeyGenerator) {
70
+ return function PreviewComponent(props) {
71
+ const queryKey = cacheKeyGenerator(props);
72
+ const { data, isLoading } = (0, import_react_query2.useQuery)({
73
+ queryKey,
74
+ queryFn: () => fetcherFn(props)
75
+ });
76
+ if (isLoading) {
77
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
78
+ "div",
79
+ {
80
+ style: {
81
+ minHeight: "100vh",
82
+ display: "flex",
83
+ alignItems: "center",
84
+ justifyContent: "center"
85
+ },
86
+ children: "Loading preview..."
87
+ }
88
+ );
89
+ }
90
+ if (!data) {
91
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(
92
+ "div",
93
+ {
94
+ style: {
95
+ position: "fixed",
96
+ height: "100vh",
97
+ background: "white",
98
+ zIndex: 1e4,
99
+ width: "100%",
100
+ display: "grid",
101
+ placeContent: "center"
102
+ },
103
+ children: "Page not found"
104
+ }
105
+ );
106
+ }
107
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(WrappedComponent, { ...props, ...data });
108
+ };
109
+ }
110
+ // Annotate the CommonJS export names for ESM import in node:
111
+ 0 && (module.exports = {
112
+ PreviewProviders,
113
+ withLivePreview
114
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,87 @@
1
+ "use client";
2
+
3
+ // src/PreviewProviders.tsx
4
+ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
5
+ import { useState, useEffect } from "react";
6
+ import { jsx } from "react/jsx-runtime";
7
+ function PreviewProviders({
8
+ children,
9
+ staleTime = 5 * 60 * 1e3,
10
+ refetchOnWindowFocus = false
11
+ }) {
12
+ const [queryClient] = useState(
13
+ () => new QueryClient({
14
+ defaultOptions: {
15
+ queries: {
16
+ staleTime,
17
+ refetchOnWindowFocus,
18
+ retry: 1
19
+ }
20
+ }
21
+ })
22
+ );
23
+ useEffect(() => {
24
+ if (!refetchOnWindowFocus) return;
25
+ const handleFocus = () => {
26
+ if (document.visibilityState === "visible") {
27
+ queryClient.invalidateQueries({ refetchType: "active" });
28
+ }
29
+ };
30
+ document.addEventListener("visibilitychange", handleFocus);
31
+ window.addEventListener("focus", handleFocus);
32
+ return () => {
33
+ document.removeEventListener("visibilitychange", handleFocus);
34
+ window.removeEventListener("focus", handleFocus);
35
+ };
36
+ }, [queryClient, refetchOnWindowFocus]);
37
+ return /* @__PURE__ */ jsx(QueryClientProvider, { client: queryClient, children });
38
+ }
39
+
40
+ // src/withLivePreview.tsx
41
+ import { useQuery } from "@tanstack/react-query";
42
+ import { jsx as jsx2 } from "react/jsx-runtime";
43
+ function withLivePreview(WrappedComponent, fetcherFn, cacheKeyGenerator) {
44
+ return function PreviewComponent(props) {
45
+ const queryKey = cacheKeyGenerator(props);
46
+ const { data, isLoading } = useQuery({
47
+ queryKey,
48
+ queryFn: () => fetcherFn(props)
49
+ });
50
+ if (isLoading) {
51
+ return /* @__PURE__ */ jsx2(
52
+ "div",
53
+ {
54
+ style: {
55
+ minHeight: "100vh",
56
+ display: "flex",
57
+ alignItems: "center",
58
+ justifyContent: "center"
59
+ },
60
+ children: "Loading preview..."
61
+ }
62
+ );
63
+ }
64
+ if (!data) {
65
+ return /* @__PURE__ */ jsx2(
66
+ "div",
67
+ {
68
+ style: {
69
+ position: "fixed",
70
+ height: "100vh",
71
+ background: "white",
72
+ zIndex: 1e4,
73
+ width: "100%",
74
+ display: "grid",
75
+ placeContent: "center"
76
+ },
77
+ children: "Page not found"
78
+ }
79
+ );
80
+ }
81
+ return /* @__PURE__ */ jsx2(WrappedComponent, { ...props, ...data });
82
+ };
83
+ }
84
+ export {
85
+ PreviewProviders,
86
+ withLivePreview
87
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismetic/next-preview-core",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "description": "Hybrid static/preview architecture for Next.js + Strapi projects",
5
5
  "author": "Prismetic Group FZ LLC",
6
6
  "main": "./dist/index.js",