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

gridsome-source-sanity

v1.2.0

Published

Sanity.io source for Gridsome

Downloads

388

Readme

gridsome-source-sanity

Sanity.io source for Gridsome. Requires Gridsome 0.7.1 or above.

Table of contents

Basic usage

npm install gridsome-source-sanity
# or
yarn add gridsome-source-sanity

Deploy GraphQL schema

This source plugin only works if you publish a GraphQL API for your project and dataset. It will use the GraphQL API’s schema definitions to set the proper fields for your schema types.

~/yourSanityProjectFolder > sanity graphql deploy

Remember to redeploy the GraphQL API when you have changed the schema for Sanity.

Plugin configuration

module.exports = {
  plugins: [
    {
      use: 'gridsome-source-sanity',
      options: {
        projectId: '<projectId>',
        dataset: '<datasetName>',
        // Token is only required if dataset is private
        // or `overlayDrafts` is set to true
        token: '<tokenWithReadRights>',
        overlayDrafts: false,
        // Only enable real-time changes in development
        watchMode: process.env.NODE_ENV === "development",
        // API Version has to be set to today's date for the latest features.
        // See: https://sanity.io/help/js-client-api-version
        apiVersion: '2021-10-14',

        // If the Sanity GraphQL API was deployed using `--tag <name>`,
        // use `graphqlTag` to specify the tag name. Defaults to `default`.
        graphqlTag: 'default'
      }
    }
  ]
}

Options

| Options | Type | Default | Description | | ------------- | ------- | --------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | projectId | string | | [required] Your Sanity project's ID | | dataset | string | | [required] The dataset to fetch from | | token | string | | Authentication token for fetching data from private datasets, or when using overlayDrafts Learn more | | graphqlTag | string | default | If the Sanity GraphQL API was deployed using --tag <name>, use this to specify the tag name. | | overlayDrafts | boolean | false | Set to true in order for drafts to replace their published version. By default, drafts will be skipped. | | watchMode | boolean | false | Set to true to keep a listener open and update with the latest changes in realtime. If you enable overlayDrafts, changes will be reflected almost down to each keypress. This option shouldn't be enabled during build or else the listener will prevent it from being completed. | | typeName | string | Sanity | Prefix for schema types and queries. |

Preview of unpublished content

Sometimes you might be working on some new content that is not yet published, which you want to make sure looks alright within your Gridsome site. By setting the overlayDrafts setting to true, the draft versions will as the option says "overlay" the regular document. In terms of Gridsome nodes, it will replace the published document with the draft.

Keep in mind that drafts do not have to conform to any validation rules, so your frontend will usually want to double-check all nested properties before attempting to use them.

"Raw" fields

Certain fields (portable text fields being one of them) will get a "raw JSON" representation in a field called _raw<FieldName>. For instance, a field named body will be mapped to _rawBody. This is a workaround for a known GraphQL introspection shortcoming that will be addressed in a future version of Sanity.

Quite often, you'll want to replace reference fields (eg _ref: '<documentId>'), with the actual document that is referenced. This is done automatically for regular fields, but within raw fields, you have to explicitly enable this behavior, by using the field-level resolveReferences argument:

{
  allSanityProject {
    edges {
      node {
        _rawTasks(resolveReferences: {maxDepth: 5})
      }
    }
  }
}

Portable Text / Block Content

Rich text in Sanity is usually represented as Portable Text (previously known as "Block Content").

These data structures can be deep and a chore to query (specifying all the possible fields). As noted above, there is a "raw" alternative available for these fields which is usually what you'll want to use.

You can install sanity-blocks-vue-component from npm and use it in your Gridsome project to serialize Portable Text. It lets you use your own Vue components to override defaults and render custom content types. Learn more about Portable Text in our documentation.