Skip to content

Commit 0620640

Browse files
authored
Generate OG images as WebP (withastro#7940)
1 parent 42acee5 commit 0620640

File tree

4 files changed

+63
-48
lines changed

4 files changed

+63
-48
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
"astro": "^4.6.3",
4242
"astro-auto-import": "^0.4.2",
4343
"astro-eslint-parser": "^0.16.0",
44-
"astro-og-canvas": "^0.4.1",
44+
"astro-og-canvas": "^0.5.0",
4545
"bcp-47-normalize": "^2.1.0",
46-
"canvaskit-wasm": "^0.37.0",
46+
"canvaskit-wasm": "^0.39.1",
4747
"dedent-js": "^1.0.1",
4848
"domhandler": "^4.3.1",
4949
"eslint": "^8.29.0",

pnpm-lock.yaml

Lines changed: 51 additions & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pages/open-graph/[...path].ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,18 @@ export const { getStaticPaths, GET } = OGImageRoute({
1919

2020
pages,
2121

22+
getSlug(path) {
23+
path = path.replace(/^\/src\/pages\//, '');
24+
path = path.replace(/\.[^.]*$/, '') + '.webp';
25+
path = path.replace(/\/index\.webp$/, '.webp');
26+
return path;
27+
},
28+
2229
getImageOptions: async (_, { data, slug }: (typeof pages)[string]): Promise<OGImageOptions> => {
2330
const isRtl = rtlLanguages.has(getLangFromSlug(slug));
2431
return {
32+
format: 'WEBP',
33+
quality: 90,
2534
title: data.title,
2635
description: data.description,
2736
dir: isRtl ? 'rtl' : 'ltr',

src/util/getOgImageUrl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const paths = new Set(routes.map(({ params }) => params.path));
1313
* @returns Path to the OpenGraph image if found. Otherwise, `undefined`.
1414
*/
1515
export function getOgImageUrl(path: string, isFallback: boolean): string | undefined {
16-
let imagePath = path.replace(/^\//, '').replace(/\/$/, '') + '.png';
16+
let imagePath = path.replace(/^\//, '').replace(/\/$/, '') + '.webp';
1717
if (isFallback) {
1818
// Replace the language segment with 'en' for fallback pages.
1919
imagePath = 'en' + imagePath.slice(imagePath.indexOf('/'));

0 commit comments

Comments
 (0)