Skip to content

Instantly share code, notes, and snippets.

@dane-stevens
Last active March 19, 2019 16:16
Show Gist options
  • Save dane-stevens/028051a315c7caea9e7027b047ecbb0f to your computer and use it in GitHub Desktop.
Save dane-stevens/028051a315c7caea9e7027b047ecbb0f to your computer and use it in GitHub Desktop.
class Img extends React.Component {
constructor(props) {
...
this.window = typeof window !== 'undefined' && window
this.isWebpSupported = this.isWebpSupported.bind(this)
}
...
isWebpSupported() {
if (!this.window.createImageBitmap) {
return false;
}
return true;
}
render() {
...
// If a format has not been specified, detect webp support
// Set the fm (format) option in the image URL
if (!options['fm'] && this.isWebpSupported) {
options['fm'] = 'webp'
}
...
return (
...
)
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment