npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@altano/astro-opengraph-image

v0.0.1-alpha.5

Published

Render Open Graph Images from Astro Components

Downloads

15

Readme

⚠️ WORK IN PROGRESS: This package requires changes^filename-change in Astro before it will be useable. Sit tight!

astro-opengraph-image

This is an Astro integration that lets you turn any Astro component (or framework component) into an Open Graph image for your Astro site.

Unlike existing Astro integrations for Open Graph images, this one:

  • Doesn't require any out-of-band screenshotting. It's all Astro native, so to speak.
  • Can turn ANY component into an Open Graph image. You're in full control of the html/css.

Prerequisites

  • This integration is for Astro.

Installation

In your existing Astro project:

# Using NPM
npx astro add @altano/astro-opengraph-image
# Using Yarn
yarn astro add @altano/astro-opengraph-image
# Using PNPM
pnpm astro add @altano/astro-opengraph-image

Configuration

You'll need to configure the integration in your Astro config. At the very least, you must provide some fonts to use (as there are no defaults).

export default defineConfig({
  integrations: [
    opengraphImage({
      async getSvgOptions() {
        return {
          fonts: [...],
        };
      },
    }),
  ],
});

Create a component to convert to an image. It must have a .png.astro extension, e.g. image.png.astro:

<html><body>Hello!</body></html>

NOTE: Your Astro component must be HTML elements and styles supported by Satori, e.g. it can't be stateful or use calc() in css. The OG Image Playground is a great place to test your component before copying it into your Astro project.

Lastly, in any pages/layouts that have a opengraph-image.png.astro in that route, you need to add the <OpenGraphMeta /> component to generate opengraph meta tags in your head, e.g.:

---
import OpenGraphMeta from "@altano/astro-opengraph-image/components/meta.astro";
---

<html>
  <head>
    <OpenGraphMeta />
  </head>
  <body>
    <p>My opengraph-image should be the root one!</p>
  </body>
</html>

Options Reference

The integration requires the following options:

  • getSvgOptions.fonts: an array of fonts that will be used in your image component. Each font requires:
    • name: This is whatever you reference in your css, e.g. Inter
    • path: A string path to your font file. Can be in your node_modules folder, e.g. "node_modules/@fontsource/inter/files/inter-latin-400-normal.woff"
    • weight: A weight, from 100 to 900. You can provide different fonts for different weights.

See the TypeScript type-hints and comments for more info.

Examples

Using custom fonts

astro.config.mts:

import { defineConfig } from "astro/config";
import opengraphImage from "@altano/astro-opengraph-image";

// https://astro.build/config
export default defineConfig({
  integrations: [
    opengraphImage({
      async getSvgOptions() {
        return {
          fonts: [
            {
              name: "Inter",
              path: "node_modules/@fontsource/inter/files/inter-latin-400-normal.woff",
              weight: 400,
              style: "normal",
            },
            {
              name: "Inter",
              path: "node_modules/@fontsource/inter/files/inter-latin-800-normal.woff",
              weight: 800,
              style: "normal",
            },
          ],
        };
      },
    }),
  ],
});

src/pages/opengraph-image.png.astro:

---
/**
 * This is not used during image generation. It is only here
 * to make the fonts consistent between the generated image
 * and how the component is rendered if the image generation
 * middleware is disabled.
 */
import "@fontsource-variable/inter";
---

<html>
  <body
    style=`font-family: "Inter Variable";
           background: white;
           height: 100vh;
           width: 100vw;
           display: flex;
           flex-direction: column;
           align-items: center;
           justify-content: center;`
  >
    <h1
      style="font-weight: 800;
             font-size: 5rem;
             margin: 0;"
    >
      My Website!
    </h1>
    <p style="font-weight: 400;
              font-size: 2rem;">
      This is rendered as a PNG image.
    </p>
  </body>
</html>

src/pages/index.astro

<html>
  <head>
    <title>Homepage</title>
    <OpenGraphMeta title="My Website" description="This is a website." />
  </head>
  <body>
    ...
  </body>
</html>

See https://github.com/altano/npm-packages/tree/main/examples/astro-opengraph-image for a slightly more involved example.

Serving another opengraph-image

If your opengraph-image.png.astro is somewhere else, you can specify a directory relative to the current request URL, e.g. to point at an opengraph-image.png.astro file you've put at the root of your site:

<html>
  <head>
    <title>My opengraph image is at the site root</title>
    <OpenGraphMeta directory="/" />
  </head>
</html>

Adding title/description

Lastly, for convenience, you can optionally pass in title and/or description to get the og:title and og:description meta tags:

<OpenGraphMeta title="Nope" description="I'm not important" />

Dynamic Routes

If you have a dynamic route such as ./src/pages/[...slug].astro, convert it to ./src/pages/[...slug]/index.astro

Along-side your dynamic route (e.g. ./src/pages/[...slug]/index.astro) add ./src/pages/[...slug]/opengraph-image.png.astro:

src/
├─ pages/
│  ├─ [...slug]/
│  │  ├─ index.astro
│  │  ├─ opengraph-image.png.astro

Within opengraph-image.png.astro you can either duplicate your implementation of getStaticPaths or you can re-export the same one used in index.astro:

---
import { getStaticPaths as gsp } from "./index.astro";
export const getStaticPaths = gsp;
---

How it Works

This library is a tiny wrapper around @altano/astro-html-to-image which can convert any html response in Astro into an image.