🛠️ Implement SVG optimization utility and update getSource function to use it

This commit is contained in:
pheralb
2025-08-27 19:29:55 +01:00
parent 879f8eb10e
commit 146e8fda04
3 changed files with 42 additions and 2 deletions
+4 -1
View File
@@ -1,3 +1,5 @@
import { optimizeSvg } from "@/utils/optimizeSvg";
interface SourceParams {
url: string | undefined;
}
@@ -5,5 +7,6 @@ interface SourceParams {
export const getSource = async (params: SourceParams) => {
const response = await fetch(params.url || "");
const content = await response.text();
return content;
const optimizedContent = optimizeSvg({ svgCode: content });
return optimizedContent;
};
+37
View File
@@ -0,0 +1,37 @@
import { optimize } from "svgo/browser";
interface OptimizeSvg {
svgCode: string;
}
export const optimizeSvg = ({ svgCode }: OptimizeSvg) => {
const svgo = optimize(svgCode, {
multipass: true,
plugins: [
"removeDimensions",
"removeXMLNS",
"removeDoctype",
"removeComments",
"removeStyleElement",
"cleanupAttrs",
"cleanupEnableBackground",
"cleanupIds",
"minifyStyles",
"removeDoctype",
"removeDesc",
"removeEmptyAttrs",
"removeEmptyText",
"removeHiddenElems",
"removeNonInheritableGroupAttrs",
"removeUnknownsAndDefaults",
"removeUselessDefs",
"removeUselessStrokeAndFill",
"removeXMLProcInst",
{
name: "removeAttrs",
params: { attrs: "(data-name|id|class)" },
},
],
});
return svgo.data;
};
+1 -1
View File
@@ -1,4 +1,4 @@
import { optimize } from "svgo";
import { optimize } from "svgo/browser";
export const getPrefixFromSvgUrl = (svgUrl: string) => {
return svgUrl.split("/").pop()!.replace(".svg", "").split("-").join("_");