Skip to content

Commit

Permalink
fix(core): skip preloading images from <picture> tags
Browse files Browse the repository at this point in the history
  • Loading branch information
johannschopplich committed Feb 18, 2024
1 parent 634fb6b commit 2f87c35
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/core/src/lazyLoad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ export function loadImage(
image: HTMLImageElement,
onImageLoad?: (image: HTMLImageElement) => void,
) {
const isChildOfPictureElement = image.parentElement?.tagName.toLowerCase() === 'picture'

// Skip preloading its `data-src` or `data-srcset` to avoid unnecessary requests
if (isChildOfPictureElement) {
updatePictureSources(image)
updateImageSrcset(image)
updateImageSrc(image)
onImageLoad?.(image)
return
}

const imagePreLoader = new Image()
const { srcset, src, sizes } = image.dataset

Expand All @@ -114,9 +125,6 @@ export function loadImage(
imagePreLoader.sizes = image.sizes
}

// Update sources to prevent duplicate downloads
updatePictureSources(image)

if (srcset)
imagePreLoader.srcset = srcset
if (src)
Expand Down

0 comments on commit 2f87c35

Please sign in to comment.