gatsby: SVG in css files are broken (only in build mode)

Description

SVG files referenced in scss files are broken in build mode. In development mode, I can see all files without problems. This affects bootstrap icons too.

Related issues: https://github.com/gatsbyjs/gatsby/issues/31619 https://github.com/gatsbyjs/gatsby/issues/15629

In my scss file I have:

background-image: url('../images/site-core/icons/arrow-prev-icon.svg')

This compiles to:

background-image: url(data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgOS40ODcgNi4zMjgiPjxwYXRoIGZpbGw9IiNmZmYiIGQ9Ik0zLjQzNy4xMjFhLjQzMS40MzEgMCAwIDEgMCAuNjA2bC0yIDIuMDA3aDcuNjIyYS40MjguNDI4IDAgMCAxIDAgLjg1N0gxLjQ0bDIgMi4wMDdhLjQzNC40MzQgMCAwIDEgMCAuNjA2LjQyNy40MjcgMCAwIDEtLjYgMEwuMTE4IDMuNDY2YS40ODEuNDgxIDAgMCAxLS4wODktLjEzNS40MDkuNDA5IDAgMCAxLS4wMzMtLjE2NS40My40MyAwIDAgMSAuMTIyLS4zTDIuODMzLjEzMWEuNDIuNDIgMCAwIDEgLjYwNC0uMDF6IiBkYXRhLW5hbWU9Ikljb24gaW9uaWMtaW9zLWFycm93LXJvdW5kLWJhY2siLz48L3N2Zz4=);

This is the original SVG:

<svg xmlns="http://www.w3.org/2000/svg" width="9.487" height="6.328" viewBox="0 0 9.487 6.328"><path id="Icon_ionic-ios-arrow-round-back" data-name="Icon ionic-ios-arrow-round-back" d="M13.932,11.373a.431.431,0,0,0,0,.606l2,2.007H8.307a.428.428,0,0,0,0,.857h7.622l-2,2.007a.434.434,0,0,0,0,.606.427.427,0,0,0,.6,0l2.715-2.735h0a.481.481,0,0,0,.089-.135.409.409,0,0,0,.033-.165.43.43,0,0,0-.122-.3l-2.715-2.735A.42.42,0,0,0,13.932,11.373Z" transform="translate(-7.882 -11.252)" fill="#fff"/></svg>

Steps to reproduce

Repository with gatsby plugin sass and an icon in index.js page.

https://github.com/MedicalWebExperts/gatsby-bug

Expected result

All SVG files should be fine in build mode.

Actual result

The SVG files are broken in build mode.

Environment

System: OS: macOS 11.3.1 CPU: (4) x64 Intel® Core™ i5-7360U CPU @ 2.30GHz Shell: 5.8 - /bin/zsh Binaries: Node: 14.17.0 - /usr/local/bin/node Yarn: 1.22.4 - /usr/local/bin/yarn npm: 6.14.13 - /usr/local/bin/npm Languages: Python: 3.9.1 - /Users/javier/.pyenv/shims/python Browsers: Chrome: 91.0.4472.77 Safari: 14.1 npmPackages: gatsby: ^3.7.1 => 3.7.1 gatsby-plugin-gatsby-cloud: ^2.7.1 => 2.7.1 gatsby-plugin-image: ^1.7.1 => 1.7.1 gatsby-plugin-manifest: ^3.7.1 => 3.7.1 gatsby-plugin-offline: ^4.7.1 => 4.7.1 gatsby-plugin-react-helmet: ^4.7.1 => 4.7.1 gatsby-plugin-sass: ^4.7.1 => 4.7.1 gatsby-plugin-sharp: ^3.7.1 => 3.7.1 gatsby-source-filesystem: ^3.7.1 => 3.7.1 gatsby-transformer-sharp: ^3.7.1 => 3.7.1 npmGlobalPackages: gatsby-cli: 2.12.113

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 12
  • Comments: 23 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Edit from LekoArts:

Please note that this issue is fixed in gatsby@3.8.1. Don’t use the workaround mentioned below if you can upgrade to that version.


A quick workaround: add this to your gatsby-node.js

// TODO: temporary workaround for https://github.com/gatsbyjs/gatsby/issues/31878
exports.onCreateWebpackConfig = ({
  actions,
  plugins,
  stage,
  getConfig
}) => {
  // override config only during production JS & CSS build
  if (stage === 'build-javascript') {
    // get current webpack config
    const config = getConfig()

    const options = {
      minimizerOptions: {
        preset: [
          `default`,
          {
            svgo: {
              full: true,
              plugins: [
                // potentially destructive plugins removed - see https://github.com/gatsbyjs/gatsby/issues/15629
                // use correct config format and remove plugins requiring specific params - see https://github.com/gatsbyjs/gatsby/issues/31619
                `removeUselessDefs`,
                `cleanupAttrs`,
                `cleanupEnableBackground`,
                `cleanupIDs`,
                `cleanupListOfValues`,
                `cleanupNumericValues`,
                `collapseGroups`,
                `convertColors`,
                `convertPathData`,
                `convertStyleToAttrs`,
                `convertTransform`,
                `inlineStyles`,
                `mergePaths`,
                `minifyStyles`,
                `moveElemsAttrsToGroup`,
                `moveGroupAttrsToElems`,
                `prefixIds`,
                `removeAttrs`,
                `removeComments`,
                `removeDesc`,
                `removeDimensions`,
                `removeDoctype`,
                `removeEditorsNSData`,
                `removeEmptyAttrs`,
                `removeEmptyContainers`,
                `removeEmptyText`,
                `removeHiddenElems`,
                `removeMetadata`,
                `removeNonInheritableGroupAttrs`,
                `removeOffCanvasPaths`,
                `removeRasterImages`,
                `removeScriptElement`,
                `removeStyleElement`,
                `removeTitle`,
                `removeUnknownsAndDefaults`,
                `removeUnusedNS`,
                `removeUselessStrokeAndFill`,
                `removeXMLProcInst`,
                `reusePaths`,
                `sortAttrs`,
              ],
            },
          },
        ],
      }
    }
    // find CSS minimizer
    const minifyCssIndex = config.optimization.minimizer.findIndex(
      minimizer => minimizer.constructor.name ===
        'CssMinimizerPlugin'
    )
    // if found, overwrite existing CSS minimizer with the new one
    if (minifyCssIndex > -1) {
      config.optimization.minimizer[minifyCssIndex] =
        plugins.minifyCss(options)
    }
    // replace webpack config with the modified object
    actions.replaceWebpackConfig(config)
  }
};

Why is this closed? Still not working in v4 https://github.com/gatsbyjs/gatsby/issues/34395

Just wanted to report that it’s still broken in gatsby@3.12.1. Would it be possible to open this issue again since it’s not fixed?

Still not working in Gatsby version: 5.7.0, works fine when using other svgs as images, but breaks when using scss and background-image.

I even checked the base64 before by converting and after (running build), they’re not the same.

I’m finding that the xmlns attribute is still getting removed from SVGs in develop mode. Build mode is all good though.

Tested on gatsby@3.13.1 and gatsby@3.14.0.

Posting here since it seems to be related. Using gatsby@3.10.1 and discovered that one svg is broken when I build. In develop mode it works like it should.

The difference between the broken build svg and the working develop one is that the working is using explicit width and height (and no viewBox) while the broken one has replaced the width and the height with a viewBox.

To be more explicit. Working svg: 👇

<svg width="346" height="68"…

Broken svg: 👇

<svg viewBox="0 0 346 68"…

This feels like a bug to me.

@Kolombiken same issue with gatsby@3.10.2

Working Svg:

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="20" height="34"><path d="m 19,3 -2,-2 -16,16 16,16 1,-1 -15,-15 15,-15 z" fill="#FFF"/></svg>

Broken svg:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 34"><path fill="#FFF" d="m19 3-2-2L1 17l16 16 1-1L3 17 18 2z"/></svg>