fimo-next 2.8.0-staging.1 → 2.8.0-staging.11

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # fimo-next
2
2
 
3
- The Next.js runtime for Fimo projects. Install it alongside `fimo` and
3
+ The Next.js runtime for Fimo sites. Install it alongside `fimo` and
4
4
  `fimo-react` at the same version, plus `@tanstack/react-query`:
5
5
 
6
6
  ```bash
@@ -9,7 +9,7 @@ npm install fimo fimo-react fimo-next @tanstack/react-query
9
9
 
10
10
  Render `FimoProvider` once at the application root: from `fimo-next` in the
11
11
  App Router root layout (a Server Component), or from `fimo-next/client` in
12
- `pages/_app.tsx`. It resolves the connection to the linked Fimo project on the
12
+ `pages/_app.tsx`. It resolves the connection to the linked Fimo site on the
13
13
  server and passes only public connection data to the browser; the application
14
14
  never manages Fimo runtime variables.
15
15
 
@@ -68,5 +68,5 @@ export function parseConnection(text) {
68
68
  return null;
69
69
  }
70
70
  }
71
- export const NOT_CONNECTED_MESSAGE = 'This Next.js application is not connected to a Fimo project. ' +
71
+ export const NOT_CONNECTED_MESSAGE = 'This Next.js application is not connected to a Fimo site. ' +
72
72
  'Run `fimo link` in the project directory, or set FIMO_CONTENT_API_URL to the tenant content API URL.';
@@ -4,7 +4,7 @@ import { QueryClient, QueryClientContext, QueryClientProvider } from '@tanstack/
4
4
  import { createLabels } from 'fimo/labels';
5
5
  import { ensurePreviewScript } from 'fimo/preview';
6
6
  import { useRouter as usePagesRouter } from 'next/compat/router.js';
7
- import { usePathname } from 'next/navigation.js';
7
+ import { usePathname, useRouter } from 'next/navigation.js';
8
8
  import { createContext, Suspense, useCallback, useContext, useEffect, useMemo, useState } from 'react';
9
9
  import { resolveConnection } from '#connection';
10
10
  import { connectClientRuntime } from './client-runtime.js';
@@ -40,23 +40,37 @@ export function FimoProvider({ children, ...props }) {
40
40
  const [connection] = useState(resolveConnection);
41
41
  return (_jsxs(_Fragment, { children: [_jsx("script", { id: FIMO_CONNECTION_SCRIPT_ID, type: "application/json", dangerouslySetInnerHTML: { __html: serializeConnection(connection) } }), _jsx(FimoClientProvider, { connection: connection, ...props, children: children })] }));
42
42
  }
43
+ /**
44
+ * `next/compat/router` answers only under the Pages Router, and
45
+ * `next/navigation`'s router hook only under the App Router, so each router
46
+ * gets its own bridge rather than one component calling a hook that may not
47
+ * exist.
48
+ */
43
49
  function FimoPreviewRuntime({ previewScriptUrl }) {
50
+ const pagesRouter = usePagesRouter();
51
+ return pagesRouter ? (_jsx(PagesRouterPreview, { router: pagesRouter, previewScriptUrl: previewScriptUrl })) : (_jsx(AppRouterPreview, { previewScriptUrl: previewScriptUrl }));
52
+ }
53
+ function AppRouterPreview({ previewScriptUrl }) {
54
+ const router = useRouter();
55
+ // The router fetches and renders the picked route; a raw history.pushState
56
+ // only moved the URL and left the previous page on screen.
57
+ const navigate = useCallback((to) => router.push(to), [router]);
58
+ usePreviewBridge(previewScriptUrl, usePathname() ?? '/', navigate);
59
+ return null;
60
+ }
61
+ function PagesRouterPreview({ router, previewScriptUrl, }) {
62
+ const pathname = usePathname() ?? router.asPath.split(/[?#]/, 1)[0] ?? '/';
63
+ const navigate = useCallback((to) => router.push(to), [router]);
64
+ usePreviewBridge(previewScriptUrl, pathname, navigate);
65
+ return null;
66
+ }
67
+ function usePreviewBridge(previewScriptUrl, pathname, navigate) {
44
68
  const context = useContext(FimoNextContext);
45
69
  const locale = context.locale ?? context.connection?.defaultLocale ?? 'en';
46
- const appPathname = usePathname();
47
- const pagesRouter = usePagesRouter();
48
- const pathname = appPathname ?? pagesRouter?.asPath.split(/[?#]/, 1)[0] ?? '/';
49
- const navigate = useCallback((to) => {
50
- if (pagesRouter) {
51
- return pagesRouter.push(to);
52
- }
53
- window.history.pushState(null, '', to);
54
- }, [pagesRouter]);
55
70
  useEffect(() => {
56
71
  ensurePreviewScript(previewScriptUrl);
57
72
  return connectNextPreview({ locale, navigate, pathname, previewScriptUrl });
58
73
  }, [locale, navigate, pathname, previewScriptUrl]);
59
- return null;
60
74
  }
61
75
  export function useNextContentLocale(requestedLocale, defaultLocale) {
62
76
  const context = useContext(FimoNextContext);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fimo-next",
3
- "version": "2.8.0-staging.1",
3
+ "version": "2.8.0-staging.11",
4
4
  "description": "Fimo runtime for Next.js projects.",
5
5
  "files": [
6
6
  "dist/",
@@ -49,22 +49,22 @@
49
49
  "test:watch": "vitest"
50
50
  },
51
51
  "devDependencies": {
52
- "@fimo/tsconfig": "2.8.0-staging.1",
52
+ "@fimo/tsconfig": "2.8.0-staging.11",
53
53
  "@tanstack/react-query": "^5.80.6",
54
54
  "@types/node": "22.18.6",
55
55
  "@types/react": "19.2.14",
56
56
  "@types/react-dom": "19.2.3",
57
- "fimo": "2.8.0-staging.1",
58
- "fimo-react": "2.8.0-staging.1",
59
- "next": "16.3.3",
57
+ "fimo": "2.8.0-staging.11",
58
+ "fimo-react": "2.8.0-staging.11",
59
+ "next": "16.3.8",
60
60
  "react": "19.2.4",
61
61
  "react-dom": "19.2.4",
62
62
  "typescript": "7.0.2",
63
- "vitest": "5.0.0"
63
+ "vitest": "5.0.3"
64
64
  },
65
65
  "peerDependencies": {
66
66
  "@tanstack/react-query": "^5.80.6",
67
- "fimo": "2.8.0-staging.1",
67
+ "fimo": "2.8.0-staging.11",
68
68
  "fimo-react": ">=2.0.1",
69
69
  "next": "^15.0.0 || ^16.0.0",
70
70
  "react": "^18.2.0 || ^19.0.0",