@zubyjs/react 1.0.39 → 1.0.41

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zubyjs/react",
3
- "version": "1.0.39",
3
+ "version": "1.0.41",
4
4
  "description": "Zuby.js JsxProvider for react",
5
5
  "type": "module",
6
6
  "main": "index.js",
@@ -16,8 +16,8 @@
16
16
  "linkDirectory": true
17
17
  },
18
18
  "dependencies": {
19
- "@vitejs/plugin-react": "^4.2.0",
20
- "wouter": "^2.12.0",
19
+ "@vitejs/plugin-react": "^4.2.1",
20
+ "wouter": "^2.12.1",
21
21
  "react": "^18.2.0",
22
22
  "react-dom": "^18.2.0",
23
23
  "@types/react": "^18.2.37",
package/router.d.ts CHANGED
@@ -7,9 +7,10 @@ import { ZubyPageContext } from 'zuby/pageContext/index.js';
7
7
  export default function Router({ context }: {
8
8
  context: ZubyPageContext;
9
9
  }): import("react/jsx-runtime").JSX.Element;
10
- export declare function Page({ page, context }: {
10
+ export declare function Page({ page, context, initialPath, }: {
11
11
  page: LazyTemplate;
12
12
  context: ZubyPageContext;
13
+ initialPath?: boolean;
13
14
  }): import("react/jsx-runtime").JSX.Element;
14
15
  export declare function Error({ error, context }: {
15
16
  error: LazyTemplate;
package/router.js CHANGED
@@ -2,7 +2,7 @@ import { jsx as _jsx } from "react/jsx-runtime";
2
2
  import { Router as WouterRouter, Route as WouterRoute, Switch as WouterSwitch } from 'wouter';
3
3
  export { useRoute, useRouter, useParams, useLocation, Link, Redirect, } from 'wouter';
4
4
  import { useParams } from 'wouter';
5
- import { Suspense, lazy } from 'react';
5
+ import { Suspense, lazy, useEffect, useState } from 'react';
6
6
  import { createElement } from 'react';
7
7
  import { useFetch } from './hooks/index.js';
8
8
  let pages;
@@ -13,6 +13,14 @@ let errors;
13
13
  */
14
14
  export default function Router({ context }) {
15
15
  const isProduction = import.meta?.env?.MODE === 'production';
16
+ const [initialPath, setInitialPath] = useState(true);
17
+ useEffect(() => {
18
+ const onPathChange = () => setInitialPath(false);
19
+ window.addEventListener('popstate', onPathChange);
20
+ return () => {
21
+ window.removeEventListener('popstate', onPathChange);
22
+ };
23
+ }, [initialPath, setInitialPath]);
16
24
  const loadTemplates = (templates) => templates?.map((pageTemplate) => ({
17
25
  ...pageTemplate,
18
26
  component: lazy(pageTemplate.component),
@@ -33,7 +41,7 @@ export default function Router({ context }) {
33
41
  return pathRegex.test(context.url.pathname ?? '');
34
42
  });
35
43
  const routes = [
36
- ...pages.map((page) => (_jsx(WouterRoute, { path: page.path, children: _jsx(Page, { context: context, page: page }) }, page.path))),
44
+ ...pages.map((page) => (_jsx(WouterRoute, { path: page.path, children: _jsx(Page, { context: context, page: page, initialPath: initialPath }) }, page.path))),
37
45
  _jsx(WouterRoute, { children: error?.component && _jsx(Error, { context: context, error: error }) }, "error"),
38
46
  ];
39
47
  const page = _jsx(WouterSwitch, { children: routes });
@@ -41,13 +49,13 @@ export default function Router({ context }) {
41
49
  children: page,
42
50
  }) }) }));
43
51
  }
44
- export function Page({ page, context }) {
52
+ export function Page({ page, context, initialPath, }) {
45
53
  context.statusCode = 200;
46
54
  context.params = useParams();
47
- if (typeof window !== 'undefined') {
55
+ if (typeof window !== 'undefined' && !initialPath) {
48
56
  context.props = useFetch(`/_props${context.url.pathname}`);
49
57
  }
50
- return _jsx(page.component, { context: context });
58
+ return _jsx(page.component, { ...context.props, context: context });
51
59
  }
52
60
  export function Error({ error, context }) {
53
61
  context.statusCode = 404;
@@ -5,7 +5,9 @@ import { hydrateRoot, createRoot } from 'react-dom/client';
5
5
  import Router from '@zubyjs/react/router.js';
6
6
  import { ZubyPageContext } from 'zuby/pageContext/index.js';
7
7
  if (typeof window !== 'undefined') {
8
- const pageContext = new ZubyPageContext();
8
+ const pageContext = new ZubyPageContext({
9
+ props: global._props,
10
+ });
9
11
  const appElement = document.getElementById('app');
10
12
  if (appElement.hasChildNodes()) {
11
13
  hydrateRoot(appElement, _jsx(Router, { context: pageContext }));
@@ -1,3 +1,5 @@
1
- export default function InnerLayout({ innerHtml }: {
1
+ import { ZubyPageContext } from 'zuby/pageContext/index.js';
2
+ export default function InnerLayout({ innerHtml, context, }: {
2
3
  innerHtml: string;
4
+ context: ZubyPageContext;
3
5
  }): import("react/jsx-runtime").JSX.Element;
@@ -1,4 +1,6 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
2
- export default function InnerLayout({ innerHtml }) {
3
- return _jsx("div", { id: "app", dangerouslySetInnerHTML: { __html: innerHtml } });
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ export default function InnerLayout({ innerHtml, context, }) {
3
+ return (_jsxs(_Fragment, { children: [_jsx("div", { id: "app", dangerouslySetInnerHTML: { __html: innerHtml } }), _jsx("script", { type: "text/javascript", dangerouslySetInnerHTML: {
4
+ __html: `global._props=${JSON.stringify(context.props)};`,
5
+ } })] }));
4
6
  }