|
|
|
@ -10,6 +10,12 @@ import { inBrowser, pathToFile } from '../utils'
|
|
|
|
const hasFetched = new Set<string>()
|
|
|
|
const hasFetched = new Set<string>()
|
|
|
|
const createLink = () => document.createElement('link')
|
|
|
|
const createLink = () => document.createElement('link')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getLinkUrl = (link: HTMLAnchorElement | SVGAElement) =>
|
|
|
|
|
|
|
|
new URL(
|
|
|
|
|
|
|
|
link.href instanceof SVGAnimatedString ? link.href.animVal : link.href,
|
|
|
|
|
|
|
|
link.baseURI
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const viaDOM = (url: string) => {
|
|
|
|
const viaDOM = (url: string) => {
|
|
|
|
const link = createLink()
|
|
|
|
const link = createLink()
|
|
|
|
link.rel = `prefetch`
|
|
|
|
link.rel = `prefetch`
|
|
|
|
@ -64,9 +70,9 @@ export function usePrefetch() {
|
|
|
|
observer = new IntersectionObserver((entries) => {
|
|
|
|
observer = new IntersectionObserver((entries) => {
|
|
|
|
entries.forEach((entry) => {
|
|
|
|
entries.forEach((entry) => {
|
|
|
|
if (entry.isIntersecting) {
|
|
|
|
if (entry.isIntersecting) {
|
|
|
|
const link = entry.target as HTMLAnchorElement
|
|
|
|
const link = entry.target as HTMLAnchorElement | SVGAElement
|
|
|
|
observer!.unobserve(link)
|
|
|
|
observer!.unobserve(link)
|
|
|
|
const { pathname } = link
|
|
|
|
const { pathname } = getLinkUrl(link)
|
|
|
|
if (!hasFetched.has(pathname)) {
|
|
|
|
if (!hasFetched.has(pathname)) {
|
|
|
|
hasFetched.add(pathname)
|
|
|
|
hasFetched.add(pathname)
|
|
|
|
const pageChunkPath = pathToFile(pathname)
|
|
|
|
const pageChunkPath = pathToFile(pathname)
|
|
|
|
@ -80,12 +86,7 @@ export function usePrefetch() {
|
|
|
|
document
|
|
|
|
document
|
|
|
|
.querySelectorAll<HTMLAnchorElement | SVGAElement>('#app a')
|
|
|
|
.querySelectorAll<HTMLAnchorElement | SVGAElement>('#app a')
|
|
|
|
.forEach((link) => {
|
|
|
|
.forEach((link) => {
|
|
|
|
const { hostname, pathname } = new URL(
|
|
|
|
const { hostname, pathname } = getLinkUrl(link)
|
|
|
|
link.href instanceof SVGAnimatedString
|
|
|
|
|
|
|
|
? link.href.animVal
|
|
|
|
|
|
|
|
: link.href,
|
|
|
|
|
|
|
|
link.baseURI
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
const extMatch = pathname.match(/\.\w+$/)
|
|
|
|
const extMatch = pathname.match(/\.\w+$/)
|
|
|
|
if (extMatch && extMatch[0] !== '.html') {
|
|
|
|
if (extMatch && extMatch[0] !== '.html') {
|
|
|
|
return
|
|
|
|
return
|
|
|
|
@ -94,7 +95,7 @@ export function usePrefetch() {
|
|
|
|
if (
|
|
|
|
if (
|
|
|
|
// only prefetch same tab navigation, since a new tab will load
|
|
|
|
// only prefetch same tab navigation, since a new tab will load
|
|
|
|
// the lean js chunk instead.
|
|
|
|
// the lean js chunk instead.
|
|
|
|
link.target !== '_blank' &&
|
|
|
|
link.getAttribute('target') !== '_blank' &&
|
|
|
|
// only prefetch inbound links
|
|
|
|
// only prefetch inbound links
|
|
|
|
hostname === location.hostname
|
|
|
|
hostname === location.hostname
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
|