Skip to content
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

Preserve SVG viewBox when minifying SVGs for production #5662

Merged
merged 1 commit into from
Jul 26, 2019

Conversation

robertknight
Copy link
Member

@robertknight robertknight commented Jul 17, 2019

By default svgo (our SVG minifier) removes the viewBox attribute of the <svg> element, which breaks
scaling of the SVG content when rendering.

This caused various icons on the site to render at an incorrect scale.
In some cases this was not very noticeable because the natural size of
the content was close to the scaled size. In other cases the icon failed
to appear at all.

To see the difference, compare:

rm build/images/icons/*
NODE_ENV=production gulp build-images
cat build/images/icons/groups.svg

Before and after this patch is applied.

I'm unclear if this issue appeared in production when svgo was last updated, which happened in 2762912, or whether it has in fact existed for a much longer period of time, since the behavior is not new in svgo.

Other projects will need to be checked for the same issue.

Fixes #5656

Background reading on how SVG scaling works: https://css-tricks.com/scale-svg/

@dwhly
Copy link
Member

dwhly commented Jul 17, 2019

Whoop!

@robertknight
Copy link
Member Author

In the Hypothesis client we are not actually minifying images at all in production builds, so the issue doesn't apply.

By default svgo removes the `viewBox` attribute of the SVG, which breaks
scaling of the SVG content when rendering.

This caused various icons on the site to render at an incorrect scale.
In some cases this was not very noticeable because the natural size of
the content was close to the scaled size. In other cases the icon failed
to appear at all.

To see the difference, compare:

```
rm build/images/icons/*
NODE_ENV=production gulp build-images
cat build/images/icons/groups.svg
```

Before and after this patch is applied.

Fixes #5656
Copy link
Contributor

@lyzadanger lyzadanger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix!

I can verify that the viewBox has reappeared in minified SVGs locally. On master (i.e. "before"):

lyza@sweetbirch:~/Projects/h master
$ cat build/images/icons/groups.svg
<svg xmlns="http://www.w3.org/2000/svg" width="120" height="120"><g fill="currentColor" transform="translate(.508 7.627)" fill-rule="evenodd"><circle cx="36" cy="41" r="18"/><circle cx="84" cy="41" r="18"/><path d="M72 97.042h44V85s0-19-32-19c-9.065 0-15.562 1.525-20.218 3.71a24.324 24.324 0 0 1 3.278 3.213c2.135 2.536 3.518 5.274 4.291 8.027.276.984.456 1.908.558 2.756.066.553.091.99.091 1.294v12.042z"/><path d="M4 97.042h64V85s0-19-32-19S4.004 85 4.004 85L4 97.042z"/></g></svg>

On this branch:

lyza@sweetbirch:~/Projects/h ROB-fix-svg-rendering
$ cat build/images/icons/groups.svg
<svg xmlns="http://www.w3.org/2000/svg" width="120" height="120" viewBox="0 0 120 120"><g fill="currentColor" transform="translate(.508 7.627)" fill-rule="evenodd"><circle cx="36" cy="41" r="18"/><circle cx="84" cy="41" r="18"/><path d="M72 97.042h44V85s0-19-32-19c-9.065 0-15.562 1.525-20.218 3.71a24.324 24.324 0 0 1 3.278 3.213c2.135 2.536 3.518 5.274 4.291 8.027.276.984.456 1.908.558 2.756.066.553.091.99.091 1.294v12.042z"/><path d="M4 97.042h64V85s0-19-32-19S4.004 85 4.004 85L4 97.042z"/></g></svg>

@robertknight robertknight merged commit 41e454d into master Jul 26, 2019
@robertknight robertknight deleted the fix-svg-rendering branch July 26, 2019 13:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Icon missing next to group name on web app
3 participants