diff --git a/scripts/aem.js b/scripts/aem.js index af8f44a9e..b48b5b5cf 100644 --- a/scripts/aem.js +++ b/scripts/aem.js @@ -307,17 +307,17 @@ function createOptimizedPicture( eager = false, breakpoints = [{ media: '(min-width: 600px)', width: '2000' }, { width: '750' }], ) { - const url = new URL(src, window.location.href); + const url = !src.startsWith('http') ? new URL(src, window.location.href) : new URL(src); const picture = document.createElement('picture'); - const { pathname } = url; - const ext = pathname.substring(pathname.lastIndexOf('.') + 1); + const { origin, pathname } = url; + const ext = pathname.split('.').pop(); // webp breakpoints.forEach((br) => { const source = document.createElement('source'); if (br.media) source.setAttribute('media', br.media); source.setAttribute('type', 'image/webp'); - source.setAttribute('srcset', `${pathname}?width=${br.width}&format=webply&optimize=medium`); + source.setAttribute('srcset', `${origin}${pathname}?width=${br.width}&format=webply&optimize=medium`); picture.appendChild(source); }); @@ -326,14 +326,14 @@ function createOptimizedPicture( if (i < breakpoints.length - 1) { const source = document.createElement('source'); if (br.media) source.setAttribute('media', br.media); - source.setAttribute('srcset', `${pathname}?width=${br.width}&format=${ext}&optimize=medium`); + source.setAttribute('srcset', `${origin}${pathname}?width=${br.width}&format=${ext}&optimize=medium`); picture.appendChild(source); } else { const img = document.createElement('img'); img.setAttribute('loading', eager ? 'eager' : 'lazy'); img.setAttribute('alt', alt); picture.appendChild(img); - img.setAttribute('src', `${pathname}?width=${br.width}&format=${ext}&optimize=medium`); + img.setAttribute('src', `${origin}${pathname}?width=${br.width}&format=${ext}&optimize=medium`); } });