// A picture fetched before it is drawn, so it shows at once when it is: the // next stories, a carousel's next photo. Called from an effect alone, in a // browser, never as a component draws, so a server draws nothing of it and // the browser's first drawing stays the server's. Each is held only until // it loads or fails; the browser's cache keeps it from there, and the // `` that draws it later, asking for the same source (and the same // `srcset` and `sizes`), takes it from there. const loading = new Map(); /** Fetches `src` (or of `srcset`, the size `sizes` asks) ahead of its drawing; one already on its way is left to it. */ export function preload(src: string, srcset?: string, sizes?: string): void { const key = srcset ? `${srcset} ${sizes ?? ''}` : src; if (loading.has(key)) return; const image = new Image(); image.decoding = 'async'; const done = () => void loading.delete(key); image.onload = image.onerror = done; if (srcset) { if (sizes) image.sizes = sizes; image.srcset = srcset; } image.src = src; loading.set(key, image); }