@prismetic/next-preview-core 1.0.8 → 1.0.14
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 +3 -0
- package/package.json +2 -1
- package/src/withLivePreview.tsx +23 -2
- package/dist/index.d.mts +0 -43
- package/dist/index.d.ts +0 -43
- package/dist/index.js +0 -89
- package/dist/index.mjs +0 -62
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prismetic/next-preview-core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"description": "Hybrid static/preview architecture for Next.js + Strapi projects",
|
|
5
|
+
"author": "Prismetic Group FZ LLC",
|
|
5
6
|
"main": "./dist/index.js",
|
|
6
7
|
"module": "./dist/index.mjs",
|
|
7
8
|
"types": "./dist/index.d.ts",
|
package/src/withLivePreview.tsx
CHANGED
|
@@ -38,14 +38,35 @@ export function withLivePreview<P extends object>(
|
|
|
38
38
|
|
|
39
39
|
if (isLoading) {
|
|
40
40
|
return (
|
|
41
|
-
<div
|
|
41
|
+
<div
|
|
42
|
+
style={{
|
|
43
|
+
minHeight: "100vh",
|
|
44
|
+
display: "flex",
|
|
45
|
+
alignItems: "center",
|
|
46
|
+
justifyContent: "center",
|
|
47
|
+
}}
|
|
48
|
+
>
|
|
42
49
|
Loading preview...
|
|
43
50
|
</div>
|
|
44
51
|
);
|
|
45
52
|
}
|
|
46
53
|
|
|
47
54
|
if (!data) {
|
|
48
|
-
return
|
|
55
|
+
return (
|
|
56
|
+
<div
|
|
57
|
+
style={{
|
|
58
|
+
position: "fixed",
|
|
59
|
+
height: "100vh",
|
|
60
|
+
background: "white",
|
|
61
|
+
zIndex: 10000,
|
|
62
|
+
width: "100%",
|
|
63
|
+
display: "grid",
|
|
64
|
+
placeContent: "center",
|
|
65
|
+
}}
|
|
66
|
+
>
|
|
67
|
+
Page not found
|
|
68
|
+
</div>
|
|
69
|
+
);
|
|
49
70
|
}
|
|
50
71
|
|
|
51
72
|
// Props spread first, then fetched data takes priority
|
package/dist/index.d.mts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
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.d.ts
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
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)("div", { style: { minHeight: "100vh", display: "flex", alignItems: "center", justifyContent: "center" }, children: "Loading preview..." });
|
|
78
|
-
}
|
|
79
|
-
if (!data) {
|
|
80
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: "Page not found" });
|
|
81
|
-
}
|
|
82
|
-
return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(WrappedComponent, { ...props, ...data });
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
-
0 && (module.exports = {
|
|
87
|
-
PreviewProviders,
|
|
88
|
-
withLivePreview
|
|
89
|
-
});
|
package/dist/index.mjs
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
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("div", { style: { minHeight: "100vh", display: "flex", alignItems: "center", justifyContent: "center" }, children: "Loading preview..." });
|
|
52
|
-
}
|
|
53
|
-
if (!data) {
|
|
54
|
-
return /* @__PURE__ */ jsx2("div", { children: "Page not found" });
|
|
55
|
-
}
|
|
56
|
-
return /* @__PURE__ */ jsx2(WrappedComponent, { ...props, ...data });
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
export {
|
|
60
|
-
PreviewProviders,
|
|
61
|
-
withLivePreview
|
|
62
|
-
};
|