From 0a815255b9f226ec5ac032d6db5b151caa9c58fb Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 21 May 2020 17:58:32 -0400 Subject: [PATCH] fix: avoid unnecessary prefetches --- src/client/app/composables/preFetch.ts | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/client/app/composables/preFetch.ts b/src/client/app/composables/preFetch.ts index d8584b1c..59171bb4 100644 --- a/src/client/app/composables/preFetch.ts +++ b/src/client/app/composables/preFetch.ts @@ -73,8 +73,24 @@ export function usePrefetch() { rIC(() => { document.querySelectorAll('.vitepress-content a').forEach((link) => { - if ((link as HTMLAnchorElement).hostname === location.hostname) { - observer!.observe(link) + const { target, hostname, pathname } = link as HTMLAnchorElement + if ( + // only prefetch same page navigation, since a new page will load + // the lean js chunk instead. + target !== `_blank` && + // only prefetch inbound links + hostname === location.hostname + ) { + if (pathname !== location.pathname) { + observer!.observe(link) + } else { + // No need to prefetch chunk for the current page, but also mark + // it as already fetched. This is because the initial page uses its + // lean chunk, and if we don't mark it, navigation to another page + // with a link back to the first page will fetch its full chunk + // which isn't needed. + hasFetched.add(pathname) + } } }) })