Skip to content

Commit 9e42379

Browse files
[COM-3172]: add avif support
1 parent 361e608 commit 9e42379

5 files changed

Lines changed: 34 additions & 18 deletions

File tree

example/react-entry.tsx

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

77
// Adds 'webp' class to html element if the browser supports webp.
8-
import '../src/utils/webpDetector';
8+
import '../src/utils/imgSupportDetector';
99

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

src/utils/backgroundCss.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ const breakpointMedia = (media: string | null): ((nestedCss: string) => string)
99
};
1010

1111
const getSelector = (selector: string, extension: string) => {
12-
return extension === 'webp' ? `html.webp ${selector}` : selector;
12+
if (extension === 'avif') {
13+
return `html.avif ${selector}`;
14+
}
15+
16+
if (extension === 'webp') {
17+
return `html.webp ${selector}`;
18+
}
19+
20+
return selector;
1321
};
1422

1523
const srcSetCss = (selector: string, sources: ExtensionSrcSet[]): string => {

src/utils/imgSupportDetector.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://avif.io/blog/tutorials/use-avif-in-css/
2+
3+
function imgSupportDetector () {
4+
const avif = new Image();
5+
avif.src = "data:image/avif;base64,AAAAFGZ0eXBhdmlmAAAAAG1pZjEAAACgbWV0YQAAAAAAAAAOcGl0bQAAAAAAAQAAAB5pbG9jAAAAAEQAAAEAAQAAAAEAAAC8AAAAGwAAACNpaW5mAAAAAAABAAAAFWluZmUCAAAAAAEAAGF2MDEAAAAARWlwcnAAAAAoaXBjbwAAABRpc3BlAAAAAAAAAAQAAAAEAAAADGF2MUOBAAAAAAAAFWlwbWEAAAAAAAAAAQABAgECAAAAI21kYXQSAAoIP8R8hAQ0BUAyDWeeUy0JG+QAACANEkA=";
6+
7+
avif.onload = () => { document.documentElement.classList.add("avif") };
8+
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+
};
15+
}
16+
17+
imgSupportDetector();

src/utils/webpDetector.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

src/webpack/loader.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
6868
(a, b, imagePath) => imagePath,
6969
);
7070

71-
let webpSrcSet: SrcSet, originalExtensionSrcSet: SrcSet, data: OrderedBreakpointSource;
71+
let avifSrcSet: SrcSet, webpSrcSet: SrcSet, originalExtensionSrcSet: SrcSet, data: OrderedBreakpointSource;
7272
// Disables picture processing, srcSet is generated only for the original image type
7373
if (options.imgproxy.disable) {
7474
data = {
@@ -86,6 +86,7 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
8686
};
8787
} else {
8888
const buildUrlsForPixelRatios = getImgproxyUrlBuilder(options.imgproxy);
89+
avifSrcSet = buildUrlsForPixelRatios(pixelRatios, outputImagePath, 'avif');
8990
webpSrcSet = buildUrlsForPixelRatios(pixelRatios, outputImagePath, 'webp');
9091
originalExtensionSrcSet = buildUrlsForPixelRatios(
9192
pixelRatios,
@@ -97,6 +98,10 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
9798
order,
9899
breakpointMedia,
99100
srcSets: [
101+
{
102+
extension: 'avif',
103+
srcSet: avifSrcSet,
104+
},
100105
{
101106
extension: 'webp',
102107
srcSet: webpSrcSet,
@@ -109,6 +114,7 @@ export const loader = function (this: webpack.loader.LoaderContext, source: stri
109114
};
110115
// Add links to images via imgproxy to the global object
111116
imageUrls.push(
117+
...(Object.values(avifSrcSet) as string[]),
112118
...(Object.values(webpSrcSet) as string[]),
113119
...(Object.values(originalExtensionSrcSet) as string[]),
114120
);

0 commit comments

Comments
 (0)