Skip to content

Commit 6d4fa76

Browse files
Florian Gygerpieh
Florian Gyger
andauthored
fix(gatsby-plugin-sharp): fix Traced SVG scaling issue in Internet Explorer (#22358)
* fixed Traced SVG scaling issue in Internet Explorer * return SVG string from mocked potrace and updated snapshots * fix snapshot Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 75b041e commit 6d4fa76

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -1058,7 +1058,7 @@ Object {
10581058
"originalName": "test.png",
10591059
"src": "/static/1234/7e516/test.png",
10601060
"srcSet": "/static/1234/7e516/test.png 1x",
1061-
"tracedSVG": "data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='100'%20height='100'%3e%3cpath%20d='M41%2024c-18%207-24%2029-11%2043%2015%2017%2044%208%2046-15%201-19-17-34-35-28'%20fill='red'%20fill-rule='evenodd'/%3e%3c/svg%3e",
1061+
"tracedSVG": "data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='100'%20height='100'%20viewBox='0%200%20100%20100'%20preserveAspectRatio='none'%3e%3cpath%20d='M41%2024c-18%207-24%2029-11%2043%2015%2017%2044%208%2046-15%201-19-17-34-35-28'%20fill='red'%20fill-rule='evenodd'/%3e%3c/svg%3e",
10621062
"width": 100,
10631063
}
10641064
`;
@@ -1078,6 +1078,6 @@ Object {
10781078
/static/1234/a1812/test.png 50w,
10791079
/static/1234/7e516/test.png 100w",
10801080
"srcSetType": "image/png",
1081-
"tracedSVG": "data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='100'%20height='100'%3e%3cpath%20d='M41%2024c-18%207-24%2029-11%2043%2015%2017%2044%208%2046-15%201-19-17-34-35-28'%20fill='red'%20fill-rule='evenodd'/%3e%3c/svg%3e",
1081+
"tracedSVG": "data:image/svg+xml,%3csvg%20xmlns='http://www.w3.org/2000/svg'%20width='100'%20height='100'%20viewBox='0%200%20100%20100'%20preserveAspectRatio='none'%3e%3cpath%20d='M41%2024c-18%207-24%2029-11%2043%2015%2017%2044%208%2046-15%201-19-17-34-35-28'%20fill='red'%20fill-rule='evenodd'/%3e%3c/svg%3e",
10821082
}
10831083
`;

packages/gatsby-plugin-sharp/src/__tests__/trace-svg.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ jest.mock(`sharp`, () => {
1717
})
1818

1919
jest.mock(`potrace`, () => {
20+
const circleSvgString = `<svg height="100" width="100"><circle cx="50" cy="50" r="40" /></svg>`
2021
return {
21-
trace: (_, _2, cb) => cb(null, ``),
22+
trace: (_, _2, cb) => cb(null, circleSvgString),
2223
Potrace: {
2324
TURNPOLICY_MAJORITY: `wat`,
2425
},

packages/gatsby-plugin-sharp/src/trace-svg.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,24 @@ exports.notMemoizedPrepareTraceSVGInputFile = async ({
6868

6969
const optimize = svg => {
7070
const SVGO = require(`svgo`)
71-
const svgo = new SVGO({ multipass: true, floatPrecision: 0 })
71+
const svgo = new SVGO({
72+
multipass: true,
73+
floatPrecision: 0,
74+
plugins: [
75+
{
76+
removeViewBox: false,
77+
},
78+
{
79+
addAttributesToSVGElement: {
80+
attributes: [
81+
{
82+
preserveAspectRatio: `none`,
83+
},
84+
],
85+
},
86+
},
87+
],
88+
})
7289
return svgo.optimize(svg).then(({ data }) => data)
7390
}
7491

0 commit comments

Comments
 (0)