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-cloudinary-social-cards

v1.0.2

Published

Automatically generate social sharing images for your Gatsby pages using Cloudinary.

Downloads

8

Readme

gatsby-plugin-cloudinary-social-cards

Add social sharing cards to your Gatsby sites using Cloudinary!

This plugin will:

  1. Look for a template image in your repo
  2. Upload that template to your Cloudinary account
  3. Automatically create social media sharing images with the title (and optional tagline) of your post

Installation

# install the plugin and its peer dependencies
npm install gatsby-plugin-cloudinary-social-cards react-helmet gatsby-plugin-react-helmet

Create a new API key and secret in your Cloudinary console and set the following environment variables (create a file called .env locally):

CLOUDINARY_API_KEY=<your API key>
CLOUDINARY_API_SECRET=<your API secret>

Next, configure the plugin in your gatsby-config.js:

require('dotenv').config();

module.exports = {
  plugins: [
    'gatsby-plugin-react-helmet',
    {
      resolve: 'gatsby-plugin-cloudinary-social-cards',
      options: {
        cloudName: 'jlengstorf',
        apiKey: process.env.CLOUDINARY_API_KEY,
        apiSecret: process.env.CLOUDINARY_API_SECRET,
        imageTemplate: 'src/assets/social-card-template.jpg',
      },
    },
  ],
};

Configuration

Option | Required | Default | Description ------ | -------- | ------- | ----------- cloudName | true | | Your Cloudinary cloud name (usually your account name). apiKey | true | | Your Cloudinary API key (get one here). apiSecret | true | | Your Cloudinary API secret (get one here). imageTemplate | true | | Path to the social card image template. This should be a local file in your repo. uploadFolder | | null | Optional subfolder where the template should be uploaded in your Cloudinary account. imageOptions | | {} | Additional settings for your template image.

If you need a template, I wrote a post on creating a social sharing template image.

imageOptions

The options passed in imageOptions can be any of the available options for the @jlengstorf/get-share-image utility.

NOTE: The cloudName, apiKey, apiSecret, title, tagline, and imagePublicID settings will be overridden by the settings in the plugin.

Usage

There are two ways to use this plugin:

  1. Import the GatsbySocialImage component
  2. Use the getSocialCard GraphQL query

Import the GatsbySocialImage component

In most cases, the easiest solution is to use the built-in React component:

import React from 'react';
import { Helmet } from 'react-helmet';
import { GatsbySocialImage } from 'gatsby-plugin-cloudinary-social-cards';

export default () => {
  const title = 'This Page Boops';
  const tagline = 'I hope you like corgis.';

  return (
    <>
      <Helmet>
        <title>{title}</title>
      </Helmet>
      <GatsbySocialImage title={title} tagline={tagline} />
      <h1>{title}</h1>
      <p>{tagline}</p>
    </>
  );
};

The component adds an og:image meta tag to the page:

Rendered Gatsby page with the GatsbySocialImage component.

The generated social card looks like this:

Card with the Learn With Jason logo that says, “This page boops. I hope you like corgis.”

Use the getSocialCard GraphQL query

You can also query for the image URL, which is an alternative for dynamically generated pages.

In gatsby-node.js:

const pages = [
  {
    path: '/test1',
    title: 'The First Test Page',
    tagline: 'This page needs a snappy tagline.',
  },
  {
    path: '/test2',
    title: 'Another Test Page',
  },
];

exports.createPages = ({ actions }) => {
  pages.forEach((page) => {
    actions.createPage({
      path: page.path,
      component: require.resolve('./src/templates/dynamic.js'),
      context: {
        title: page.title,
        tagline: page.tagline,
      },
    });
  });
};

in src/templates/dynamic.js:

import React from 'react';
import { graphql } from 'gatsby';
import { Helmet } from 'react-helmet';

export const query = graphql`
  query($title: String!, $tagline: String) {
    getSocialCard(title: $title, tagline: $tagline) {
      url
    }
  }
`;

export default ({ data, pageContext: { title } }) => {
  return (
    <>
      <Helmet>
        <title>{title}</title>
        <meta property="og:image" content={data.getSocialCard.url} />
      </Helmet>
      <h1>{title}</h1>
    </>
  );
};

The page will have the meta tag added like so:

Rendered Gatsby page with the GatsbySocialImage component.

The generated card looks like this:

Card with the Learn With Jason logo that says, “The First Test Page. This page needs a snappy tagline.”