{"id":15062422,"url":"https://github.com/shepherdjerred/astro-opengraph-images","last_synced_at":"2025-05-07T16:06:15.676Z","repository":{"id":246396021,"uuid":"820930467","full_name":"shepherdjerred/astro-opengraph-images","owner":"shepherdjerred","description":"Generate Open Graph images for your Astro site","archived":false,"fork":false,"pushed_at":"2024-12-29T17:19:46.000Z","size":5878,"stargazers_count":34,"open_issues_count":5,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-02T16:50:09.680Z","etag":null,"topics":["astro","open-graph","resvg","satori","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/astro-opengraph-images","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shepherdjerred.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-27T13:12:30.000Z","updated_at":"2024-12-29T17:19:49.000Z","dependencies_parsed_at":"2024-08-11T05:45:12.069Z","dependency_job_id":"e42a4877-9ea3-4e05-830d-2a3134a1523f","html_url":"https://github.com/shepherdjerred/astro-opengraph-images","commit_stats":null,"previous_names":["shepherdjerred/astro-satori","shepherdjerred/astro-opengraph-images"],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shepherdjerred%2Fastro-opengraph-images","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shepherdjerred%2Fastro-opengraph-images/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shepherdjerred%2Fastro-opengraph-images/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shepherdjerred%2Fastro-opengraph-images/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shepherdjerred","download_url":"https://codeload.github.com/shepherdjerred/astro-opengraph-images/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":233352214,"owners_count":18663254,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["astro","open-graph","resvg","satori","typescript"],"created_at":"2024-09-24T23:35:45.344Z","updated_at":"2025-05-07T16:06:15.658Z","avatar_url":"https://github.com/shepherdjerred.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cpicture\u003e\n    \u003csource media=\"(prefers-color-scheme: dark)\" srcset=\"https://cdn.rawgit.com/shepherdjerred/astro-opengraph-images/main/assets/logo-dark.png\"\u003e\n    \u003csource media=\"(prefers-color-scheme: light)\" srcset=\"https://cdn.rawgit.com/shepherdjerred/astro-opengraph-images/main/assets/logo-light.png\"\u003e\n    \u003cimg alt=\"project logo\" src=\"https://cdn.rawgit.com/shepherdjerred/astro-opengraph-images/main/assets/logo-light.png\" height=75\u003e\n  \u003c/picture\u003e\n\n[![astro-opengraph-images](https://img.shields.io/npm/v/astro-opengraph-images.svg)](https://www.npmjs.com/package/astro-opengraph-images)\n\nGenerate Open Graph images for your Astro site.\n\nThis 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).\n\n\u003c/div\u003e\n\n## What is Open Graph?\n\n[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.\n\nYou'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.\n\n## Features\n\n\u003e [!WARNING]\n\u003e This integration has only been tested with statically rendered sites. It is untested with server-side rendering.\n\n- Written in TypeScript\n- Generate Open Graph images for every page on your site.\n- Use a preset renderer to get started quickly.\n- Images are fully customizable using [Satori](https://github.com/vercel/satori).\n- Use React/JSX + Tailwind syntax or vanilla JavaScript to define your own custom images.\n- Supports both static pages and Astro content collections.\n- Pages can be written in Markdown, MDX, HTML, or any other format.\n\n## Quick Start\n\nTo 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.\n\n1. Add this integration to your Astro config:\n\n   - Option 1: use the `astro` command:\n\n     ```bash\n     npx astro add astro-opengraph-images\n     ```\n\n   - Option 2: install the package and add the integration to your Astro config:\n\n     ```bash\n     npm i astro-opengraph-images\n     ```\n\n     ```diff\n     +import opengraphImages from \"astro-opengraph-images\";\n\n     export default defineConfig({\n       integrations: [\n     +    opengraphImages()\n       ],\n     });\n     ```\n\n1. 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.\n\n   ```bash\n   npm i -D react\n   ```\n\n1. 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:\n\n   ```bash\n   npm i @fontsource/roboto\n   ```\n\n   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.\n\n1. Configure the integration in your Astro config:\n\n   ```diff\n   -import opengraphImages from \"astro-opengraph-images\";\n   +import opengraphImages, { presets } from \"astro-opengraph-images\";\n\n   export default defineConfig({\n     integrations: [\n   -    opengraphImages()\n   +    opengraphImages({\n   +      options: {\n   +        fonts: [\n   +          {\n   +            name: \"Roboto\",\n   +            weight: 400,\n   +            style: \"normal\",\n   +            data: fs.readFileSync(\"node_modules/@fontsource/roboto/files/roboto-latin-400-normal.woff\"),\n   +          },\n   +        ],\n   +      },\n   +      render: presets.blackAndWhite,\n   +    }),\n     ],\n   });\n   ```\n\n1. Set the [`site`](https://docs.astro.build/en/reference/configuration-reference/#site) property in your Astro config:\n\n   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.\n\n   ```diff\n   export default defineConfig({\n   +  site: \"https://\u003cyour site\u003e.com\",\n     integrations: [\n       opengraphImages({\n         options: {\n           fonts: [\n             {\n               name: \"Roboto\",\n               weight: 400,\n               style: \"normal\",\n               data: fs.readFileSync(\"node_modules/@fontsource/roboto/files/roboto-latin-400-normal.woff\"),\n             },\n           ],\n         },\n         render: presets.blackAndWhite,\n       }),\n     ],\n   });\n   ```\n\n1. Update your main Astro layout with the appropriate `meta` tags. The [Open Graph site](https://ogp.me/) has more information possible tags.\n\n   The following `meta` tags must be defined:\n\n   - `og:title`\n     - This field may be used when generating images.\n   - `og:type`\n     - See the [Open Graph documentation](https://ogp.me/#types) for valid values.\n   - `og:image`\n     - Set this to the return value of `getImagePath` (example shown below).\n     - 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.\n   - `og:description`\n     - Optional. This field may be used when generating images.\n\n   Your site will fail to build if the tags above are not set.\n\n   - Option 1: Use the [`astro-seo`](https://github.com/jonasmerlin/astro-seo) package:\n\n     Install the `astro-seo` package:\n\n     ```bash\n     npm i astro-seo\n     ```\n\n     Update your Astro layout to use the `SEO` component:\n\n     ```diff\n     ---\n     +import { SEO } from \"astro-seo\";\n     +import { getImagePath } from \"astro-opengraph-images\";\n\n     interface Props {\n       title: string;\n     }\n\n     const { title } = Astro.props;\n     +const { url, site } = Astro;\n     +const openGraphImageUrl = getImagePath({ url, site });\n     ---\n\n     \u003c!doctype html\u003e\n     \u003chtml lang=\"en\"\u003e\n       \u003chead\u003e\n         \u003cmeta charset=\"UTF-8\" /\u003e\n         \u003cmeta name=\"description\" content=\"Astro description\" /\u003e\n         \u003cmeta name=\"viewport\" content=\"width=device-width\" /\u003e\n         \u003clink rel=\"icon\" type=\"image/svg+xml\" href=\"/favicon.svg\" /\u003e\n         \u003cmeta name=\"generator\" content={Astro.generator} /\u003e\n         \u003ctitle\u003e{title}\u003c/title\u003e\n\n     +    \u003cSEO\n     +      openGraph={\n     +        {\n     +          basic: {\n     +            title: title,\n     +            type: \"website\",\n     +            image: openGraphImageUrl,\n     +            url: url,\n     +          },\n     +          optional: {\n     +            description: \"My page description\",\n     +          },\n     +        }\n     +      }\n     +    /\u003e\n       \u003c/head\u003e\n       \u003cbody\u003e\n         \u003cslot /\u003e\n       \u003c/body\u003e\n     \u003c/html\u003e\n     ```\n\n   - Option 2: Manually add the `meta` tags to your Astro layout.\n\n1. 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.\n\n1. Deploy your site. You can verify that your images are correct by:\n\n   - Sending a link to your site in an application that supports Open Graph like iMessage, Slack, Discord, etc.\n   - Visit [opengraph.xyz](http://opengraph.xyz/) and test your site.\n\n## Examples\n\nThere are example sites using this integration under [`examples/`](https://github.com/shepherdjerred/astro-opengraph-images/tree/main/examples).\n\n### Sites Using This\n\nIf you're using this project, [open a PR](https://github.com/shepherdjerred/astro-opengraph-images/compare) to add your site to this list.\n\n- [sjer.red](https://sjer.red) ([source](https://github.com/shepherdjerred/sjer.red))\n\n## Custom Renderers\n\nYou 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.\n\nRenderers 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.\n\nThis 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).\n\n\u003e [!TIP]\n\u003e Satori supports [a subset of CSS](https://github.com/vercel/satori?tab=readme-ov-file#css). Be sure to familiarize yourself with its limitations.\n\u003e\n\u003e You can use the [Satori playground](https://og-playground.vercel.app/) to work on your images.\n\u003e\n\u003e 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.\n\n### Loading Local Images\n\nYou may receive an error when setting the source attribute on an `img` tag, claiming you need an absolute URL. When using a custom renderer, you can load local images to use in your final image using the following syntax in your `.tsx` file:\n\n```javascript\nconst filePath = path.join(process.cwd(), \"src\", \"assets\", \"my-image.png\");\nconst imageBase64 = `data:image/png;base64,${fs.readFileSync(filePath).toString(\"base64\")}`;\n```\n\nThe code above will load the image at `src/assets/my-image.png` and convert it to a base64 string which can be passed to an image tag's `src` attribute. Be sure to update the `data:image/png;base64,` part of the string to match the image's type and that the image's file ath is correct.\n\n```jsx\n\u003cimg\n  style={{\n    ...twj(\"absolute inset-0 w-full h-full\"),\n    ...{ objectFit: \"cover\" },\n  }}\n  src={imageBase64}\n/\u003e\n```\n\nHere's a complete example of a custom renderer with a locally loaded image:\n\n```tsx\nimport type { RenderFunctionInput } from \"astro-opengraph-images\";\nconst { twj } = await import(\"tw-to-css\");\n\n// Remember to import these modules\nimport path from \"path\";\nimport * as fs from \"node:fs\";\n\nconst filePath = path.join(\n  process.cwd(),\n  \"src\",\n  \"assets\",\n  \"my-image.png\",\n);\nconst imageBase64 = `data:image/png;base64,${fs.readFileSync(filePath).toString(\"base64\")}`;\n\n// from https://fullstackheroes.com/resources/vercel-og-templates/simple/\nexport async function simpleBlog({ title, description }: RenderFunctionInput): Promise\u003cReact.ReactNode\u003e {\n  return Promise.resolve(\n    \u003cdiv style={twj(\"h-full w-full flex items-start justify-start border border-blue-500 border-[12px] bg-gray-50\")}\u003e\n      \u003cdiv style={twj(\"flex items-start justify-start h-full\")}\u003e\n        \u003c!-- Locally loaded Image Used Here --\u003e\n        \u003cimg\n          style={{\n            ...twj(\"absolute inset-0 w-full h-full\"),\n            ...{ objectFit: \"cover\" },\n          }}\n          src={imageBase64}\n        /\u003e\n        \u003cdiv style={twj(\"flex flex-col justify-between w-full h-full\")}\u003e\n          \u003ch1 style={twj(\"text-[80px] p-20 font-black text-left\")}\u003e{title}\u003c/h1\u003e\n          \u003cdiv style={twj(\"text-2xl pb-10 px-20 font-bold mb-0\")}\u003e{description}\u003c/div\u003e\n        \u003c/div\u003e\n      \u003c/div\u003e\n    \u003c/div\u003e,\n  );\n}\n```\n\n## Presets\n\nPresets 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.\n\nNote: 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.\n\n```bash\nnpm i tw-to-css\n```\n\n### `backgroundImage`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.backgroundImage,\n    }),\n  ],\n});\n```\n\n![](assets/presets/backgroundImage.png)\n\n### `blackAndWhite`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.blackAndWhite,\n    }),\n  ],\n});\n```\n\n![](assets/presets/blackAndWhite.png)\n\n### `brandedLogo`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.brandedLogo,\n    }),\n  ],\n});\n```\n\n![](assets/presets/brandedLogo.png)\n\n### `customProperty`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.customProperty,\n    }),\n  ],\n});\n```\n\n![](assets/presets/customProperty.png)\n\n### `gradients`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.gradients,\n    }),\n  ],\n});\n```\n\n![](assets/presets/gradients.png)\n\n### `podcast`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.podcast,\n    }),\n  ],\n});\n```\n\n![](assets/presets/podcast.png)\n\n### `rauchg`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.rauchg,\n    }),\n  ],\n});\n```\n\n![](assets/presets/rauchg.png)\n\n### `simpleBlog`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.simpleBlog,\n    }),\n  ],\n});\n```\n\n![](assets/presets/simpleBlog.png)\n\n### `tailwind`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.tailwind,\n    }),\n  ],\n});\n```\n\n![](assets/presets/tailwind.png)\n\n### `vercel`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.vercel,\n    }),\n  ],\n});\n```\n\n![](assets/presets/vercel.png)\n\n### `waveSvg`\n\n```diff\nimport opengraphImages, { presets } from \"astro-opengraph-images\";\n\nexport default defineConfig({\n  integrations: [\n    opengraphImages({\n+      render: presets.waveSvg,\n    }),\n  ],\n});\n```\n\n![](assets/presets/waveSvg.png)\n\n## Alternatives\n\nHere are some similar libraries using Satori and Astro. I haven't done a feature comparison.\n\n- https://github.com/florian-lefebvre/satori-astro (This library looks excellent)\n- https://github.com/delucis/astro-og-canvas (Doesn't allow arbitrary layouts)\n- https://github.com/thewebforge/astro-og-images (Only allows you to choose from a list of templates)\n- https://github.com/tomaskebrle/astro-og-image (Seems limited)\n- https://github.com/cijiugechu/astro-satori (Possibly dead, hasn't been updated in a year)\n- https://github.com/kevinzunigacuellar/astro-satori (Possibly dead, hasn't been updated in a year)\n- https://github.com/rumaan/astro-vercel-og (Possibly dead, hasn't been updated in a year)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshepherdjerred%2Fastro-opengraph-images","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshepherdjerred%2Fastro-opengraph-images","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshepherdjerred%2Fastro-opengraph-images/lists"}