Prefetch images
Prefetch images referenced in prefetched html
This commit is contained in:
parent
0ab19ba615
commit
c96abcba1f
1 changed files with 12 additions and 0 deletions
|
@ -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 {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue