Search code examples
svggatsbynetlify

Problems with SVGs on Gatsby Cloud and Netlify deployment


I have problem with svgs used as imported i gatsby site. Everything seems to work fine on develop and build and serve modus. When i deploy site on gatsby cloud integrated with netflify all svgs which are imported not as inline html code not working properly. Weird is that i didnt get any error during build/deploy process. Anyone can help ? Below my dependencies and example of using svg on the site.

"dependencies": {
"@mdx-js/mdx": "^1.6.6",
"@mdx-js/react": "^1.6.6",
"babel-plugin-styled-components": "^1.10.7",
"dotenv": "^8.2.0",
"gatsby": "^2.24.2",
"gatsby-image": "^2.4.13",
"gatsby-plugin-layout": "^1.3.10",
"gatsby-plugin-manifest": "^2.4.18",
"gatsby-plugin-mdx": "^1.2.25",
"gatsby-plugin-offline": "^3.2.17",
"gatsby-plugin-react-helmet": "^3.3.10",
"gatsby-plugin-react-svg": "^3.0.0",
"gatsby-plugin-sharp": "^2.6.19",
"gatsby-plugin-styled-components": "^3.3.10",
"gatsby-plugin-web-font-loader": "^1.0.4",
"gatsby-source-datocms": "^2.3.0",
"gatsby-source-filesystem": "^2.3.19",
"gatsby-transformer-sharp": "^2.5.11",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-helmet": "^6.1.0",
"slugify": "^1.4.4",
"styled-components": "^5.1.1"

},

Example of using svg on the site:

import heroBgSvg from "../../assets/images/bg/bgHeroDark.svg"

const HeroSectionWrapper = styled.div`
  background-image: url(${heroBgSvg});
  background-repeat: no-repeat;`

All is working correctly on build/develop but not when page is online afer build/deployment on gatsby cloud.


Solution

  • When using gatsby-plugin-react-svg your SVG folder must not contain any other kind of asset rather than SVGs. Make sure that your including rule is a SVG-only folder, ideally by creating a specific folder, like:

    {
      resolve: 'gatsby-plugin-react-svg',
      options: {
        rule: {
          include: /svg/
        }
      }
    }