Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit d0761a9

Browse files
author
Je
committed
feat: prefetch page module when mouse over a Link
1 parent aa14e4b commit d0761a9

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

aleph.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ export function ALEPH({ initial }: {
8989
}, [onpopstate])
9090

9191
useEffect(() => {
92-
const { routing: { baseUrl } } = ref.current
92+
const { routing } = ref.current
93+
const { baseUrl } = routing
9394
const onUpdateData = (data: any) => {
9495
console.log('[DATA]', data)
9596
setStaticData(data)
@@ -149,15 +150,32 @@ export function ALEPH({ initial }: {
149150
break
150151
}
151152
}
153+
const onFetchPageModule = async ({ url: pathname }: { url: string }) => {
154+
const [url, pageModuleTree] = routing.createRouter({ pathname })
155+
if (url.pagePath !== '') {
156+
const imports = pageModuleTree.map(async mod => {
157+
await import(getModuleImportUrl(baseUrl, mod))
158+
if (mod.asyncDeps) {
159+
// import async dependencies
160+
for (const dep of mod.asyncDeps) {
161+
await import(getModuleImportUrl(baseUrl, { id: dep.url.replace(reModuleExt, '.js'), hash: dep.hash }))
162+
}
163+
}
164+
})
165+
await Promise.all(imports)
166+
}
167+
}
152168

153169
events.on('update-data', onUpdateData)
154170
events.on('add-module', onAddModule)
155171
events.on('remove-module', onRemoveModule)
172+
events.on('fetch-page-module', onFetchPageModule)
156173

157174
return () => {
158175
events.off('update-data', onUpdateData)
159176
events.off('add-module', onAddModule)
160177
events.off('remove-module', onRemoveModule)
178+
events.off('fetch-page-module', onFetchPageModule)
161179
}
162180
}, [ref])
163181

link.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
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'
22
import { redirect } from './aleph.ts'
3+
import events from './events.ts'
34
import { useRouter } from './hooks.ts'
45
import util from './util.ts'
56

@@ -11,6 +12,8 @@ interface LinkProps {
1112
style?: CSSProperties
1213
}
1314

15+
const fetchedPageModules = new Set<string>()
16+
1417
export default function Link({
1518
to,
1619
replace = false,
@@ -19,15 +22,10 @@ export default function Link({
1922
style,
2023
children
2124
}: PropsWithChildren<LinkProps>) {
22-
const { pathname: currentPath, query: currentQuery } = useRouter()
25+
const { pathname: currentPathname, query: currentQuery } = useRouter()
2326
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])
3129
const href = useMemo(() => {
3230
if (util.isHttpUrl(to)) {
3331
return to
@@ -36,15 +34,14 @@ export default function Link({
3634
if (pathname.startsWith('/')) {
3735
pathname = util.cleanPath(pathname)
3836
} else {
39-
pathname = util.cleanPath(currentPath + '/' + pathname)
37+
pathname = util.cleanPath(currentPathname + '/' + pathname)
4038
}
4139
return [pathname, search].filter(Boolean).join('?')
42-
}, [currentPath, to])
43-
const prefetchStatus = useRef('')
40+
}, [currentPathname, to])
4441
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)
4845
}
4946
}, [href, currentHref])
5047
const onClick = useCallback((e: MouseEvent) => {

0 commit comments

Comments
 (0)