-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unique id is broken since v3 #1746
Comments
Hello Work:
Doesn't work:
|
Try to put prefixIds after cleanupIds |
|
@TrySound can you help us on this please? |
Running into this issue as well. Without any changes to the source SVG, previously I would get an ID like This is my config: module.exports = {
js2svg: {
indent: 2,
pretty: true,
},
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false,
},
},
},
'convertStyleToAttrs',
'prefixIds',
'removeDimensions',
],
}; Any suggestions on how to fix this please? @TrySound |
TrySound is correct,
The alternate solution was to use I've opened the following PR: With this, I've tested both of your examples using the following configuration, and can confirm they no longer break. const crypto = require('crypto');
module.exports = {
plugins: [
'preset-default',
{
name: 'prefixIds',
params: {
delim: '',
prefix: () => crypto.randomBytes(20).toString('hex').slice(0, 4)
}
}
],
}; Sorry for the headache, and thanks for being patient with this. The fix will be released in v3.0.3.
This CLI works fine for me, but if you're using the JS API, it may be because you didn't provide the More info: https://github.com/svg/svgo#optimize By default, |
Thanks so much @SethFalco 🙌 Can confirm this works in our project 👍 Here's a modern ES Modules + JSDoc version of the import { randomBytes } from 'node:crypto';
/** @type {import('svgo').Config} */
const svgoConfig = {
plugins: [
'preset-default',
// Avoid collisions with ids in other SVGs,
// which was causing incorrect gradient directions
// https://github.com/svg/svgo/issues/1746#issuecomment-1803595909
//
// Previously, this was a problem where unique ids
// could not be generated since svgo@3
// https://github.com/svg/svgo/issues/674
// https://github.com/svg/svgo/issues/1746
{
name: 'prefixIds',
params: {
delim: '',
prefix: () => randomBytes(20).toString('hex').slice(0, 4),
},
},
],
};
export default svgoConfig; |
A simpler alternative which does not rely on any imports is a simple counter, can also confirm this works in our project: let svgoPrefixIdsCount = 0;
/** @type {import('svgo').Config} */
const svgoConfig = {
plugins: [
'preset-default',
// Avoid collisions with ids in other SVGs,
// which was causing incorrect gradient directions
// https://github.com/svg/svgo/issues/1746#issuecomment-1803600573
//
// Previously, this was a problem where unique ids
// could not be generated since svgo@3
// https://github.com/svg/svgo/issues/674
// https://github.com/svg/svgo/issues/1746
{
name: 'prefixIds',
params: {
delim: '',
prefix: () => svgoPrefixIdsCount++,
},
},
],
};
export default svgoConfig; |
Describe the bug
I need unique ids when multiple SVGs have
<defs>
. Withv2.8
I was using the following code and it works well.But in
v3
theprefix
parameter ofcleanupIDs
is removed replacingprefixIds
. I can't get the same rendering for the IDs.I've tested this code but it breaks the
<defs>
.In input there are 2 svgs
gradient.svg
delete-alert.svg
In output, with the
prefixIds
plugin, it producedI've also tested the solution from #674 (comment) but there is always ids conflics. It produces the following output:
To Reproduce
I don't think it's necessary, tell me if necessary
Expected behavior
Unique ids should be possible as in
v.2.8
.Screenshots
Desktop (please complete the following information):
3.0.2
14.21.2
macOS 12.6
Additional context
The package is used inside the svg-chunk-webpack-plugin. All SVGO options are transmitted from plugin users.
The question was posted on the SVGO Discord channel, without response (See question on SVGO Discord).
This issue is linked to #674
The text was updated successfully, but these errors were encountered: