New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

gatsby-plugin-cloudinary-social-cards

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gatsby-plugin-cloudinary-social-cards

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

1.0.2
latest
Source
npm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

gatsby-plugin-cloudinary-social-cards

Add social sharing cards to your Gatsby sites using Cloudinary!

This plugin will:

  • Look for a template image in your repo
  • Upload that template to your Cloudinary account
  • 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

OptionRequiredDefaultDescription
cloudNametrueYour Cloudinary cloud name (usually your account name).
apiKeytrueYour Cloudinary API key (get one here).
apiSecrettrueYour Cloudinary API secret (get one here).
imageTemplatetruePath to the social card image template. This should be a local file in your repo.
uploadFoldernullOptional 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:

  • Import the GatsbySocialImage component
  • 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.”

Keywords

gatsby

FAQs

Package last updated on 26 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts