Skip to content

Commit 8ff9cc3

Browse files
SwarleysEsteban Ramos Coronadoaxe312ger
authored
feat(contentful): modifying schemes to add support for AVIF images (#33903)
* feat(contentful): modifying schemes to add support for AVIF images * add avif to the missing parts of the image processing code * extend e2e tests to check for custom image formats Co-authored-by: Esteban Ramos Coronado <[email protected]> Co-authored-by: axe312ger <[email protected]>
1 parent 150f086 commit 8ff9cc3

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

e2e-tests/contentful/cypress/integration/gatsby-plugin-image.js

+12
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ describe(`gatsby-plugin-image`, () => {
7979
it(`blurred`, testConfig, () =>
8080
testGatsbyPluginImage(`blurred`, hasBase64Placeholder)
8181
)
82+
it(`Custom Image Formats`, testConfig, () => {
83+
cy.get(`[data-cy="customImageFormats"] picture source[type="image/webp"]`)
84+
.invoke(`attr`, `srcset`)
85+
.should("contain", "fm=webp")
86+
cy.get(`[data-cy="customImageFormats"] picture source[type="image/avif"]`)
87+
.invoke(`attr`, `srcset`)
88+
.should("contain", "fm=avif")
89+
cy.get(`[data-cy="customImageFormats"] picture img`)
90+
.invoke(`attr`, `srcset`)
91+
.should("not.contain", "fm=webp")
92+
.should("not.contain", "fm=avif")
93+
})
8294
it(`sqip`, testConfig, () => testGatsbyPluginImage(`sqip`, hasSVGPlaceholder))
8395

8496
it(`english`, testConfig, () => {

e2e-tests/contentful/src/pages/gatsby-plugin-image.js

+20
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ const GatsbyPluginImagePage = ({ data }) => {
123123
))}
124124
</Grid>
125125

126+
<h2>gatsby-plugin-image: Custom Image Formats (WebP & AVIF)</h2>
127+
<Grid data-cy="customImageFormats">
128+
{data.default.nodes.map(node => (
129+
<div>
130+
<p>
131+
<strong>
132+
{node.title} ({node.file.fileName.split(".").pop()})
133+
</strong>
134+
</p>
135+
{node.description && <p>{node.description}</p>}
136+
{node.customImageFormats ? (
137+
<GatsbyImage image={node.customImageFormats} />
138+
) : (
139+
<SvgImage src={node.file.url} />
140+
)}
141+
</div>
142+
))}
143+
</Grid>
144+
126145
<h2>gatsby-plugin-image: SQIP Placeholder</h2>
127146
<Grid data-cy="sqip">
128147
{data.default.nodes.map(node => (
@@ -232,6 +251,7 @@ export const pageQuery = graphql`
232251
layout: FIXED
233252
placeholder: BLURRED
234253
)
254+
customImageFormats: gatsbyImageData(formats: [AVIF, WEBP, AUTO])
235255
sqip(numberOfPrimitives: 12, blur: 0, mode: 1) {
236256
dataURI
237257
}

packages/gatsby-source-contentful/src/gatsby-plugin-image.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ export function generateImageSource(
217217

218218
if (!validImageFormats.has(toFormat)) {
219219
console.warn(
220-
`[gatsby-source-contentful] Invalid image format "${toFormat}". Supported types are jpg, png and webp"`
220+
`[gatsby-source-contentful] Invalid image format "${toFormat}". Supported types are jpg, png, webp and avif"`
221221
)
222222
return undefined
223223
}

packages/gatsby-source-contentful/src/image-helpers.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { URLSearchParams } from "url"
66
export const CONTENTFUL_IMAGE_MAX_SIZE = 4000
77

88
// Supported Image Formats by the Contentful Image API (https://www.contentful.com/developers/docs/references/images-api/#/reference/changing-formats/image-format)
9-
export const validImageFormats = new Set([`jpg`, `png`, `webp`, `gif`])
9+
export const validImageFormats = new Set([`jpg`, `png`, `webp`, `gif`, `avif`])
1010

1111
// Determine the proper file extension based on mime type
1212
export const mimeTypeExtensions = new Map([
@@ -15,6 +15,7 @@ export const mimeTypeExtensions = new Map([
1515
[`image/gif`, `.gif`],
1616
[`image/png`, `.png`],
1717
[`image/webp`, `.webp`],
18+
[`image/avif`, `.avif`],
1819
])
1920

2021
// Check if Contentful asset is actually an image

packages/gatsby-source-contentful/src/schemes.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const ImageFormatType = new GraphQLEnumType({
99
JPG: { value: `jpg` },
1010
PNG: { value: `png` },
1111
WEBP: { value: `webp` },
12+
AVIF: { value: `avif` },
1213
},
1314
})
1415

0 commit comments

Comments
 (0)