Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shepherdjerred/astro-opengraph-images
Generate Open Graph images for your Astro site
https://github.com/shepherdjerred/astro-opengraph-images
astro open-graph resvg satori typescript
Last synced: 12 days ago
JSON representation
Generate Open Graph images for your Astro site
- Host: GitHub
- URL: https://github.com/shepherdjerred/astro-opengraph-images
- Owner: shepherdjerred
- License: gpl-3.0
- Created: 2024-06-27T13:12:30.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-12-29T17:19:46.000Z (24 days ago)
- Last Synced: 2025-01-02T16:50:09.680Z (20 days ago)
- Topics: astro, open-graph, resvg, satori, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/astro-opengraph-images
- Size: 5.61 MB
- Stars: 34
- Watchers: 1
- Forks: 2
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[![astro-opengraph-images](https://img.shields.io/npm/v/astro-opengraph-images.svg)](https://www.npmjs.com/package/astro-opengraph-images)
Generate Open Graph images for your Astro site.
This project is actively maintained. If you have a feature request or need help, please [create an issue](https://github.com/shepherdjerred/astro-opengraph-images/issues/new).
## What is Open Graph?
[Open Graph](https://ogp.me/) is a protocol created by Facebook. It allows pages on your site to be richly embedded into other sites and applications.
You've probably seen this in action when posting a link on Facebook, Twitter, Slack, iMessage, or Discord. Links posted in supported applications will display the Open Graph metadata which often includes an image. This library will generate those images for you.
## Features
> [!WARNING]
> This integration has only been tested with statically rendered sites. It is untested with server-side rendering.- Written in TypeScript
- Generate Open Graph images for every page on your site.
- Use a preset renderer to get started quickly.
- Images are fully customizable using [Satori](https://github.com/vercel/satori).
- Use React/JSX + Tailwind syntax or vanilla JavaScript to define your own custom images.
- Supports both static pages and Astro content collections.
- Pages can be written in Markdown, MDX, HTML, or any other format.## Quick Start
To better illustrate these steps, I've created a [video](https://www.loom.com/share/a66a65be8a4e48ec8612b78489db590d?sid=3e9f7cd6-68ec-49da-b942-75eb4608cb5e) following them to help others follow along.
1. Add this integration to your Astro config:
- Option 1: use the `astro` command:
```bash
npx astro add astro-opengraph-images
```- Option 2: install the package and add the integration to your Astro config:
```bash
npm i astro-opengraph-images
``````diff
+import opengraphImages from "astro-opengraph-images";export default defineConfig({
integrations: [
+ opengraphImages()
],
});
```1. Install React. React is used by the presets, and can be used to easily author custom images. Note that React is only used for generating the images and will not be shipped to clients.
```bash
npm i -D react
```1. Install the fonts you want to use. Fonts must be explicitly declared to be used for images. System fonts are _not_ available. For this quick start guide, we'll install the [Roboto](https://fontsource.org/fonts/roboto) font:
```bash
npm i @fontsource/roboto
```You can find more fonts on [Fontsource](https://fontsource.org/), or you can use any font file that you have. See [Satori's font documentation](https://github.com/vercel/satori?tab=readme-ov-file#fonts) for more information.
1. Configure the integration in your Astro config:
```diff
-import opengraphImages from "astro-opengraph-images";
+import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
- opengraphImages()
+ opengraphImages({
+ options: {
+ fonts: [
+ {
+ name: "Roboto",
+ weight: 400,
+ style: "normal",
+ data: fs.readFileSync("node_modules/@fontsource/roboto/files/roboto-latin-400-normal.woff"),
+ },
+ ],
+ },
+ render: presets.blackAndWhite,
+ }),
],
});
```1. Set the [`site`](https://docs.astro.build/en/reference/configuration-reference/#site) property in your Astro config:
Open Graph requires URLs to be absolute, including the domain your site is hosted at. This integration uses the site defined in your Astro config to create the correct URLs for Open Graph which is `site` must be defined.
```diff
export default defineConfig({
+ site: "https://.com",
integrations: [
opengraphImages({
options: {
fonts: [
{
name: "Roboto",
weight: 400,
style: "normal",
data: fs.readFileSync("node_modules/@fontsource/roboto/files/roboto-latin-400-normal.woff"),
},
],
},
render: presets.blackAndWhite,
}),
],
});
```1. Update your main Astro layout with the appropriate `meta` tags. The [Open Graph site](https://ogp.me/) has more information possible tags.
The following `meta` tags must be defined:
- `og:title`
- This field may be used when generating images.
- `og:type`
- See the [Open Graph documentation](https://ogp.me/#types) for valid values.
- `og:image`
- Set this to the return value of `getImagePath` (example shown below).
- If the value of `og:image` does not match what this integration expects then your site will fail to build. This will ensure your site is correctly configured to display Open Graph images.
- `og:description`
- Optional. This field may be used when generating images.Your site will fail to build if the tags above are not set.
- Option 1: Use the [`astro-seo`](https://github.com/jonasmerlin/astro-seo) package:
Install the `astro-seo` package:
```bash
npm i astro-seo
```Update your Astro layout to use the `SEO` component:
```diff
---
+import { SEO } from "astro-seo";
+import { getImagePath } from "astro-opengraph-images";interface Props {
title: string;
}const { title } = Astro.props;
+const { url, site } = Astro;
+const openGraphImageUrl = getImagePath({ url, site });
---
{title}+
```- Option 2: Manually add the `meta` tags to your Astro layout.
1. Build your site. You should see a `.png` file next to each `.html` page in your `dist` folder. Double-check that the `og:image` proprety in your `.html` file matches the path to the `.png` file.
1. Deploy your site. You can verify that your images are correct by:
- Sending a link to your site in an application that supports Open Graph like iMessage, Slack, Discord, etc.
- Visit [opengraph.xyz](http://opengraph.xyz/) and test your site.## Examples
There is an example site using this integration under [`examples/`](https://github.com/shepherdjerred/astro-opengraph-images/tree/main/example).
### Sites Using This
If you're using this project, [open a PR](https://github.com/shepherdjerred/astro-opengraph-images/compare) to add your site to this list.
- [sjer.red](https://sjer.red) ([source](https://github.com/shepherdjerred/sjer.red))
## Custom Renderers
You can create your own custom images with a render function. Take a look at how [a preset](https://github.com/shepherdjerred/astro-opengraph-images/blob/main/src/presets/blackAndWhite.tsx) works.
Renderers have access to the page's DOM using [jsdom](https://github.com/jsdom/jsdom). You can use this to render your Open Graph image using any of the content from the associated HTML page. An example of this is shown in the [custom property preset](https://github.com/shepherdjerred/astro-opengraph-images/blob/main/src/presets/customProperty.tsx) which shows a preview of the page's body text in the Open Graph image.
This library uses [Satori](https://github.com/vercel/satori) to convert React components to SVG. The SVG is then converted to a PNG using [resvg-js](https://github.com/yisibl/resvg-js).
> [!TIP]
> Satori supports [a subset of CSS](https://github.com/vercel/satori?tab=readme-ov-file#css). Be sure to familiarize yourself with its limitations.
>
> You can use the [Satori playground](https://og-playground.vercel.app/) to work on your images.
>
> You can use Tailwind syntax with [tw-to-css](https://github.com/vinicoder/tw-to-css). An example is the [Tailwind preset](https://github.com/shepherdjerred/astro-opengraph-images/blob/main/src/presets/tailwind.tsx). You'll need to install this package yourself.## Presets
Presets are located in [`src/presets/`](https://github.com/shepherdjerred/astro-opengraph-images/tree/main/src/presets). [Open a pull request](https://github.com/shepherdjerred/astro-opengraph-images/compare) to contribute a preset you've created.
Note: some presets use the [`tw-to-css`](https://github.com/vinicoder/tw-to-css) library. You'll need to install this dependency separately when using one of these presets. You'll see an error if the library is not already installed.
```bash
npm i tw-to-css
```### `backgroundImage`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.backgroundImage,
}),
],
});
```![](assets/presets/backgroundImage.png)
### `blackAndWhite`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.blackAndWhite,
}),
],
});
```![](assets/presets/blackAndWhite.png)
### `brandedLogo`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.brandedLogo,
}),
],
});
```![](assets/presets/brandedLogo.png)
### `customProperty`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.customProperty,
}),
],
});
```![](assets/presets/customProperty.png)
### `gradients`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.gradients,
}),
],
});
```![](assets/presets/gradients.png)
### `podcast`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.podcast,
}),
],
});
```![](assets/presets/podcast.png)
### `rauchg`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.rauchg,
}),
],
});
```![](assets/presets/rauchg.png)
### `simpleBlog`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.simpleBlog,
}),
],
});
```![](assets/presets/simpleBlog.png)
### `tailwind`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.tailwind,
}),
],
});
```![](assets/presets/tailwind.png)
### `vercel`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.vercel,
}),
],
});
```![](assets/presets/vercel.png)
### `waveSvg`
```diff
import opengraphImages, { presets } from "astro-opengraph-images";export default defineConfig({
integrations: [
opengraphImages({
+ render: presets.waveSvg,
}),
],
});
```![](assets/presets/waveSvg.png)
## Alternatives
Here are some similar libraries using Satori and Astro. I haven't done a feature comparison.
- https://github.com/florian-lefebvre/satori-astro (This library looks excellent)
- https://github.com/delucis/astro-og-canvas (Doesn't allow arbitrary layouts)
- https://github.com/thewebforge/astro-og-images (Only allows you to choose from a list of templates)
- https://github.com/tomaskebrle/astro-og-image (Seems limited)
- https://github.com/cijiugechu/astro-satori (Possibly dead, hasn't been updated in a year)
- https://github.com/kevinzunigacuellar/astro-satori (Possibly dead, hasn't been updated in a year)
- https://github.com/rumaan/astro-vercel-og (Possibly dead, hasn't been updated in a year)