diff --git a/packages/gatsby-image/src/__tests__/index.js b/packages/gatsby-image/src/__tests__/index.js
index 78809a8485a9e..d58bb7465eb86 100644
--- a/packages/gatsby-image/src/__tests__/index.js
+++ b/packages/gatsby-image/src/__tests__/index.js
@@ -168,7 +168,7 @@ describe(``, () => {
expect(component).toMatchSnapshot()
})
- it(`should have the the "critical" prop set "loading='eager'"`, () => {
+ it(`should have the "critical" prop set "loading='eager'"`, () => {
jest.spyOn(global.console, `log`)
const props = { critical: true }
@@ -177,6 +177,15 @@ describe(``, () => {
expect(console.log).toBeCalled()
})
+ it(`should not have the "loading" prop when "nativeLazyLoading" is set to false`, () => {
+ const props = { nativeLazyLoading: false }
+ const component = setup(false, props)
+ const imageTag = component.querySelector(`picture img`)
+ const noscriptTag = component.querySelector(`noscript`)
+ expect(imageTag.getAttribute(`loading`)).toEqual(null)
+ expect(noscriptTag.innerHTML).not.toContain(`loading`)
+ })
+
it(`should warn if image variants provided are missing media keys.`, () => {
jest.spyOn(global.console, `warn`)
diff --git a/packages/gatsby-image/src/index.js b/packages/gatsby-image/src/index.js
index faeae8520c8ed..f5dc0e19ae8ab 100644
--- a/packages/gatsby-image/src/index.js
+++ b/packages/gatsby-image/src/index.js
@@ -207,7 +207,10 @@ const noscriptImg = props => {
const crossOrigin = props.crossOrigin
? `crossorigin="${props.crossOrigin}" `
: ``
- const loading = props.loading ? `loading="${props.loading}" ` : ``
+ const loading =
+ props.loading && props.nativeLazyLoading
+ ? `loading="${props.loading}" `
+ : ``
const draggable = props.draggable ? `draggable="${props.draggable}" ` : ``
const sources = generateNoscriptSources(props.imageVariants)
@@ -232,31 +235,16 @@ const Placeholder = ({ src, imageVariants, generateSources, spreadProps }) => {
}
const Img = React.forwardRef((props, ref) => {
- const {
- sizes,
- srcSet,
- src,
- style,
- onLoad,
- onError,
- onClick,
- loading,
- draggable,
- ...otherProps
- } = props
+ const { style, nativeLazyLoading, ...otherProps } = props
+
+ if (!nativeLazyLoading) {
+ delete otherProps.loading
+ }
return (
)}
@@ -508,6 +501,7 @@ class Image extends React.Component {
alt,
title,
loading,
+ nativeLazyLoading,
...image,
imageVariants,
}),
@@ -597,6 +591,7 @@ class Image extends React.Component {
itemProp={itemProp}
loading={loading}
draggable={draggable}
+ nativeLazyLoading={nativeLazyLoading}
/>
)}
@@ -609,6 +604,7 @@ class Image extends React.Component {
alt,
title,
loading,
+ nativeLazyLoading,
...image,
imageVariants,
}),
@@ -631,6 +627,7 @@ Image.defaultProps = {
// We set it to `lazy` by default because it's best to default to a performant
// setting and let the user "opt out" to `eager`
loading: `lazy`,
+ nativeLazyLoading: true,
}
const fixedObject = PropTypes.shape({
@@ -681,6 +678,7 @@ Image.propTypes = {
Tag: PropTypes.string,
itemProp: PropTypes.string,
loading: PropTypes.oneOf([`auto`, `lazy`, `eager`]),
+ nativeLazyLoading: PropTypes.bool,
draggable: PropTypes.bool,
}