@zubyjs/preact 1.0.47 → 1.0.48
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/components/lazyWithPreload.d.ts +8 -0
- package/components/lazyWithPreload.js +29 -0
- package/hooks/useProps.js +3 -1
- package/package.json +1 -1
- package/router.js +4 -3
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ComponentType } from 'preact';
|
|
2
|
+
export type PreloadableComponent<T extends ComponentType<any>> = T & {
|
|
3
|
+
preload: () => Promise<T>;
|
|
4
|
+
};
|
|
5
|
+
export declare function lazyWithPreload<T extends ComponentType<any>>(factory: () => Promise<{
|
|
6
|
+
default: T;
|
|
7
|
+
}>): PreloadableComponent<T>;
|
|
8
|
+
export default lazyWithPreload;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Original code from:
|
|
2
|
+
// https://github.com/ianschmitz/react-lazy-with-preload/blob/master/src/index.ts
|
|
3
|
+
import { createElement } from 'preact';
|
|
4
|
+
import { forwardRef, lazy } from 'preact/compat';
|
|
5
|
+
import { useRef } from 'preact/hooks';
|
|
6
|
+
export function lazyWithPreload(factory) {
|
|
7
|
+
const ReactLazyComponent = lazy(factory);
|
|
8
|
+
let PreloadedComponent;
|
|
9
|
+
let factoryPromise;
|
|
10
|
+
const Component = forwardRef(function LazyWithPreload(props, ref) {
|
|
11
|
+
// Once one of these is chosen, we must ensure that it continues to be
|
|
12
|
+
// used for all subsequent renders, otherwise it can cause the
|
|
13
|
+
// underlying component to be unmounted and remounted.
|
|
14
|
+
const ComponentToRender = useRef(PreloadedComponent ?? ReactLazyComponent);
|
|
15
|
+
return createElement(ComponentToRender.current, Object.assign(ref ? { ref } : {}, props));
|
|
16
|
+
});
|
|
17
|
+
const LazyWithPreload = Component;
|
|
18
|
+
LazyWithPreload.preload = () => {
|
|
19
|
+
if (!factoryPromise) {
|
|
20
|
+
factoryPromise = factory().then(module => {
|
|
21
|
+
PreloadedComponent = module.default;
|
|
22
|
+
return PreloadedComponent;
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return factoryPromise;
|
|
26
|
+
};
|
|
27
|
+
return LazyWithPreload;
|
|
28
|
+
}
|
|
29
|
+
export default lazyWithPreload;
|
package/hooks/useProps.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { useFetch } from './useFetch.js';
|
|
2
|
+
import { getContext } from 'zuby/context/index.js';
|
|
2
3
|
export default function useProps(path, priority = 'auto') {
|
|
3
|
-
|
|
4
|
+
const { buildId } = getContext();
|
|
5
|
+
path = `/_props${path}/?${buildId}`;
|
|
4
6
|
path = path.replace(/\/+/g, '/');
|
|
5
7
|
return useFetch(path);
|
|
6
8
|
}
|
package/package.json
CHANGED
package/router.js
CHANGED
|
@@ -2,10 +2,11 @@ import { jsx as _jsx } from "preact/jsx-runtime";
|
|
|
2
2
|
import { Router as WouterRouter, Route as WouterRoute, Switch as WouterSwitch, Link as WouterLink, useRoute, } from 'wouter-preact';
|
|
3
3
|
export { useRoute, useRouter, useParams, useLocation, Redirect, } from 'wouter-preact';
|
|
4
4
|
import { useParams } from 'wouter-preact';
|
|
5
|
-
import { Suspense
|
|
5
|
+
import { Suspense } from 'preact/compat';
|
|
6
6
|
import { createElement } from 'preact';
|
|
7
7
|
import { preloadPage } from 'zuby/preload/index.js';
|
|
8
|
-
import
|
|
8
|
+
import lazyWithPreload from './components/lazyWithPreload.js';
|
|
9
|
+
import useProps from './hooks/useProps.js';
|
|
9
10
|
let pages;
|
|
10
11
|
let apps;
|
|
11
12
|
let errors;
|
|
@@ -56,7 +57,7 @@ export function Error({ error, context }) {
|
|
|
56
57
|
function loadAsyncTemplates(templates) {
|
|
57
58
|
return (templates?.map((pageTemplate) => ({
|
|
58
59
|
...pageTemplate,
|
|
59
|
-
component:
|
|
60
|
+
component: lazyWithPreload(pageTemplate.component),
|
|
60
61
|
})) || []);
|
|
61
62
|
}
|
|
62
63
|
function loadSyncTemplates(templates) {
|