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

og-img

v0.2.1

Published

Generate dynamic Open Graph images for your website

Downloads

459

Readme

og-img

This is a framework agnostic package for generating Open Graph images using Satori and resvg. Built using Web APIs, this package can be executed with Node.js and on the edge. You can use this package to add dynamic Open Graph images to your SvelteKit, Astro, SolidStart or Qwik website.

The difference to @vercel/og is that this package loads the WebAssembly module needed to convert SVG to PNG lazily at runtime and provides a framework agnostic workaround for defining the content of the image using satori-html.

Installation

This library is available for Node and Bun.

npm install og-img    # npm
yarn add og-img       # yarn
pnpm add og-img       # pnpm
bun add og-img        # bun

How it works

To generate an image, all you need to do is return an ImageResponse via a server endpoint (it probably will not work with SSG). You can use html to easily define the content of your image.

To get proper syntax highlighting for the tagged template literal in Visual Studio Code, you can install the lit-html extension.

import { fetchFont, ImageResponse, html } from 'og-img';

// With SvelteKit
export async function GET() {
  return new ImageResponse(
    // Use Tailwind CSS or style attribute
    html`
      <div tw="text-4xl text-green-700" style="background-color: tan">
        Hello, world!
      </div>
    `,
    {
      width: 1200,
      height: 600,
      fonts: [
        {
          name: 'Roboto',
          // Use `fs` (Node.js only) or `fetch` to read font file
          data: await fetchFont('https://www.example.com/fonts/roboto-400.ttf'),
          weight: 400,
          style: 'normal',
        },
      ],
    }
  );
}

// With Qwik
export const onGet = async ({ send }) => {
  send(
    new ImageResponse(
      // Use Tailwind CSS or style attribute
      html`
        <div tw="text-4xl text-green-700" style="background-color: tan">
          Hello, world!
        </div>
      `,
      {
        width: 1200,
        height: 600,
        fonts: [
          {
            name: 'Roboto',
            // Use `fs` (Node.js only) or `fetch` to read font file
            data: await fetchFont(
              'https://www.example.com/fonts/roboto-400.ttf'
            ),
            weight: 400,
            style: 'normal',
          },
        ],
      }
    )
  );
};

Then all you need to do is point to your API endpoint with a meta tag in the head of your website to embed the Open Graph image.

<head>
  <title>Hello, world!</title>
  <meta property="og:image" content="https://www.example.com/og-image" />
</head>

You can use URL parameters to dynamically change the content of your Open Graph image. Take a look at Valibot's Open Graph image. You can find the source code here.

Credits

  • Satori: https://github.com/vercel/satori
  • Satori HTML: https://github.com/natemoo-re/satori-html
  • resvg: https://github.com/RazrFalcon/resvg
  • resvg-js: https://github.com/yisibl/resvg-js

Feedback

Find a bug or have an idea how to improve the library? Please fill out an issue. Together we can make the library even better!

License

This project is available free of charge and licensed under the MPL-2.0 license.