Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid converting svg to data-uri in Gatsby

Tags:

gatsby

I need to use an inline SVG so I can animate it, but the SVG is less than 10KB so Gatsby is converting it to a data-uri. Is there a way to tell Gatsby to not convert a particular resource to a data-uri?

like image 781
Eric Johnson Avatar asked Jan 25 '18 01:01

Eric Johnson


People also ask

Does Gatsby image work with SVG?

As you pointed out, there's no GatsbyImageData because Gatsby's transformers and sharps ( gatsby-transformer-sharp but especially gatsby-plugin-sharp ) doesn't support SVG. As they are not able to interpret SVG assets, they are not able to create the GraphQL nodes.

How do you use SVG in Gatsby?

This you can use as src of img tag. And this method: import { ReactComponent as Img } from 'path/to/file. svg' allows you to import inline svg.


2 Answers

You can use gatsby-plugin-react-svg.

npm install --save gatsby-plugin-react-svg

Add the plugin to your gatsby-config.js, then in your code:

import Icon from './path/icon.svg';

// ...

<Icon />

Alternatively, you can also use gatsby-plugin-svgr.

like image 132
Fabian Schultz Avatar answered Oct 01 '22 13:10

Fabian Schultz


To add to Fabian Schultz's answer

There is Gatsby v2 support for gatsby-plugin-react-svg if you use the @next version

npm install gatsby-plugin-react-svg@next --save

There were 2 tickets opened in the repo

Issue 4

Issue 7

like image 28
Sarah Avatar answered Oct 01 '22 12:10

Sarah