Skip to content

Commit d3d9020

Browse files
dsewnrpieh
authored andcommitted
feat(gatsby-remark-images): add disableBgImage option (#19152)
* Add disableBgImage option for gatsby-remark-images package to prevent "Stylesheet too long" error on AMP * Add disableBgImage option for gatsby-remark-images package to prevent "Stylesheet too long" error on AMP * Test disableBgImage * Update gatsby-remark-images snapshot * Update packages/gatsby-remark-images/README.md Co-Authored-By: Michal Piechowiak <[email protected]> * chore: format * fix snapshot
1 parent 992f841 commit d3d9020

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

packages/gatsby-remark-images/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ plugins: [
5858
| `tracedSVG` | `false` | Use traced SVGs for placeholder images instead of the "blur up" effect. Pass `true` for traced SVGs with the default settings (seen [here][3]), or an object of options to override the defaults. For example, pass `{ color: "#F00", turnPolicy: "TURNPOLICY_MAJORITY" }` to change the color of the trace to red and the turn policy to TURNPOLICY_MAJORITY. See [`node-potrace` parameter documentation][4] for a full listing and explanation of the available options. |
5959
| `loading` | `lazy` | Set the browser's native lazy loading attribute. One of `lazy`, `eager` or `auto`. |
6060
| `disableBgImageOnAlpha` | `false` | Images containing transparent pixels around the edges results in images with blurry edges. As a result, these images do not work well with the "blur up" technique used in this plugin. As a workaround to disable background images with blurry edges on images containing transparent pixels, enable this setting. |
61+
| `disableBgImage` | `false` | Remove background image and its' inline style. Useful to prevent `Stylesheet too long` error on AMP. |
6162

6263
## dynamic wrapperStyle example
6364

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`disableBgImage disables background image when disableBgImage === true 1`] = `
4+
"<span
5+
class=\\"gatsby-resp-image-wrapper\\"
6+
style=\\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px;\\"
7+
>
8+
<a
9+
class=\\"gatsby-resp-image-link\\"
10+
href=\\"not-a-real-dir/images/my-image.jpeg\\"
11+
style=\\"display: block\\"
12+
target=\\"_blank\\"
13+
rel=\\"noopener\\"
14+
>
15+
<span
16+
class=\\"gatsby-resp-image-background-image\\"
17+
style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; display: block;\\"
18+
></span>
19+
<img
20+
class=\\"gatsby-resp-image-image\\"
21+
alt=\\"some alt\\"
22+
title=\\"some title\\"
23+
src=\\"not-a-real-dir/images/my-image.jpeg\\"
24+
srcset=\\"not-a-real-dir/images/my-image.jpeg, not-a-real-dir/images/my-image.jpeg\\"
25+
sizes=\\"(max-width: 650px) 100vw, 650px\\"
26+
loading=\\"lazy\\"
27+
/>
28+
</a>
29+
</span>"
30+
`;
31+
32+
exports[`disableBgImage does not disable background image when disableBgImage === false 1`] = `
33+
"<span
34+
class=\\"gatsby-resp-image-wrapper\\"
35+
style=\\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 300px;\\"
36+
>
37+
<a
38+
class=\\"gatsby-resp-image-link\\"
39+
href=\\"not-a-real-dir/images/my-image.jpeg\\"
40+
style=\\"display: block\\"
41+
target=\\"_blank\\"
42+
rel=\\"noopener\\"
43+
>
44+
<span
45+
class=\\"gatsby-resp-image-background-image\\"
46+
style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw'); background-size: cover; display: block;\\"
47+
></span>
48+
<img
49+
class=\\"gatsby-resp-image-image\\"
50+
alt=\\"some alt\\"
51+
title=\\"some title\\"
52+
src=\\"not-a-real-dir/images/my-image.jpeg\\"
53+
srcset=\\"not-a-real-dir/images/my-image.jpeg, not-a-real-dir/images/my-image.jpeg\\"
54+
sizes=\\"(max-width: 650px) 100vw, 650px\\"
55+
loading=\\"lazy\\"
56+
/>
57+
</a>
58+
</span>"
59+
`;
60+
361
exports[`disableBgImageOnAlpha disables background image on transparent images when disableBgImageOnAlpha === true 1`] = `
462
"<span
563
class=\\"gatsby-resp-image-wrapper\\"

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,33 @@ describe(`disableBgImageOnAlpha`, () => {
622622
expect(node.value).toMatchSnapshot()
623623
})
624624
})
625+
626+
describe(`disableBgImage`, () => {
627+
it(`does not disable background image when disableBgImage === false`, async () => {
628+
const imagePath = `images/my-image.jpeg`
629+
const content = `![some alt](./${imagePath} "some title")`
630+
631+
const nodes = await plugin(createPluginOptions(content, imagePath), {
632+
disableBgImage: false,
633+
})
634+
expect(nodes.length).toBe(1)
635+
636+
const node = nodes.pop()
637+
expect(node.type).toBe(`html`)
638+
expect(node.value).toMatchSnapshot()
639+
})
640+
641+
it(`disables background image when disableBgImage === true`, async () => {
642+
const imagePath = `images/my-image.jpeg`
643+
const content = `![some alt](./${imagePath} "some title")`
644+
645+
const nodes = await plugin(createPluginOptions(content, imagePath), {
646+
disableBgImage: true,
647+
})
648+
expect(nodes.length).toBe(1)
649+
650+
const node = nodes.pop()
651+
expect(node.type).toBe(`html`)
652+
expect(node.value).toMatchSnapshot()
653+
})
654+
})

packages/gatsby-remark-images/src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ exports.DEFAULT_OPTIONS = {
99
tracedSVG: false,
1010
loading: `lazy`,
1111
disableBgImageOnAlpha: false,
12+
disableBgImage: false,
1213
}
1314

1415
exports.imageClass = `gatsby-resp-image-image`

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ module.exports = (
287287
const imageStats = await stats({ file: imageNode, reporter })
288288
if (imageStats && imageStats.isTransparent) removeBgImage = true
289289
}
290+
if (options.disableBgImage) {
291+
removeBgImage = true
292+
}
290293

291294
const bgImage = removeBgImage
292295
? ``

0 commit comments

Comments
 (0)