Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/_lib/public/ui/autosizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* SOFTWARE.
*
* Algorithm:
* 1. polyfillAutoSizes(): Uses UA sniffing to detect Chrome < 126 (no native support).
* 1. polyfillAutoSizes(): Uses UA sniffing to detect browsers without native support.
* Avoids polyfilling if browser is too old (no PerformanceObserver/paint timing).
* 2. Before FCP: Store src/srcset in data attributes and remove originals to prevent loading.
* Also strips srcset from <source> elements inside <picture> to prevent bypass.
Expand All @@ -43,12 +43,19 @@

const chromeMatch = navigator.userAgent.match(/Chrome\/(\d+)/);

if (!chromeMatch) {
if (chromeMatch) {
const chromeVersion = Number.parseInt(chromeMatch[1], 10);
return chromeVersion < 126;
}

const firefoxMatch = navigator.userAgent.match(/Firefox\/(\d+)/);

if (!firefoxMatch) {
return true;
}

const chromeVersion = Number.parseInt(chromeMatch[1], 10);
return chromeVersion < 126;
const firefoxVersion = Number.parseInt(firefoxMatch[1], 10);
return firefoxVersion < 150;
};

if (!polyfillAutoSizes()) {
Expand Down
2 changes: 1 addition & 1 deletion test/code-quality/code-quality-exceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ const ALLOWED_NULLISH_COALESCING = frozenSet([
// src/_lib/public - frontend JavaScript (browser-side, no collections)
"src/_lib/public/cart/cart.js:86",
"src/_lib/public/cart/cart.js:87",
"src/_lib/public/ui/autosizes.js:76",
"src/_lib/public/ui/autosizes.js:83",

// src/_lib/utils - utility functions
"src/_lib/utils/collection-utils.js:71", // indexer may not contain the lookup slug
Expand Down
24 changes: 21 additions & 3 deletions test/unit/build/autosizes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,23 @@ describe("autosizes", () => {
expect(img.hasAttribute("data-auto-sizes-src")).toBe(true);
});

test("Runs polyfill for non-Chrome browsers (Firefox, Safari)", async () => {
test("Does not run polyfill for Firefox 150+", async () => {
const { window, img } = await createAutosizesTestEnv({
userAgent: "Mozilla/5.0 Firefox/120",
userAgent: "Mozilla/5.0 Firefox/150",
});
runAndExpectSrc(window, img, true);
});

test("Runs polyfill for Firefox 149 (older than 150)", async () => {
const { window, img } = await createAutosizesTestEnv({
userAgent: "Mozilla/5.0 Firefox/149",
});
runAndExpectSrc(window, img, false);
});

test("Runs polyfill for Safari", async () => {
const { window, img } = await createAutosizesTestEnv({
userAgent: "Mozilla/5.0 Version/18.5 Safari/605.1.15",
});
runAndExpectSrc(window, img, false);
});
Expand Down Expand Up @@ -236,7 +250,11 @@ describe("autosizes", () => {

test("Processes local images with relative paths", async () => {
const { window, img } = await createAutosizesTestEnv({
imgAttrs: { src: "/images/photo.jpg", sizes: "auto", loading: "lazy" },
imgAttrs: {
src: "/images/photo.jpg",
sizes: "auto",
loading: "lazy",
},
});
runAndCheckDeferred(window, img, "/images/photo.jpg");
});
Expand Down
Loading