ID collisions with multiple SVG

See original GitHub issue

With React + Webpack when I load multiple SVG in the same component (SVG is in the dom), ids are the same in multiple SVG :

First SVG result :

<svg width="158" height="185" xmlns:xlink="http://www.w3.org/1999/xlink" class="sc-fYxtnH hSLfWw">
<defs>
<linearGradient x1="0%" y1="100%" y2="0%" id="c">
...

Second SVG result :

<svg width="23" height="23" xmlns:xlink="http://www.w3.org/1999/xlink">
  <defs>
      <path d="M22.733 1-.267-2.09z" id="c"></path>
  </defs>
  <g fill="none" fill-rule="evenodd"> 
      <g>
          <mask id="d" fill="#fff">
              <use xlink:href="#c"></use>
          </mask>
          <path fill="#EA4335" fill-rule="nonzero" mask="url(#d)" d="M-1.07 4.705L8.023 11.5l3.744-3.189 12.838-2.038v-7.318H-1.07z"></path>
      </g>
...

The second SVG is not rendered correctly because of that. When i remove the first SVG everything works fine.

Here is my Webpack config :

{
  test: /\.svg$/,
  use: [{
    loader:'@svgr/webpack',
    options: {
      "svgoConfig": {
        "plugins": [{ "cleanupIDs": true }]
      }
    }
  }]
}

Is there something I forgot ? Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
wbobeirnecommented, Oct 31, 2018

I ran into this with linear gradients, fortunately some searching around led me here. However, this seems like a pretty disastrous default. I was able to fix mine with a simplified version of the hashing solution:

const hash = require('string-hash');

...

use: ({ resource }) => ({
  loader: '@svgr/webpack',
  options: {
    svgoConfig: {
      plugins: [{
        cleanupIDs: {
          prefix: `svg-${hash(resource)}`,
        },
      }],
    },
  },
}),

Is there a way we could get the default behavior to be more like this hashing solution? Seems like this is a fairly common problem.

8reactions
jjmax75commented, Aug 22, 2018

found this - https://github.com/svg/svgo/issues/674#issuecomment-324193597

the next one is a fix too

Hope it helps

 {
            test: /\.svg$/,
            use: ({resource}) => ({
                loader: '@svgr/webpack',
                options: {
                  svgoConfig: {
                    plugins: [{
                      "cleanupIDs": {
                        "prefix": `svg${hash(relative(context, resource))}`
                      }
                    }]
                  }
                }
              })
          }
Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix duplicate SVG ID collision in React | by Anton Ball - Medium
A gotcha with SVG fill ids that can mean unexpected results when inline SVGs are rendered onto the page.
Read more >
conflicting ids in two different SVG files included in the same ...
Making IDs unique is not only necessary if two SVGs use the same ID, but also if the same SVG is used multiple...
Read more >
If You're Inlining SVG Icons, How Do You Deal With Unique ...
Using the same svg in multiple places produces duplicate id's, but at the same time, the values of those titles and descriptions haven't...
Read more >
Multiple Inline SVGs (From QuickChart) - Jim Nielsen's Blog
When multiple SVGs from QuickChart get embedded in the same document, they clobber each other's namespace! In other words: clip-path="url(#clip1) ...
Read more >
react-svg-unique-id - npm
If both of these SVGs were not wrapped in SVGUniqueID component there would be an id collision since two html elements would have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found