import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { ImageResponse } from "next/og";
import { getAllIcons } from "@/lib/api";
export const dynamic = "force-dynamic";
export const size = {
width: 1200,
height: 630,
};
export const alt = "Icon Open Graph Image";
export const contentType = "image/png";
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(" ");
// Read the icon file from local filesystem
let iconData: Buffer | null = null;
try {
const iconPath = join(process.cwd(), `../png/${icon}.png`);
console.log(
`Generating opengraph image for ${icon} (${index + 1} / ${totalIcons}) from path ${iconPath}`,
);
iconData = await readFile(iconPath);
} catch (_error) {
console.error(`Icon ${icon} was not found locally`);
}
// Convert the image data to a data URL or use placeholder
const iconUrl = iconData
? `data:image/png;base64,${iconData.toString("base64")}`
: null;
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,
},
);
}