Inline and optimize SVG's from your GraphQL data source
gatsby-transformer-inline-svg
currently only works with Contentful assets and doesn't work with File
nodes, so this plugin aims to be CMS agnostic, it can still work with CMS images if they are added via the gatsby-source-filesystem
via the createRemoteFileNode API
npm i gatsby-transformer-inline-svg
gatsby-config.js:
module.exports = {
plugins: [
'gatsby-transformer-inline-svg-v2'
]
}
GraphQL Query:
file {
childInlineSvg {
content
}
url
}
Rendering:
import React from 'react'
export default function Image({ file }) {
// inlined SVG
if (file?.childInlineSvg?.content) {
return <div dangerouslySetInnerHTML={{ __html: file?.childInlineSvg?.content }} />
}
// other images
return <img src={file.url} alt={alt} />
}