How can I detect if the browser supports AVIF images using JavaScript? I have viewed this question and after reading the answers I was able to build a useful one-liner function to check browser support for various image types.
const isSupported = (type) => document.createElement('canvas').toDataURL(`image/${type}`).indexOf(`data:image/${type}`) === 0;
And it works fine for webp (and jpeg & png) images. But it's not working for avif (and gif) images.
By the word Not working, I mean the function is returning false even if the browser supports AVIF images.
So, what is wrong with this approach? And what is the correct way to detect browser support for avif images using JavaScript?