From 3502e968b832a3a172e6359feb66dfec026f42a2 Mon Sep 17 00:00:00 2001 From: Chris Millar Date: Fri, 1 Dec 2023 15:18:08 -0700 Subject: [PATCH] GH-299 Optimized Off Origin Images * Allow optimize image to use the supplied origin Resolves: GH-299 --- scripts/aem.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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`); } });