Skip to content

Commit

Permalink
GH-299 Optimized Off Origin Images
Browse files Browse the repository at this point in the history
* Allow optimize image to use the supplied origin

Resolves: GH-299
  • Loading branch information
auniverseaway committed Dec 1, 2023
1 parent efc8214 commit 3502e96
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand All @@ -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`);
}
});

Expand Down

0 comments on commit 3502e96

Please sign in to comment.