Skip to content

Commit 93d924b

Browse files
committed
fix(avif): COM-3172 set avif generation under the flag
1 parent 9e42379 commit 93d924b

4 files changed

Lines changed: 19 additions & 14 deletions

File tree

example/react-entry.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { PictureSmart } from '../src/react';
44
import { backgroundCssSmart } from '../src/utils/backgroundCss';
55
import { getOriginal } from '../src/utils';
66

7-
// Adds 'webp' class to html element if the browser supports webp.
7+
// Adds 'webp' or 'avif' class to html element if the browser supports it.
88
import '../src/utils/imgSupportDetector';
99

1010
const oneImageForAllBreakpoints = require.context('./images/oneImageForAllBreakpoints');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@csssr/csssr.images",
3-
"version": "3.3.2",
3+
"version": "3.3.3",
44
"description": "Utilities for Webpack and React to simplify images workflow",
55
"main": "dist/index.js",
66
"types": "dist",

src/utils/imgSupportDetector.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
function imgSupportDetector () {
44
const avif = new Image();
55
avif.src = "data:image/avif;base64,AAAAFGZ0eXBhdmlmAAAAAG1pZjEAAACgbWV0YQAAAAAAAAAOcGl0bQAAAAAAAQAAAB5pbG9jAAAAAEQAAAEAAQAAAAEAAAC8AAAAGwAAACNpaW5mAAAAAAABAAAAFWluZmUCAAAAAAEAAGF2MDEAAAAARWlwcnAAAAAoaXBjbwAAABRpc3BlAAAAAAAAAAQAAAAEAAAADGF2MUOBAAAAAAAAFWlwbWEAAAAAAAAAAQABAgECAAAAI21kYXQSAAoIP8R8hAQ0BUAyDWeeUy0JG+QAACANEkA=";
6-
76
avif.onload = () => { document.documentElement.classList.add("avif") };
87

9-
avif.onerror = () => {
10-
const webp = new Image();
11-
webp.src = "data:image/webp;base64,UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==";
12-
13-
webp.onload = () => { document.documentElement.classList.add("webp") };
14-
};
8+
// before we remove `generateAvif` flag, we want to detect webp support even if avif support is present
9+
const webp = new Image();
10+
webp.src = "data:image/webp;base64,UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==";
11+
webp.onload = () => { document.documentElement.classList.add("webp") };
1512
}
1613

1714
imgSupportDetector();

src/webpack/loader.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export type LoaderOptions = {
1919
disable: boolean;
2020
imagesHost: string;
2121
host: string;
22+
generateAvif: boolean;
2223
};
2324
originalPixelRatio: Dpr;
2425
};
@@ -98,10 +99,6 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
9899
order,
99100
breakpointMedia,
100101
srcSets: [
101-
{
102-
extension: 'avif',
103-
srcSet: avifSrcSet,
104-
},
105102
{
106103
extension: 'webp',
107104
srcSet: webpSrcSet,
@@ -112,9 +109,20 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
112109
},
113110
],
114111
};
112+
113+
if (options.imgproxy.generateAvif) {
114+
data.srcSets.unshift({
115+
extension: 'avif',
116+
srcSet: avifSrcSet,
117+
})
118+
119+
imageUrls.push(
120+
...(Object.values(avifSrcSet) as string[]),
121+
);
122+
}
123+
115124
// Add links to images via imgproxy to the global object
116125
imageUrls.push(
117-
...(Object.values(avifSrcSet) as string[]),
118126
...(Object.values(webpSrcSet) as string[]),
119127
...(Object.values(originalExtensionSrcSet) as string[]),
120128
);

0 commit comments

Comments
 (0)