question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

viewBox stripped from HTML output (nextjs)

See original GitHub issue

Recently noticed the viewBox attribute has been stripped from our rendered SVG elements. I’ve attempted to use the plugins option inside the svgo property as plugins: [{ "removeViewBox": false }] but it throws an error on compilation for ‘Plugin name should be specified’ so I believe this setup should work?

Currently using

"babel-plugin-inline-react-svg": "2.0.1",
"next": "10.2.0",

.babelrc:

plugins: [
    [
        'inline-react-svg',
        {
            svgo: {
                plugins: [
                    {
                        name: 'removeViewBox',
                        params: false,
                    }
                ],
            },
        },
    ],
    ['styled-components', { ssr: true }],
],

svg element:

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15.557 15.556" width="15.557" height="15.556">
    ..
</svg>

output:

<svg xmlns="http://www.w3.org/2000/svg" width="15.557" height="15.556">
    ..
</svg>

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

5reactions
art-1stcommented, May 13, 2021

I have same issues during bump up some libraries. so i just downgrade babel-plugin-inline-react-svg version ^1.1.2. it work normally.

or if you want using latest version, change attribute name params to active https://github.com/svg/svgo/releases/tag/v2.0.0

"plugins": [
    [
      "inline-react-svg",
      {
        "svgo": {
          "plugins": [
            {
              "name": "removeViewBox",
              "active": false
            }
          ]
        }
      }
    ],
}

then will work fine.

0reactions
joshuaaroncommented, May 13, 2021

@nikola1970 I didn’t really change much, outside the params property from my original answer, if it helps, my .babelrc looks like this:

{
    "presets": ["next/babel"],
    "plugins": [
        ["inline-react-svg", { "svgo": { "plugins": [ {
            "name": "removeViewBox",
            "active": false
        }]}}],
        [ "styled-components", { "ssr": true }]
    ],
    "env": {
        "production": {
            "plugins": ["babel-plugin-jsx-remove-data-test-id"]
        }
    }
}

Now when I view my SVGs in the browser, the viewBox is being rendered correctly. Still using

"babel-plugin-inline-react-svg": "2.0.1",
"next": "10.2.0",
Read more comments on GitHub >

github_iconTop Results From Across the Web

SVG viewBox attribute getting stripped in HTML #1523
I have looked into it and it appears HTMLAsset.js is calling posthtml-parser with its own config of {lowerCaseAttributeNames: true} which causes ...
Read more >
Next js app with SSR is not pre-rendering HTML, so web- ...
js app that uses server side rendering. When running the application both locally and in production (on vercel edge), the initial page response ......
Read more >
next/image
When the browser chooses, it does not yet know the size of the image on the page, so it selects an image that...
Read more >
next/legacy/image
The layout behavior of the image as the viewport changes size. ... that is invoked once the image is completely loaded and the...
Read more >
Blog - Next.js 8
The Next.js serverless target outputs Serverless functions from pages ... As Next.js pre-renders HTML, it wraps pages into a default ...
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