1
- import React , { Children , cloneElement , CSSProperties , isValidElement , MouseEvent , PropsWithChildren , useCallback , useEffect , useMemo , useRef } from 'https://esm.sh/react'
1
+ import React , { Children , cloneElement , CSSProperties , isValidElement , MouseEvent , PropsWithChildren , useCallback , useEffect , useMemo } from 'https://esm.sh/react'
2
2
import { redirect } from './aleph.ts'
3
+ import events from './events.ts'
3
4
import { useRouter } from './hooks.ts'
4
5
import util from './util.ts'
5
6
@@ -11,6 +12,8 @@ interface LinkProps {
11
12
style ?: CSSProperties
12
13
}
13
14
15
+ const fetchedPageModules = new Set < string > ( )
16
+
14
17
export default function Link ( {
15
18
to,
16
19
replace = false ,
@@ -19,15 +22,10 @@ export default function Link({
19
22
style,
20
23
children
21
24
} : PropsWithChildren < LinkProps > ) {
22
- const { pathname : currentPath , query : currentQuery } = useRouter ( )
25
+ const { pathname : currentPathname , query : currentQuery } = useRouter ( )
23
26
const currentHref = useMemo ( ( ) => {
24
- return [ currentPath , Object . entries ( currentQuery ) . map ( ( [ key , value ] ) => {
25
- if ( util . isArray ( value ) ) {
26
- return value . map ( v => `${ key } =${ v } ` ) . join ( '&' )
27
- }
28
- return `${ key } =${ value } `
29
- } ) . join ( '&' ) ] . filter ( Boolean ) . join ( '?' )
30
- } , [ currentPath , currentQuery ] )
27
+ return [ currentPathname , currentQuery . toString ( ) ] . filter ( Boolean ) . join ( '?' )
28
+ } , [ currentPathname , currentQuery ] )
31
29
const href = useMemo ( ( ) => {
32
30
if ( util . isHttpUrl ( to ) ) {
33
31
return to
@@ -36,15 +34,14 @@ export default function Link({
36
34
if ( pathname . startsWith ( '/' ) ) {
37
35
pathname = util . cleanPath ( pathname )
38
36
} else {
39
- pathname = util . cleanPath ( currentPath + '/' + pathname )
37
+ pathname = util . cleanPath ( currentPathname + '/' + pathname )
40
38
}
41
39
return [ pathname , search ] . filter ( Boolean ) . join ( '?' )
42
- } , [ currentPath , to ] )
43
- const prefetchStatus = useRef ( '' )
40
+ } , [ currentPathname , to ] )
44
41
const prefetch = useCallback ( ( ) => {
45
- if ( prefetchStatus . current != href && ! util . isHttpUrl ( href ) && href !== currentHref ) {
46
- prefetchStatus . current = href
47
- // prefetchPage (href)
42
+ if ( ! util . isHttpUrl ( href ) && href !== currentHref && ! fetchedPageModules . has ( href ) ) {
43
+ events . emit ( 'fetch-page-module' , { url : href } )
44
+ fetchedPageModules . add ( href )
48
45
}
49
46
} , [ href , currentHref ] )
50
47
const onClick = useCallback ( ( e : MouseEvent ) => {
0 commit comments