File tree Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Expand file tree Collapse file tree 2 files changed +16
-3
lines changed Original file line number Diff line number Diff line change 1
1
import { ComponentChildren , VNode } from 'preact' ;
2
2
3
- export default function lazy < T > ( load : ( ) => Promise < { default : T } | T > ) : T ;
3
+ export default function lazy < T > ( load : ( ) => Promise < { default : T } | T > ) : T & {
4
+ preload : ( ) => Promise < T > ;
5
+ } ;
4
6
5
7
export function ErrorBoundary ( props : { children ?: ComponentChildren ; onError ?: ( error : Error ) => void } ) : VNode ;
Original file line number Diff line number Diff line change @@ -3,14 +3,25 @@ import { useState, useRef } from 'preact/hooks';
3
3
4
4
export default function lazy ( load ) {
5
5
let p , c ;
6
- return props => {
6
+
7
+ const loadModule = ( ) =>
8
+ load ( ) . then ( m => ( c = ( m && m . default ) || m ) ) ;
9
+
10
+ const LazyComponent = props => {
7
11
const [ , update ] = useState ( 0 ) ;
8
12
const r = useRef ( c ) ;
9
- if ( ! p ) p = load ( ) . then ( m => ( c = ( m && m . default ) || m ) ) ;
13
+ if ( ! p ) p = loadModule ( ) ;
10
14
if ( c !== undefined ) return h ( c , props ) ;
11
15
if ( ! r . current ) r . current = p . then ( ( ) => update ( 1 ) ) ;
12
16
throw p ;
13
17
} ;
18
+
19
+ LazyComponent . preload = ( ) => {
20
+ if ( ! p ) p = loadModule ( ) ;
21
+ return p ;
22
+ }
23
+
24
+ return LazyComponent ;
14
25
}
15
26
16
27
// See https://github.com/preactjs/preact/blob/88680e91ec0d5fc29d38554a3e122b10824636b6/compat/src/suspense.js#L5
You can’t perform that action at this time.
0 commit comments