1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/** @jsxImportSource npm:react */
import { render as renderPNG } from "https://deno.land/x/resvg_wasm@0.2.0/mod.ts";
import { renderToStaticMarkup } from "https://esm.sh/react-dom/server";
type SVGComponentProps = {
request: Request;
url: URL;
};
const cache = new Map();
export function render(Component: React.ComponentType<SVGComponentProps>, headers: any = {}) {
return async function(req: Request): Promise<Response> {
const url = new URL(req.url);
const svg = renderToStaticMarkup(<Component request={req} url={url} />);
console.log(svg);
const png = await renderPNG(svg);
return new Response(png, {
headers: {
"Content-Type": "image/png",
...headers,
},
});
};
}
export default render;
/*
let png;
if (cache[req.url]) {
console.log("using cached response");
png = cache[req.url];
} else {
const svg = renderToStaticMarkup(<Component request={req} url={url} />);
console.log(svg);
png = await renderPNG(svg);
cache[req.url] = png;
}
*/
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 21, 2024