Skip to content

Commit ef803c8

Browse files
authored
refactor(card): add SVG minification to reduce response size (#940)
1 parent 051c535 commit ef803c8

3 files changed

Lines changed: 194 additions & 10 deletions

File tree

package-lock.json

Lines changed: 175 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
"@resvg/resvg-js": "^2.6.2",
1010
"@types/escape-html": "^1.0.4",
1111
"@types/express": "^5.0.6",
12+
"@types/html-minifier-terser": "^7.0.2",
1213
"@types/node": "^25.5.0",
1314
"axios": "^1.14.0",
1415
"dotenv": "^17.3.1",
1516
"escape-html": "^1.0.3",
1617
"express": "^5.2.1",
18+
"html-minifier-terser": "^7.2.0",
1719
"millify": "^6.1.0",
1820
"node-base64-image": "^2.2.0",
1921
"sharp": "^0.34.5"

src/card.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sharp from "sharp";
2+
import { minify } from "html-minifier-terser";
23
import parseBoolean from "@barudakrosul/parse-boolean";
34
import type { GetData } from "./getData";
45
import type { UiConfig } from "../api/index";
@@ -235,7 +236,7 @@ export default async function card(data: GetData, uiConfig: UiConfig): Promise<s
235236
// Dynamic SVG height based on the number of items
236237
const svgHeight = Math.max(220, 45 + visibleItems.length * 25);
237238

238-
return `
239+
const rawSVG = `
239240
<svg width="535" height="${svgHeight}" direction="${isRtl ? "rtl" : "ltr"}" viewBox="0 0 535 ${svgHeight}" fill="none" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
240241
<style>
241242
${animationsCSS}
@@ -295,4 +296,19 @@ export default async function card(data: GetData, uiConfig: UiConfig): Promise<s
295296
</g>
296297
</svg>
297298
`;
299+
300+
// Minify SVG
301+
const minifiedSVG = await minify(rawSVG, {
302+
collapseWhitespace: true,
303+
removeComments: true,
304+
removeEmptyAttributes: true,
305+
removeRedundantAttributes: true,
306+
useShortDoctype: true,
307+
removeOptionalTags: false,
308+
removeAttributeQuotes: false,
309+
minifyCSS: true,
310+
minifyJS: false,
311+
});
312+
313+
return minifiedSVG;
298314
}

0 commit comments

Comments
 (0)