Wouldn’t using the <picture> element[0] allow this this? It’s pretty widely supported (every browser that supports WebP and Safari)[1] and allows a fallback to a JPEG in an <img> tag.
[0] https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pi...