import { Resvg } from "@resvg/resvg-js"; import satori from "satori"; import path from "path"; import { promises as fs } from "fs"; import { LoaderFunctionArgs } from "@remix-run/node"; export async function loader({ request }: LoaderFunctionArgs) { const jsonDirectory = path.join(process.cwd()); // Read the json data file data.json const CircularBold = await fs.readFile( jsonDirectory + "/public/fonts/CircularXXWeb-Bold.woff", ); const CircularMedium = await fs.readFile( jsonDirectory + "/public/fonts/CircularXXWeb-Medium.woff", ); const CircularRegular = await fs.readFile( jsonDirectory + "/public/fonts/CircularXXWeb-Regular.woff", ); const { searchParams } = new URL(request.url); const hasTitle = searchParams.has("title"); const hasDescription = searchParams.has("description"); if (!hasTitle && !hasDescription) { console.log("No title, description or product provided"); throw new Error("No title, description or product provided"); } const title = searchParams.get("title")?.slice(0, 100); const description = searchParams.get("description")?.slice(0, 100); const element = (
{title}
{description}
); const svg = await satori(element, { width: 1200, height: 630, embedFont: true, fonts: [ { name: "Circular", data: CircularRegular, weight: 400, style: "normal", }, { name: "Circular", data: CircularMedium, weight: 500, style: "normal", }, { name: "Circular", data: CircularBold, style: "normal", weight: 700, }, ], }); const resvg = new Resvg(svg); const buffer = resvg.render().asPng(); return new Response(buffer, { headers: { "content-type": "image/png", "cache-control": "public, immutable, no-transform, s-max-age=31536000, max-age=31536000", }, }); }