@prismetic/next-preview-core 1.0.0 → 1.0.2

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,41 @@
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
+ */
14
+ declare function PreviewProviders({ children, staleTime }: {
15
+ children: React.ReactNode;
16
+ staleTime?: number;
17
+ }): react_jsx_runtime.JSX.Element;
18
+
19
+ /**
20
+ * A generic Higher-Order Component for Preview Mode data fetching.
21
+ * Part of @prismetic/next-preview-core.
22
+ *
23
+ * @param {React.Component} WrappedComponent - The pure UI component to render once data is fetched.
24
+ * @param {Function} fetcherFn - An async function that fetches data (receives component props as argument).
25
+ * @param {Function} cacheKeyGenerator - A function that returns a React Query queryKey array (receives component props).
26
+ *
27
+ * @example
28
+ * // In your project's preview wrapper:
29
+ * import { withLivePreview } from "@prismetic/next-preview-core";
30
+ * import HomeUI from "../HomeUI";
31
+ * import { fetchHomepageContent } from "@/api/queryModules";
32
+ *
33
+ * export const PreviewHome = withLivePreview(
34
+ * HomeUI,
35
+ * ({ locale }) => fetchHomepageContent(locale),
36
+ * ({ locale }) => ["homepage", locale]
37
+ * );
38
+ */
39
+ 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;
40
+
41
+ export { PreviewProviders, withLivePreview };
@@ -0,0 +1,41 @@
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
+ */
14
+ declare function PreviewProviders({ children, staleTime }: {
15
+ children: React.ReactNode;
16
+ staleTime?: number;
17
+ }): react_jsx_runtime.JSX.Element;
18
+
19
+ /**
20
+ * A generic Higher-Order Component for Preview Mode data fetching.
21
+ * Part of @prismetic/next-preview-core.
22
+ *
23
+ * @param {React.Component} WrappedComponent - The pure UI component to render once data is fetched.
24
+ * @param {Function} fetcherFn - An async function that fetches data (receives component props as argument).
25
+ * @param {Function} cacheKeyGenerator - A function that returns a React Query queryKey array (receives component props).
26
+ *
27
+ * @example
28
+ * // In your project's preview wrapper:
29
+ * import { withLivePreview } from "@prismetic/next-preview-core";
30
+ * import HomeUI from "../HomeUI";
31
+ * import { fetchHomepageContent } from "@/api/queryModules";
32
+ *
33
+ * export const PreviewHome = withLivePreview(
34
+ * HomeUI,
35
+ * ({ locale }) => fetchHomepageContent(locale),
36
+ * ({ locale }) => ["homepage", locale]
37
+ * );
38
+ */
39
+ 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;
40
+
41
+ export { PreviewProviders, withLivePreview };
package/dist/index.js ADDED
@@ -0,0 +1,71 @@
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({ children, staleTime = 5 * 60 * 1e3 }) {
34
+ const [queryClient] = (0, import_react.useState)(
35
+ () => new import_react_query.QueryClient({
36
+ defaultOptions: {
37
+ queries: {
38
+ staleTime,
39
+ refetchOnWindowFocus: false,
40
+ retry: 1
41
+ }
42
+ }
43
+ })
44
+ );
45
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_query.QueryClientProvider, { client: queryClient, children });
46
+ }
47
+
48
+ // src/withLivePreview.tsx
49
+ var import_react_query2 = require("@tanstack/react-query");
50
+ var import_jsx_runtime2 = require("react/jsx-runtime");
51
+ function withLivePreview(WrappedComponent, fetcherFn, cacheKeyGenerator) {
52
+ return function PreviewComponent(props) {
53
+ const queryKey = cacheKeyGenerator(props);
54
+ const { data, isLoading } = (0, import_react_query2.useQuery)({
55
+ queryKey,
56
+ queryFn: () => fetcherFn(props)
57
+ });
58
+ if (isLoading) {
59
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { style: { minHeight: "100vh", display: "flex", alignItems: "center", justifyContent: "center" }, children: "Loading preview..." });
60
+ }
61
+ if (!data) {
62
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)("div", { children: "Page not found" });
63
+ }
64
+ return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)(WrappedComponent, { ...props, ...data });
65
+ };
66
+ }
67
+ // Annotate the CommonJS export names for ESM import in node:
68
+ 0 && (module.exports = {
69
+ PreviewProviders,
70
+ withLivePreview
71
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,44 @@
1
+ "use client";
2
+
3
+ // src/PreviewProviders.tsx
4
+ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
5
+ import { useState } from "react";
6
+ import { jsx } from "react/jsx-runtime";
7
+ function PreviewProviders({ children, staleTime = 5 * 60 * 1e3 }) {
8
+ const [queryClient] = useState(
9
+ () => new QueryClient({
10
+ defaultOptions: {
11
+ queries: {
12
+ staleTime,
13
+ refetchOnWindowFocus: false,
14
+ retry: 1
15
+ }
16
+ }
17
+ })
18
+ );
19
+ return /* @__PURE__ */ jsx(QueryClientProvider, { client: queryClient, children });
20
+ }
21
+
22
+ // src/withLivePreview.tsx
23
+ import { useQuery } from "@tanstack/react-query";
24
+ import { jsx as jsx2 } from "react/jsx-runtime";
25
+ function withLivePreview(WrappedComponent, fetcherFn, cacheKeyGenerator) {
26
+ return function PreviewComponent(props) {
27
+ const queryKey = cacheKeyGenerator(props);
28
+ const { data, isLoading } = useQuery({
29
+ queryKey,
30
+ queryFn: () => fetcherFn(props)
31
+ });
32
+ if (isLoading) {
33
+ return /* @__PURE__ */ jsx2("div", { style: { minHeight: "100vh", display: "flex", alignItems: "center", justifyContent: "center" }, children: "Loading preview..." });
34
+ }
35
+ if (!data) {
36
+ return /* @__PURE__ */ jsx2("div", { children: "Page not found" });
37
+ }
38
+ return /* @__PURE__ */ jsx2(WrappedComponent, { ...props, ...data });
39
+ };
40
+ }
41
+ export {
42
+ PreviewProviders,
43
+ withLivePreview
44
+ };
package/package.json CHANGED
@@ -1,10 +1,16 @@
1
1
  {
2
2
  "name": "@prismetic/next-preview-core",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Hybrid static/preview architecture for Next.js + Strapi projects",
5
- "main": "./src/index.ts",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
6
8
  "exports": {
7
- ".": "./src/index.ts"
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.mjs",
12
+ "require": "./dist/index.js"
13
+ }
8
14
  },
9
15
  "keywords": [
10
16
  "next.js",
@@ -12,9 +18,19 @@
12
18
  "preview",
13
19
  "static"
14
20
  ],
21
+ "scripts": {
22
+ "build": "tsup",
23
+ "dev": "tsup --watch"
24
+ },
15
25
  "license": "MIT",
16
26
  "peerDependencies": {
17
- "react": ">=18",
18
- "@tanstack/react-query": ">=5"
27
+ "@tanstack/react-query": ">=5",
28
+ "react": ">=18"
29
+ },
30
+ "devDependencies": {
31
+ "@types/node": "^25.3.0",
32
+ "@types/react": "^19.2.14",
33
+ "tsup": "^8.5.1",
34
+ "typescript": "^5.9.3"
19
35
  }
20
- }
36
+ }
@@ -1,3 +1,4 @@
1
+
1
2
  "use client";
2
3
 
3
4
  import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
@@ -12,15 +13,24 @@ import { useState } from "react";
12
13
  * fetched once will be served from the cache on subsequent page navigations.
13
14
  *
14
15
  * @param {number} [staleTime=300000] - How long (ms) cached data is considered fresh. Default: 5 minutes.
16
+ * @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.
15
17
  */
16
- export function PreviewProviders({ children, staleTime = 5 * 60 * 1000 }) {
18
+ export function PreviewProviders({
19
+ children,
20
+ staleTime = 5 * 60 * 1000,
21
+ refetchOnWindowFocus = false,
22
+ }: {
23
+ children: React.ReactNode;
24
+ staleTime?: number;
25
+ refetchOnWindowFocus?: boolean;
26
+ }) {
17
27
  const [queryClient] = useState(
18
28
  () =>
19
29
  new QueryClient({
20
30
  defaultOptions: {
21
31
  queries: {
22
32
  staleTime,
23
- refetchOnWindowFocus: false,
33
+ refetchOnWindowFocus,
24
34
  retry: 1,
25
35
  },
26
36
  },
@@ -1,6 +1,7 @@
1
1
  "use client";
2
2
 
3
3
  import { useQuery } from "@tanstack/react-query";
4
+ import React from "react";
4
5
 
5
6
  /**
6
7
  * A generic Higher-Order Component for Preview Mode data fetching.
@@ -22,8 +23,12 @@ import { useQuery } from "@tanstack/react-query";
22
23
  * ({ locale }) => ["homepage", locale]
23
24
  * );
24
25
  */
25
- export function withLivePreview(WrappedComponent, fetcherFn, cacheKeyGenerator) {
26
- return function PreviewComponent(props) {
26
+ export function withLivePreview<P extends object>(
27
+ WrappedComponent: React.ComponentType<P>,
28
+ fetcherFn: (props: P) => Promise<any>,
29
+ cacheKeyGenerator: (props: P) => any[]
30
+ ) {
31
+ return function PreviewComponent(props: P) {
27
32
  const queryKey = cacheKeyGenerator(props);
28
33
 
29
34
  const { data, isLoading } = useQuery({
package/tsconfig.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es2020",
4
+ "module": "esnext",
5
+ "lib": ["DOM", "DOM.Iterable", "ESNext"],
6
+ "skipLibCheck": true,
7
+ "moduleResolution": "bundler",
8
+ "strict": true,
9
+ "jsx": "react-jsx",
10
+ "declaration": true,
11
+ "baseUrl": "."
12
+ },
13
+ "include": ["src/**/*"]
14
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,15 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["cjs", "esm"],
6
+ dts: true,
7
+ clean: true,
8
+ external: ["react", "react-dom", "@tanstack/react-query"],
9
+ esbuildOptions(options) {
10
+ // Force Next.js App Router client directive on the output bundles
11
+ options.banner = {
12
+ js: '"use client";',
13
+ };
14
+ },
15
+ });