diff options
| author | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-09-21 07:49:21 +0900 |
|---|---|---|
| committer | Yasutake Yohei <61961825+yasutakeyohei@users.noreply.github.com> | 2026-09-21 07:49:21 +0900 |
| commit | 9328db082f2882fb8c2a37c4e73c39c7582e4709 (patch) | |
| tree | 8d11342c5d94816db388949c69a9e45968c0412d /src/pages | |
| parent | 80862e66000d79846df87ca9db55f7980a53a83f (diff) | |
og/[slug].jpg.ts: OGP画像をPNGからJPEGに変更
Diffstat (limited to 'src/pages')
| -rw-r--r-- | src/pages/og/[slug].jpg.ts (renamed from src/pages/og/[slug].png.ts) | 15 | ||||
| -rw-r--r-- | src/pages/og/index.astro | 4 |
2 files changed, 11 insertions, 8 deletions
diff --git a/src/pages/og/[slug].png.ts b/src/pages/og/[slug].jpg.ts index d40dead..f2015c4 100644 --- a/src/pages/og/[slug].png.ts +++ b/src/pages/og/[slug].jpg.ts @@ -1,5 +1,6 @@ import satori from "satori"; import { Resvg } from "@resvg/resvg-js"; +import sharp from "sharp"; import { readFileSync, existsSync, @@ -146,9 +147,9 @@ export async function GET({ params }: { params: { slug: string } }) { title = `${reiwa}${month}定例会`; } - // Check cache: if title hash matches, return cached PNG from node_modules/.og-cache + // Check cache: if title hash matches, return cached JPEG from node_modules/.og-cache const cacheDir = path.join(process.cwd(), "node_modules", ".og-cache"); - const cacheFile = path.join(cacheDir, `${slug}.png`); + const cacheFile = path.join(cacheDir, `${slug}.jpg`); const cacheJsonPath = path.join(cacheDir, ".og-cache.json"); let cache: Record<string, string> = {}; @@ -163,7 +164,7 @@ export async function GET({ params }: { params: { slug: string } }) { const cachedBuffer = readFileSync(cacheFile); return new Response(cachedBuffer, { headers: { - "Content-Type": "image/png", + "Content-Type": "image/jpeg", "Cache-Control": "public, max-age=31536000, immutable", }, }); @@ -296,16 +297,18 @@ export async function GET({ params }: { params: { slug: string } }) { const resvg = new Resvg(svg, { fitTo: { mode: "width", value: 1200 } }); const pngBuffer = resvg.render().asPng(); + // OGP は JPEG で出力する(PNGだと1枚300KBを超えるため、JPEGで約1/5にする) + const jpgBuffer = await sharp(pngBuffer).jpeg({ quality: 88 }).toBuffer(); // Save to cache in node_modules/.og-cache (persists across builds) if (!existsSync(cacheDir)) mkdirSync(cacheDir, { recursive: true }); - writeFileSync(cacheFile, pngBuffer); + writeFileSync(cacheFile, jpgBuffer); cache[slug] = titleHash; writeFileSync(cacheJsonPath, JSON.stringify(cache, null, 2)); - return new Response(pngBuffer, { + return new Response(jpgBuffer, { headers: { - "Content-Type": "image/png", + "Content-Type": "image/jpeg", "Cache-Control": "public, max-age=31536000, immutable", }, }); diff --git a/src/pages/og/index.astro b/src/pages/og/index.astro index 8916dcd..bbde23d 100644 --- a/src/pages/og/index.astro +++ b/src/pages/og/index.astro @@ -84,8 +84,8 @@ const slugs = [ <h1>OGP 画像一覧({slugs.length} 枚)</h1> <div class="grid"> {slugs.map((slug) => ( - <a href={`/og/${slug}.png`} class="card"> - <img src={`/og/${slug}.png`} alt={slug} loading="lazy" /> + <a href={`/og/${slug}.jpg`} class="card"> + <img src={`/og/${slug}.jpg`} alt={slug} loading="lazy" /> <span>{slug}.png</span> </a> ))} |
