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
SVG in css files are broken (only in build mode) #31878
Comments
I'm seeing the same thing when I tried upgrading my site from The line where I reference the svg is here. `gatsby develop` output
`gatsby build` output
gatsby@2 output, live on my site (https://xavd.id, a style on the `body`)
|
I'm seeing this same problem! And I think I finally realized why this happens! SVG in CSS URLs needs to have diff --git a/node_modules/gatsby/dist/utils/webpack-utils.js b/node_modules/gatsby/dist/utils/webpack-utils.js
index d29ab17..68d082a 100644
--- a/node_modules/gatsby/dist/utils/webpack-utils.js
+++ b/node_modules/gatsby/dist/utils/webpack-utils.js
@@ -483,7 +483,7 @@ const createWebpackUtils = (stage, program) => {
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`, `removeXMLNS`, `removeXMLProcInst`, `reusePaths`, `sortAttrs`]
+ `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`]
}
}]
} |
Edit from LekoArts: Please note that this issue is fixed in A quick workaround: add this to your // 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)
}
}; |
Hi all, we've similarly come across an issue with SVG being broken when running Works (version 3.6): data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='32' height='32' viewBox='0 0 24 24' aria-hidden='true'%3E%3Ccircle cx='12' cy='12' r='10' fill='%23005eb8'%3E%3C/circle%3E%3Cpath fill='none' stroke='%23fff' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M12 8v8M8 12h8'%3E%3C/path%3E%3C/svg%3E%0A Doesn't work (version 3.7): data:image/svg+xml;charset=utf-8,%3Csvg aria-hidden='true' viewBox='0 0 24 24'%3E%3Ccircle cx='12' cy='12' r='10' fill='%23005eb8'/%3E%3Cpath fill='none' stroke='%23fff' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M12 8v8m-4-4h8'/%3E%3C/svg%3E |
Can confirm that I am seeing this issue in gatsby@3 and using the solution provided by @DavidRouyer works for now. Faced this issue with mapbox-gl control icons as they use SVG URLs in CSS. |
I am seeing this issue as well. I have a encoded SVG as a background-image in CSS, which works perfectly in |
temporary workaround for gatsbyjs/gatsby#31878
This is fixed in |
A fixed was merged in in v3.8.1, one above my base release gatsbyjs/gatsby#31878
Posting here since it seems to be related. 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.
Broken svg: 👇
This feels like a bug to me. |
@Kolombiken same issue with 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> |
@mohatt & @Kolombiken same issue with Similar to the #32123 fix that disabled "removeXMLNS", this issue is related to the removeDimensions config:
which is also disabled by default at https://github.com/svg/svgo#built-in-plugins |
still broken in build mode |
Revisiting this one using Remove removeDimensions from svgo config #32834 seems promising though. |
Just wanted to report that it's still broken in |
Just tried this out again with |
@Kolombiken Yeah looks like it got fixed in |
I'm finding that the Tested on |
Why is this closed? Still not working in v4 #34395 |
I have the same issue with version "^4.7.2" |
I'm having this same issue ^4.21.0 |
Still getting error in gatsby 4.0.0 - |
Still not working in I even checked the base64 before by converting and after (running build), they're not the same. |
Not great, but until it's properly fixed, I worked around the issue by doing a find and replace on the build output from Gatsby. I just had one messed up SVG and I couldn't get any of the other workarounds posted here to work. Here's what I did: First, I created a little shell script that has the good (from dev) and bad (from build) data URI values. It takes those values and does a find and replace on the public directory using fix_31878.sh
You'll want to update I then updated the build script command in
I'm sure that there are other better workarounds and obviously this isn't a fix, but it should get your site working again if you just have a couple of bad SVGs. |
I am also running into this issue with gatsby 4.3, hope it gets reopened! In my case the svg was rendering but didn't look right, and this comment fixed the issue after I also removed the |
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:
#31619
#15629
In my scss file I have:
This compiles to:
This is the original 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(R) Core(TM) 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
The text was updated successfully, but these errors were encountered: