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

@vtex/gatsby-plugin-cms

v1.5.21-avon.0

Published

Gatsby plugin for building pages with VTEX CMS.

Downloads

754

Readme

gatsby-plugin-cms

This plugin links your gatsby site with VTEX's CMS datalayer.

Installation

To install @vtex/gatsby-plugin-cms in your project, just open your gatsby-config.js and add:

module.exports = {
  siteMetadata: {
    siteUrl: `https://www.example.com`,
  },
  plugins: [
    {
      resolve: '@vtex/gatsby-plugin-cms',
      options: {
        tenant,
        workspace,
        environment
      },
    }
  ],
}

where tenant is your store's account name and workspace is the VTEX IO workspace being used. Usually you will want to set this option to master.

An example config, using storecomponents account, would be:

module.exports = {
  siteMetadata: {
    siteUrl: `https://storecomponents.vtex.app`,
  },
  plugins: [
    {
      resolve: '@vtex/gatsby-plugin-cms',
      options: {
        tenant: 'storecomponents',
        workspace: 'master',
        environment: 'vtexcommercestable'
      },
    }
  ],
}

Note: The siteUrl property must be defined and not left empty.

Configuring CMS

All CMS configuration is done by shadowing this plugin's index.ts file. The shadowing can be done by creating the folowing structure in your gatsby project

src/
├── @vtex
│   ├── gatsby-plugin-cms
│   │   └── index.ts

The shadowed index.ts file must export one variable called contentTypes. This option is explained in detail below

Adding Components and defining ContentTypes

To tell the CMS how to configure and render your components you need to define a component schema. The component schema is written in Json Schema v6, a versatile description language for creating forms.

For instance, a component description is something like:

import { Schemas } from '@vtex/gatsby-plugin-cms'

const schemas: Schemas = {
  'my-component-name': {
    title: 'My Awesome Component',
    description: 'This is my first CMS ready component',
    type: 'object',
    properties: {
      prop1: {
        title: 'Default value for prop1',
        type: 'string',
      },
      prop2: {
        title: 'Default value for prop2',
        type: 'string',
      }
    }
  }
}

Now that we have a component's schema, we must associate this schema to a content type. Content types define a page structure and they are those who you will be able to edit in the CMS interface. To export a content type, just:

import { Schemas, ContentTypes } from '@vtex/gatsby-plugin-cms'

const schemas: Schemas = {
  'my-component-name': {
    ...
  }
}

export const contentTypes: ContentTypes = {
  'my-lading-page': {
    blocks: {
      'my-component-name': schemas['my-component-name'],
      ...
    },
    extraBlocks: {
      ...
    },
    messages: {
      ...
    },
  },
}

Note that blocks must be a subset of schemas

You can read more about each property in the CMS developer docs

Querying data.

After creating contents in the CMS, you will be able to query them into the Gatsby GraphQL layer. Each content type will have a corresponding type on the Gatsby GraphQL layer.

For instace, let's supose you are defining the content type for your home page. In your home page you have a banner, and you have some information about the SEO of this page, like title and description tags. The following contentType definition is a valid solution:

{
  homePage: {
    blocks: {
      banner: {
        type: 'object',
        properties: {
          imageUrl: {
            type: "string",
          }
        }
      }
    },
    extraBlocks: {
      seo: {
        tags: {
          type: 'object'
          properties: {
            title: {
              type: 'string'
            },
            description: {
              type: 'string'
            }
          }

        }
      }
    },
  }
}

This, in turn, would generate types in your Gatsby GraphQL layer. To query these data you can do the follwing query:

query HomePageQuery {
  cmsHomePage {
    sections: {
      name
      props
    }
    seo {
      tags {
        title
        description
      }
    }
  }
}

which would return the follwing json:

{
  data: {
    sections: {
      name: 'banner'
      props: {
        imageUrl: 'https://path/to/image/url'
      }
    },
    seo: {
      tags: {
        title: 'Page Title',
        description: 'Page Description'
      }
    }
  }
}

Native Types

CMS plugin has pre-built blocks that speed up your content types creation. Think of this like a component library that you can import and stitch together to create the content type you desire. These types include Carousel, Seo, and much more. To use it on your project, just:

import { Carousel } from '@vtex/gatsby-plugin-cms'

...

export default {
  ...
  blocks: {
    myBlock: {
      Carousel,
      ...
    }
  }
  ...
}

Check all available blocks, and their definition, at gatsby-plugin-cms/native-types

VTEX modules and Native Types

Some VTEX modules have first-class support in our CMS. To enable this support, you need to create your contentTypes with our native types for that specific module. Below you can find the doc and how these integrations work for each module.

Catalog

Sourcing Brands/Categories can be achieved by using @vtex/gatsby-source-vtex plugin. This plugin sources a StoreCollection node into the Gatsby's data layer containing basic info about a category and brand. Although being handy for creating pages using the File System Route API, StoreCollection does not have enough data to create a rich PLP, with banners and much more. For this, you need to extend StoreCollection with more data. To help you extend StoreCollection for your business users, we created a native type called PLP for the Product List Page.

Whenever the CMS finds a node with the PLP signature, it will create a customization on the corresponding StoreCollection node adding this PLP as a foreign key on the StoreCollection node. This way, you can easily fetch all sections of the PLP when rendering the StoreCollection page, thus allowing you to add any information you want to the PLP.

To use it, just add this to your cms config:

import { PLP } from '@vtex/gatsby-plugin-cms'

export default {
  ...
  contentTypes: {
    ...PLP()
  },
  ...
}