Skip to content

Instantly share code, notes, and snippets.

@natemoo-re
Last active July 13, 2020 15:40
Show Gist options
  • Save natemoo-re/85c6cc3e4d029f26905a5931ee5cce58 to your computer and use it in GitHub Desktop.
Save natemoo-re/85c6cc3e4d029f26905a5931ee5cce58 to your computer and use it in GitHub Desktop.
Serverless README Image (React)
import React from "react";
export const ReadmeImg = ({ width, height, children }) => {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
width={width} height={height}
viewBox={`0 0 ${width} ${height}`}
>
<foreignObject width={width} height={height}>
<div {...{ xmlns: "http://www.w3.org/1999/xhtml" }}>
{children}
</div>
</foreignObject>
</svg>
);
};
import { renderToString } from "react-dom/server";
import { ReadmeImg } from "@/components";
export default async function (req, res) {
res.setHeader("Content-Type", "image/svg+xml");
const svg = renderToString(
<ReadmeImg>
<style>{`/* CSS HERE */`}</style>
<h1>Hello world!</h1>
</ReadmeImg>
);
return res.status(200).send(svg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment