Files

38 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2026-11-06 13:05:36 +01:00
import { defineHandler } from 'nitro/h3';
2024-02-01 22:38:21 +09:00
import { renderToString } from 'react-dom/server';
2026-02-26 16:57:10 +01:00
import React from 'react';
import Icon from 'lucide-react/src/Icon';
2026-02-26 12:33:53 +01:00
import type { IconNode } from 'lucide-react/src/types';
2023-06-04 17:52:03 +02:00
import { parseSync } from 'svgson';
2026-11-06 13:05:36 +01:00
export default defineHandler((event) => {
2024-02-01 22:38:21 +09:00
const { params } = event.context;
2023-06-04 17:52:03 +02:00
const [strokeWidth, svgData] = params.data.split('/');
const data = svgData.slice(0, -4);
const src = Buffer.from(data, 'base64').toString('utf8');
2026-02-26 16:57:10 +01:00
const iconNode = parseSync(src.includes('<svg') ? src : `<svg>${src}</svg>`).children.map(
({ name, attributes }) => [name, attributes],
2026-02-26 17:34:02 +01:00
) as IconNode;
2023-06-04 17:52:03 +02:00
2026-11-06 13:05:36 +01:00
event.res.headers.set('Content-Type', 'image/svg+xml');
event.res.headers.set('Cache-Control', 'public,max-age=31536000');
2023-06-04 17:52:03 +02:00
2026-02-26 16:57:10 +01:00
return renderToString(
<Icon
iconNode={iconNode}
strokeWidth={strokeWidth}
>
<style>
2026-02-26 17:34:02 +01:00
{`@media screen and (prefers-color-scheme: light) {
2026-02-26 16:57:10 +01:00
svg { fill: transparent !important; }
}
@media screen and (prefers-color-scheme: dark) {
svg { stroke: #fff; fill: transparent !important; }
}`}
</style>
2026-02-26 17:34:02 +01:00
</Icon>,
);
2024-02-01 22:38:21 +09:00
});