delucis / astro-og-canvas

Generate OpenGraph images for your Astro site
153 stars 17 forks source link

bug: format isn't considered when writing to disk #66

Open jbergstroem opened 1 month ago

jbergstroem commented 1 month ago

Using a config that looks something like:

{
  title: page.data.title,
  description: page.data.description,
  format: "WEBP",
}

..you will notice that the output files are still written as a .png:

~/w/e/t/docs main~ 21.3s $ file dist/og/analytics.png
dist/og/analytics.png: RIFF (little-endian) data, Web/P image

..due to slug not using the format input

delucis commented 1 month ago

Thanks for the issue! Yes, I noticed this too recently. As a workaround it is possible to set the getSlug() function manually and use .webp (here’s an example), but it would indeed be nice to make this work automatically!

jbergstroem commented 1 month ago

Thanks for the issue! Yes, I noticed this too recently. As a workaround it is possible to set the getSlug() function manually and use .webp (here’s an example), but it would indeed be nice to make this work automatically!

Since Work Must Go On thats what I ended up carrying with a pnpm patch approach, but yeah. I can do a PR if you want, lmk if you have input on how you want the abi to look/change.

delucis commented 1 month ago

Oh, you shouldn’t need a pnpm patch! In the example I linked, getSlug() is passed as an option to astro-og-canvas, which overrides the default one:

import { OGImageRoute } from 'astro-og-canvas';

export const { getStaticPaths, GET } = OGImageRoute({
  // ...
  getSlug: getSlugThatReturnsWebPExtensions,
  // ...
})

IIRC the issue currently is that slugs need to be determined before image options are known, so getSlug() doesn’t actually know yet what format will be set. We probably need to make a choice to do something like move format up to the top level and not support setting format differently for every image (which in any case seems like a pretty unusual requirement).

jbergstroem commented 1 month ago

In the example I linked, getSlug() is passed as an option to astro-og-canvas, which overrides the default one:

Ah, I didn't look long enough to realize I can pass my own slug function. Will switch to this instead. Thank you.