Skip to content

Commit 0fc9534

Browse files
jdcataldoGatsbyJS Bot
authored and
GatsbyJS Bot
committed
feat(gatsby-remark-images-contentful): add native lazy loading support to contentful images (#18883)
1 parent d65183b commit 0fc9534

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

packages/gatsby-remark-images-contentful/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ plugins: [
5353
| `wrapperStyle` | | Add custom styles to the div wrapping the responsive images. Use regular CSS syntax, e.g. `margin-bottom:10px; background: red;` |
5454
| `backgroundColor` | `white` | Set the background color of the image to match the background of your design |
5555
| `withWebp` | `false` | Additionally generate WebP versions alongside your chosen file format. They are added as a srcset with the appropriate mimetype and will be loaded in browsers that support the format. |
56+
| `loading` | `lazy` | Set the browser's native lazy loading attribute. One of `lazy`, `eager` or `auto`. |
5657

5758
## Troubleshooting
5859

packages/gatsby-remark-images-contentful/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"dependencies": {
1818
"@babel/runtime": "^7.7.2",
1919
"axios": "^0.19.0",
20+
"chalk": "^2.4.2",
2021
"cheerio": "^1.0.0-rc.3",
2122
"is-relative-url": "^3.0.0",
2223
"lodash": "^4.17.15",

packages/gatsby-remark-images-contentful/src/__tests__/__snapshots__/index.js.snap

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`it transforms HTML img tags 1`] = `
44
"<a class=\\"gatsby-resp-image-link\\" href=\\"https://images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/quwowooybuqbl6ntboz3.jpg\\" style=\\"display: block\\" target=\\"_blank\\" rel=\\"noopener\\">
55
<span class=\\"gatsby-resp-image-wrapper\\" style=\\"position: relative; display: block; ; max-width: 600px; margin-left: auto; margin-right: auto;\\">
66
<span class=\\"gatsby-resp-image-background-image\\" style=\\"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image;&apos;); background-size: cover; display: block;\\">
7-
<img class=\\"gatsby-resp-image-image\\" style=\\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\\" alt=\\"quwowooybuqbl6ntboz3\\" title src=\\"https://images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/quwowooybuqbl6ntboz3.jpg\\" srcset=\\"srcSet\\" sizes=\\"128px,250px\\">
7+
<img class=\\"gatsby-resp-image-image\\" style=\\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\\" alt=\\"quwowooybuqbl6ntboz3\\" title src=\\"https://images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/quwowooybuqbl6ntboz3.jpg\\" srcset=\\"srcSet\\" sizes=\\"128px,250px\\" loading=\\"lazy\\">
88
</span>
99
</span>
1010
</a>"
@@ -34,6 +34,7 @@ exports[`it transforms images in markdown 1`] = `
3434
src=\\"https://images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/quwowooybuqbl6ntboz3.jpg\\"
3535
srcset=\\"srcSet\\"
3636
sizes=\\"128px,250px\\"
37+
loading=\\"lazy\\"
3738
/>
3839
</span>
3940
</span>
@@ -72,6 +73,7 @@ exports[`it transforms images in markdown with webp srcSets if option is enabled
7273
alt=\\"image\\"
7374
title=\\"\\"
7475
src=\\"https://images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/quwowooybuqbl6ntboz3.jpg\\"
76+
loading=\\"lazy\\"
7577
/>
7678
</picture>
7779
</span>
@@ -103,6 +105,7 @@ exports[`it transforms images with a https scheme in markdown 1`] = `
103105
src=\\"https://images.ctfassets.net/rocybtov1ozk/wtrHxeu3zEoEce2MokCSi/73dce36715f16e27cf5ff0d2d97d7dff/quwowooybuqbl6ntboz3.jpg\\"
104106
srcset=\\"srcSet\\"
105107
sizes=\\"128px,250px\\"
108+
loading=\\"lazy\\"
106109
/>
107110
</span>
108111
</span>

packages/gatsby-remark-images-contentful/src/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const axios = require(`axios`)
44
const _ = require(`lodash`)
55
const Promise = require(`bluebird`)
66
const cheerio = require(`cheerio`)
7+
const chalk = require(`chalk`)
78
const { buildResponsiveSizes } = require(`./utils`)
89

910
// If the image is hosted on contentful
@@ -34,6 +35,7 @@ module.exports = async (
3435
showCaptions: false,
3536
pathPrefix,
3637
withWebp: false,
38+
loading: `lazy`,
3739
}
3840

3941
// This will only work for markdown syntax image tags
@@ -97,6 +99,18 @@ module.exports = async (
9799
const fileNameNoExt = fileName.replace(/\.[^/.]+$/, ``)
98100
const defaultAlt = fileNameNoExt.replace(/[^A-Z0-9]/gi, ` `)
99101

102+
const loading = options.loading
103+
104+
if (![`lazy`, `eager`, `auto`].includes(loading)) {
105+
reporter.warn(
106+
reporter.stripIndent(`
107+
${chalk.bold(loading)} is an invalid value for the ${chalk.bold(
108+
`loading`
109+
)} option. Please pass one of "lazy", "eager" or "auto".
110+
`)
111+
)
112+
}
113+
100114
// Create our base image tag
101115
let imageTag = `
102116
<img
@@ -109,6 +123,7 @@ module.exports = async (
109123
src="${fallbackSrc}"
110124
srcset="${srcSet}"
111125
sizes="${responsiveSizesResult.sizes}"
126+
loading="${loading}"
112127
/>
113128
`.trim()
114129

@@ -133,6 +148,7 @@ module.exports = async (
133148
alt="${node.alt ? node.alt : defaultAlt}"
134149
title="${node.title ? node.title : ``}"
135150
src="${fallbackSrc}"
151+
loading="${loading}"
136152
/>
137153
</picture>
138154
`.trim()

0 commit comments

Comments
 (0)