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

gatsby-plugin-dynamic-open-graph-images

v1.1.4

Published

Build dynamic open-graph preview images for gatsby pages. Simply use react components to create SEO ready preview images for Twitter, Facebook, Linkedin, Google, and many more.

Downloads

52

Readme

Gatsby plugin for dynamic open graph images

npm npm

A Gatsby plugin to derive and generate images for the Open Graph Protocol directly from React Components.


📌 NOTICE

This plugin originates from gatsby-plugin-open-graph-images and uses the same approach and usage. The only difference is the internal implementation. It creates open graph images relying only on file system.


How to install

npm i gatsby-plugin-dynamic-open-graph-images

How to use

  1. Place the plugin in your main gatsby-config.js

    plugins: [`gatsby-plugin-dynamic-open-graph-images`];
  2. The creation of Open Graph images is done by createOpenGraphImage() within your gatsby-node.js file.

    Example

    const { createOpenGraphImage } = require("gatsby-plugin-dynamic-open-graph-images");
    
    exports.createPages = async ({ actions }) => {
      const { createPage } = actions;
    
      const openGraphImage = createOpenGraphImage(createPage, {
        component: path.resolve(`src/templates/index.og-image.js`),
        size: {
          width: 400,
          height: 50,
        },
      });
    };

    You can also pass open-graph image metadata as pageContext and simply use it within your page or component.

    Example

    • gatsby-node.js
    const { createOpenGraphImage } = require("gatsby-plugin-dynamic-open-graph-images");
    
    exports.createPages = async ({ actions, graphql }) => {
      const { createPage } = actions;
    
      // query data
      const result = await graphql(...)
    
      // get posts data from query result
      const posts = result.data.allMdx.edges;
    
      posts.forEach(({ node }) => {
        createPage({
          path: node.frontmatter.slug,
          component: postTemplate,
          context: {
            // pass open-graph image metadata as pageContext
            ogImage: createOpenGraphImage(createPage, {
              component: postOgImageTemplate,
              context: {
                id: node.id,
                title: node.frontmatter.title,
                ...
              },
            }),
          },
        });
      });
    };
    • Within your page or component
    export const Head = ({ location, pageContext }) => {
      const { ogImage } = pageContext;
    
      return (
        <SEO
          ogImage={ogImage}
          ...
        />
      )
    }
    
    const SEO = ({ ogImage }) => {
      return (
        <>
          <meta property="og:image" content={domain + ogImage.imagePath} />
          <meta property="og:image:width" content="400" />
          <meta property="og:image:width" content="50" />
        </>
      );
    }

API

createOpenGraphImage(createPage, options)

Parameters

| parameter | Required | description | | ---------- | -------- | ---------------------------------------------------------------------------------------------------- | | createPage | O | Gatsby createPage action | | options | O | |

options

| option | Required | type | description | default | | ----------- | -------- | --------------------------------- | ------------------------------------------------------------------------------------------------------------- | ----------------------------- | | component | O | string | The absolute path of React component for open-graph iamge template. It receives context value as pageContext. | N/A | | context | O | object | Gatsby page context. id property must be provided to distinguish components/images. | N/A | | size | X | { width: number, height: number } | The size for the generated image. | { width: 1200, height: 630} | | outputDir | X | string | The directory where the rendered gatsby components are temporarily stored, to be later saved as images. | "__og-image" |

Returns

Open-graph image metadata

{
  componentPath: '__og-image/c6f9bb',
  imagePath: '__og-image/c6f9bb.png',
  size: { width: 1200, height: 630 }
}

Note

If you use plugins that iterate over your pages, like gatsby-plugin-sitemap, exclude the outputDir:

{
  resolve: `gatsby-plugin-sitemap`,
  options: {
    exclude: [`/__og-image/*`],
  },
},