Prefetch images

Prefetch images referenced in prefetched html
This commit is contained in:
tex0l 2023-09-28 15:46:20 +02:00 committed by GitHub
parent 0ab19ba615
commit c96abcba1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -6,6 +6,7 @@ const events = ['mouseenter', 'touchstart', 'focus'];
const preloaded = new Set<string>(); const preloaded = new Set<string>();
const loadedStyles = new Set<string>(); const loadedStyles = new Set<string>();
const loadedImages = new Set<string>();
function shouldPreload({ href }: { href: string }) { function shouldPreload({ href }: { href: string }) {
try { try {
@ -52,6 +53,8 @@ async function preloadHref(link: HTMLAnchorElement) {
const html = parser.parseFromString(contents, 'text/html'); const html = parser.parseFromString(contents, 'text/html');
const styles = Array.from(html.querySelectorAll<HTMLLinkElement>('link[rel="stylesheet"]')); const styles = Array.from(html.querySelectorAll<HTMLLinkElement>('link[rel="stylesheet"]'));
const images = Array.from(html.querySelectorAll<HTMLImageElement>('img'));
await Promise.all( await Promise.all(
styles styles
@ -61,6 +64,15 @@ async function preloadHref(link: HTMLAnchorElement) {
return fetch(el.href); 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 {} } catch {}
} }