From c96abcba1fe5fa4f26fb15a62b03ad6471f090a3 Mon Sep 17 00:00:00 2001 From: tex0l Date: Thu, 28 Sep 2023 15:46:20 +0200 Subject: [PATCH] Prefetch images Prefetch images referenced in prefetched html --- packages/integrations/prefetch/src/client.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/integrations/prefetch/src/client.ts b/packages/integrations/prefetch/src/client.ts index dc05cb84b..b669fb34b 100644 --- a/packages/integrations/prefetch/src/client.ts +++ b/packages/integrations/prefetch/src/client.ts @@ -6,6 +6,7 @@ const events = ['mouseenter', 'touchstart', 'focus']; const preloaded = new Set(); const loadedStyles = new Set(); +const loadedImages = new Set(); function shouldPreload({ href }: { href: string }) { try { @@ -52,7 +53,9 @@ async function preloadHref(link: HTMLAnchorElement) { const html = parser.parseFromString(contents, 'text/html'); const styles = Array.from(html.querySelectorAll('link[rel="stylesheet"]')); + const images = Array.from(html.querySelectorAll('img')); + await Promise.all( styles .filter((el) => !loadedStyles.has(el.href)) @@ -61,6 +64,15 @@ async function preloadHref(link: HTMLAnchorElement) { return fetch(el.href); }) ); + + await Promise.all( + images + .filter((el) => !loadedImages.has(el.src)) + .map((el) => { + loadedImages.add(el.src); + return fetch(el.src); + }) + ); } catch {} }