mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-11-21 12:53:56 +01:00
faster banners
This commit is contained in:
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()));
|
||||
}
|
||||
Reference in new issue
Block a user