Skip to content

Commit

Permalink
feat(contentful): modifying schemes to add support for AVIF images (#…
Browse files Browse the repository at this point in the history
…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 <esteban@Mac-mini-de-Esteban.local>
Co-authored-by: axe312ger <opensource@axe312.dev>
  • Loading branch information
3 people authored Nov 11, 2021
1 parent 150f086 commit 8ff9cc3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
12 changes: 12 additions & 0 deletions e2e-tests/contentful/cypress/integration/gatsby-plugin-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ describe(`gatsby-plugin-image`, () => {
it(`blurred`, testConfig, () =>
testGatsbyPluginImage(`blurred`, hasBase64Placeholder)
)
it(`Custom Image Formats`, testConfig, () => {
cy.get(`[data-cy="customImageFormats"] picture source[type="image/webp"]`)
.invoke(`attr`, `srcset`)
.should("contain", "fm=webp")
cy.get(`[data-cy="customImageFormats"] picture source[type="image/avif"]`)
.invoke(`attr`, `srcset`)
.should("contain", "fm=avif")
cy.get(`[data-cy="customImageFormats"] picture img`)
.invoke(`attr`, `srcset`)
.should("not.contain", "fm=webp")
.should("not.contain", "fm=avif")
})
it(`sqip`, testConfig, () => testGatsbyPluginImage(`sqip`, hasSVGPlaceholder))

it(`english`, testConfig, () => {
Expand Down
20 changes: 20 additions & 0 deletions e2e-tests/contentful/src/pages/gatsby-plugin-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ const GatsbyPluginImagePage = ({ data }) => {
))}
</Grid>

<h2>gatsby-plugin-image: Custom Image Formats (WebP & AVIF)</h2>
<Grid data-cy="customImageFormats">
{data.default.nodes.map(node => (
<div>
<p>
<strong>
{node.title} ({node.file.fileName.split(".").pop()})
</strong>
</p>
{node.description && <p>{node.description}</p>}
{node.customImageFormats ? (
<GatsbyImage image={node.customImageFormats} />
) : (
<SvgImage src={node.file.url} />
)}
</div>
))}
</Grid>

<h2>gatsby-plugin-image: SQIP Placeholder</h2>
<Grid data-cy="sqip">
{data.default.nodes.map(node => (
Expand Down Expand Up @@ -232,6 +251,7 @@ export const pageQuery = graphql`
layout: FIXED
placeholder: BLURRED
)
customImageFormats: gatsbyImageData(formats: [AVIF, WEBP, AUTO])
sqip(numberOfPrimitives: 12, blur: 0, mode: 1) {
dataURI
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export function generateImageSource(

if (!validImageFormats.has(toFormat)) {
console.warn(
`[gatsby-source-contentful] Invalid image format "${toFormat}". Supported types are jpg, png and webp"`
`[gatsby-source-contentful] Invalid image format "${toFormat}". Supported types are jpg, png, webp and avif"`
)
return undefined
}
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-source-contentful/src/image-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { URLSearchParams } from "url"
export const CONTENTFUL_IMAGE_MAX_SIZE = 4000

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

// Determine the proper file extension based on mime type
export const mimeTypeExtensions = new Map([
Expand All @@ -15,6 +15,7 @@ export const mimeTypeExtensions = new Map([
[`image/gif`, `.gif`],
[`image/png`, `.png`],
[`image/webp`, `.webp`],
[`image/avif`, `.avif`],
])

// Check if Contentful asset is actually an image
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-contentful/src/schemes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const ImageFormatType = new GraphQLEnumType({
JPG: { value: `jpg` },
PNG: { value: `png` },
WEBP: { value: `webp` },
AVIF: { value: `avif` },
},
})

Expand Down

0 comments on commit 8ff9cc3

Please sign in to comment.