import { ImageResponse } from "next/og";
import { BASE_URL } from "@/constants";
import { getAllIcons } from "@/lib/api";
export const runtime = "edge";
export const revalidate = false;
export const size = {
width: 1200,
height: 630,
};
export default async function Image({
params,
}: {
params: Promise<{ icon: string }>;
}) {
const { icon } = await params;
if (!icon) {
console.error(`[Opengraph Image] Icon not found for ${icon}`);
return new ImageResponse(
Icon not found
,
{ ...size },
);
}
const iconsData = await getAllIcons();
const totalIcons = Object.keys(iconsData).length;
const index = Object.keys(iconsData).indexOf(icon);
// Format the icon name for display
const formattedIconName = icon
.split("-")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ");
// Fetch the icon from CDN (Edge Runtime compatible)
let iconUrl: string | null = null;
try {
const iconCdnUrl = `${BASE_URL}/png/${icon}.png`;
const response = await fetch(iconCdnUrl);
if (response.ok) {
const arrayBuffer = await response.arrayBuffer();
const base64 = Buffer.from(arrayBuffer).toString("base64");
iconUrl = `data:image/png;base64,${base64}`;
}
} catch (_error) {
console.error(`Icon ${icon} was not found on CDN`);
}
return new ImageResponse(
{/* Background blur blobs */}
{/* Main content layout */}
{/* Icon container */}
{/* Text content */}
Download {formattedIconName} icon for free
Amongst {totalIcons} other high-quality dashboard icons
{["SVG", "PNG", "WEBP"].map((format) => (
{format}
))}
{/* Footer */}
,
{
...size,
},
);
}