@zubyjs/preact 1.0.45 → 1.0.47
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/hooks/useProps.d.ts +1 -0
- package/hooks/useProps.js +6 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/router.d.ts +12 -1
- package/router.js +52 -18
- package/templates/loader.d.ts +1 -0
- package/templates/loader.js +4 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function useProps(path: string, priority?: 'low' | 'high' | 'auto'): string | Object;
|
package/index.js
CHANGED
|
@@ -11,6 +11,7 @@ export default () => ({
|
|
|
11
11
|
layoutTemplateFile: normalizePath(resolve(__dirname, 'templates', 'layout.js')),
|
|
12
12
|
innerLayoutTemplateFile: normalizePath(resolve(__dirname, 'templates', 'innerLayout.js')),
|
|
13
13
|
errorTemplateFile: normalizePath(resolve(__dirname, 'templates', 'error.js')),
|
|
14
|
+
loaderTemplateFile: normalizePath(resolve(__dirname, 'templates', 'loader.js')),
|
|
14
15
|
renderFile: normalizePath(resolve(__dirname, 'render.js')),
|
|
15
16
|
getPlugins: () => preact({
|
|
16
17
|
prefreshEnabled: false,
|
package/package.json
CHANGED
package/router.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { useRoute, useRouter, useParams, useLocation,
|
|
1
|
+
export { useRoute, useRouter, useParams, useLocation, Redirect, RedirectProps, } from 'wouter-preact';
|
|
2
2
|
import { LazyTemplate } from 'zuby/templates/types.js';
|
|
3
3
|
import { createElement } from 'preact';
|
|
4
4
|
import { ZubyPageContext } from 'zuby/pageContext/index.js';
|
|
@@ -16,3 +16,14 @@ export declare function Error({ error, context }: {
|
|
|
16
16
|
error: LazyTemplate;
|
|
17
17
|
context: ZubyPageContext;
|
|
18
18
|
}): createElement.JSX.Element;
|
|
19
|
+
export interface LinkProps {
|
|
20
|
+
href?: string;
|
|
21
|
+
to?: string;
|
|
22
|
+
activeClassName?: string;
|
|
23
|
+
activeClass?: string;
|
|
24
|
+
className?: string;
|
|
25
|
+
class?: string;
|
|
26
|
+
onClick?: (event: MouseEvent) => void;
|
|
27
|
+
children?: any;
|
|
28
|
+
}
|
|
29
|
+
export declare function Link(props: LinkProps): createElement.JSX.Element;
|
package/router.js
CHANGED
|
@@ -1,43 +1,41 @@
|
|
|
1
1
|
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
|
-
import { Router as WouterRouter, Route as WouterRoute, Switch as WouterSwitch, } from 'wouter-preact';
|
|
3
|
-
export { useRoute, useRouter, useParams, useLocation,
|
|
2
|
+
import { Router as WouterRouter, Route as WouterRoute, Switch as WouterSwitch, Link as WouterLink, useRoute, } from 'wouter-preact';
|
|
3
|
+
export { useRoute, useRouter, useParams, useLocation, Redirect, } from 'wouter-preact';
|
|
4
4
|
import { useParams } from 'wouter-preact';
|
|
5
5
|
import { Suspense, lazy } from 'preact/compat';
|
|
6
6
|
import { createElement } from 'preact';
|
|
7
|
-
import {
|
|
7
|
+
import { preloadPage } from 'zuby/preload/index.js';
|
|
8
|
+
import useProps from '@zubyjs/react/src/hooks/useProps.js';
|
|
8
9
|
let pages;
|
|
9
10
|
let apps;
|
|
10
11
|
let errors;
|
|
12
|
+
let loaders;
|
|
11
13
|
/**
|
|
12
14
|
* Zuby's Router component provides support for file-system based routing.
|
|
13
15
|
*/
|
|
14
16
|
export default function Router({ context }) {
|
|
15
17
|
const isProduction = import.meta?.env?.MODE === 'production';
|
|
16
|
-
const loadTemplates = (templates) => templates?.map((pageTemplate) => ({
|
|
17
|
-
...pageTemplate,
|
|
18
|
-
component: lazy(pageTemplate.component),
|
|
19
|
-
})) || [];
|
|
20
18
|
if (!pages || !isProduction) {
|
|
21
|
-
pages =
|
|
19
|
+
pages = loadAsyncTemplates(context.templates?.pages);
|
|
22
20
|
}
|
|
23
21
|
if (!apps || !isProduction) {
|
|
24
|
-
apps =
|
|
22
|
+
apps = loadAsyncTemplates(context.templates?.apps);
|
|
25
23
|
}
|
|
26
24
|
if (!errors || !isProduction) {
|
|
27
|
-
errors =
|
|
25
|
+
errors = loadAsyncTemplates(context.templates?.errors);
|
|
28
26
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
if (!loaders || !isProduction) {
|
|
28
|
+
loaders = loadSyncTemplates(context.templates?.loaders);
|
|
29
|
+
}
|
|
30
|
+
const app = matchTemplate(apps, context.url.pathname);
|
|
31
|
+
const error = matchTemplate(errors, context.url.pathname);
|
|
32
|
+
const loader = matchTemplate(loaders, context.url.pathname);
|
|
35
33
|
const routes = [
|
|
36
34
|
...pages.map((page) => (_jsx(WouterRoute, { path: page.path, children: _jsx(Page, { context: context, page: page }) }, page.path))),
|
|
37
35
|
_jsx(WouterRoute, { children: error?.component && _jsx(Error, { context: context, error: error }) }, "error"),
|
|
38
36
|
];
|
|
39
37
|
const page = _jsx(WouterSwitch, { children: routes });
|
|
40
|
-
return (_jsx(WouterRouter, { ssrPath: context.url.pathname, children: _jsx(Suspense, { fallback: _jsx(
|
|
38
|
+
return (_jsx(WouterRouter, { ssrPath: context.url.pathname, children: _jsx(Suspense, { fallback: loader?.component && _jsx(loader.component, {}), children: createElement(app?.component, {
|
|
41
39
|
children: page,
|
|
42
40
|
}) }) }));
|
|
43
41
|
}
|
|
@@ -45,7 +43,7 @@ export function Page({ page, context }) {
|
|
|
45
43
|
context.statusCode = 200;
|
|
46
44
|
context.params = useParams();
|
|
47
45
|
if (typeof window !== 'undefined' && !context.isInitialPath) {
|
|
48
|
-
context.props =
|
|
46
|
+
context.props = useProps(context.url.pathname);
|
|
49
47
|
}
|
|
50
48
|
return _jsx(page.component, { ...context.props, context: context });
|
|
51
49
|
}
|
|
@@ -55,3 +53,39 @@ export function Error({ error, context }) {
|
|
|
55
53
|
context.props = {};
|
|
56
54
|
return _jsx(error.component, { context: context, message: 'Page was not found' });
|
|
57
55
|
}
|
|
56
|
+
function loadAsyncTemplates(templates) {
|
|
57
|
+
return (templates?.map((pageTemplate) => ({
|
|
58
|
+
...pageTemplate,
|
|
59
|
+
component: lazy(pageTemplate.component),
|
|
60
|
+
})) || []);
|
|
61
|
+
}
|
|
62
|
+
function loadSyncTemplates(templates) {
|
|
63
|
+
return (templates?.map((pageTemplate) => ({
|
|
64
|
+
...pageTemplate,
|
|
65
|
+
component: pageTemplate.component(),
|
|
66
|
+
})) || []);
|
|
67
|
+
}
|
|
68
|
+
function matchTemplate(template, path) {
|
|
69
|
+
return template.find(({ pathRegex }) => {
|
|
70
|
+
return pathRegex.test(path ?? '');
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
export function Link(props) {
|
|
74
|
+
const [isActive] = useRoute(props.href);
|
|
75
|
+
const href = props.href || props.to;
|
|
76
|
+
// Alias for Preact class names
|
|
77
|
+
props.activeClassName = props.activeClassName || props.activeClass;
|
|
78
|
+
props.className = props.className || props.class;
|
|
79
|
+
const className = `${props?.className} ${isActive ? props.activeClassName : ''}`;
|
|
80
|
+
// Preload the page
|
|
81
|
+
if (href) {
|
|
82
|
+
preloadPage(href, () => {
|
|
83
|
+
try {
|
|
84
|
+
matchTemplate(pages || [], href)?.component?.preload();
|
|
85
|
+
useProps(href, 'low');
|
|
86
|
+
}
|
|
87
|
+
catch (_e) { }
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
return (_jsx(WouterLink, { ...props, className: className, children: props.children }));
|
|
91
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function Loader(): import("preact").JSX.Element;
|