faster banners

This commit is contained in:
tresabhi committed 2026-11-18 23:38:52 -06:00
1 parent a7f0b768a5
commit fc32de0f3f
23 files changed
+67 -4

No files matched your search

@@ -0,0 +1,42 @@
import type { APIContext, GetStaticPathsItem } from "astro";
import { existsSync } from "fs";
import sharp from "sharp";
import { mixStaticPaths } from "../../../../../astro/mixStaticPaths";
import {
TOOL_BANNER_HEIGHTS,
ToolBannerSize,
tools,
} from "../../../../../constants/tools";
import { getStaticPaths as _getStaticPaths } from "../../../_index";
interface ToolBannerProps {
tool: string;
size: ToolBannerSize;
}
function bannerPath(tool: string) {
return `src/media/banners/${tool}.webp`;
}
export const getStaticPaths = mixStaticPaths(_getStaticPaths, () =>
Object.values(tools)
.filter((tool) => existsSync(bannerPath(tool.id)))
.flatMap((tool) =>
Object.values(ToolBannerSize).map(
(size) =>
({
params: { tool: tool.id, size },
props: { tool: tool.id, size } satisfies ToolBannerProps,
} satisfies GetStaticPathsItem),
),
),
);
export async function GET({ props }: APIContext<ToolBannerProps>) {
const height = TOOL_BANNER_HEIGHTS[props.size];
let image = sharp(bannerPath(props.tool));
if (height !== null) image = image.resize({ height });
return new Response(new Uint8Array(await image.webp().toBuffer()));
}