Git Product home page Git Product logo

og-img's Introduction

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

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.

og-img's People

Contributors

fabian-hiller avatar juliusmarminge avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

timothymiller

og-img's Issues

Empty image response w/ Qwik on Vercel Edge

Hi @fabian-hiller ๐Ÿ‘‹ Many thanks for creating this lib! I noticed an error that I can't resolve, so maybe it's an issue with my setup or a bug.

I'm dynamically generating open graph images in my Qwik City site with custom endpoints.

This is working fine locally both during development (npm run dev) as well as preview (npm run preview).

If deployed with the Vercel Edge Network adapter for Qwik City, the response from this endpoint is always empty (but a 200, no error is thrown). Example: og-image. Notice that the response is correctly using content-type image/png according to DevTools.

Therefore I'm wondering if I made a mistake (I should be following the guide) or if a deployment on the Vercel Edge network causes the ImageResponse to be empty.


Edit: do you have a working example with Qwik on Vercel?

Og-image is empty when running SSG with Qwik

Probably i'm missing something, but for some reason, when I run a build of a Qwik website, that generates an empty og-image file, even locally.

I'm currently deploying the website over Github Pages, and everytime I make a request towards website-url/og-image an empty file is downloaded.

I don't know what should be the correct behavior, probably this won't work with SSG?

Thanks a lot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.