UNPKG

13.4 kBMarkdownView Raw
1# gatsby-source-sanity
2
3Source plugin for pulling data from [Sanity.io](https://www.sanity.io/) into [Gatsby](https://www.gatsbyjs.org/) websites. Develop with real-time preview of all content changes. Compatible with `gatsby-image`.
4
5Get up and running in minutes with a fully configured starter project:
6* [Blog with Gatsby](https://www.sanity.io/create?template=sanity-io/sanity-template-gatsby-blog)
7* [Portfolio with Gatsby](https://www.sanity.io/create?template=sanity-io/sanity-template-gatsby-portfolio).
8
9[![Watch a video about the company website built with Gatsby using Sanity.io as a headless CMS](https://cdn.sanity.io/images/3do82whm/production/4f652e6d114e7010aa633b81cbcb97c335980fc8-1920x1080.png?w=500)](https://www.youtube.com/watch?v=STtpXBvJmDA)
10
11## Table of contents
12
13- [Install](#install)
14- [Basic usage](#basic-usage)
15- [Options](#options)
16- [Preview of unpublished content](#preview-of-unpublished-content)
17- [Real-time content preview with watch mode](#real-time-content-preview-with-watch-mode)
18- [GraphQL API](#graphql-api)
19- [Using images](#using-images)
20 - [Fluid](#fluid)
21 - [Fixed](#fixed)
22 - [Available fragments](#available-fragments)
23 - [Usage outside of GraphQL](#usage-outside-of-graphql)
24- [Generating pages](#generating-pages)
25- ["Raw" fields](#raw-fields)
26- [Portable Text / Block Content](#portable-text--block-content)
27- [Using .env variables](#using-env-variables)
28- [Credits](#credits)
29
30
31[See the getting started video](https://www.youtube.com/watch?v=qU4lFYp3KiQ)
32
33## Install
34
35From the command line, use npm (node package manager) to install the plugin:
36
37```console
38npm install gatsby-source-sanity
39```
40
41
42In the `gatsby-config.js` file in the Gatsby project's root directory, add the plugin configuration inside of the `plugins` section:
43
44```js
45module.exports = {
46 // ...
47 plugins: [
48 {
49 resolve: `gatsby-source-sanity`,
50 options: {
51 projectId: `abc123`,
52 dataset: `blog`,
53 // a token with read permissions is required
54 // if you have a private dataset
55 token: process.env.SANITY_TOKEN,
56
57 // If the Sanity GraphQL API was deployed using `--tag <name>`,
58 // use `graphqlTag` to specify the tag name. Defaults to `default`.
59 graphqlTag: 'default',
60 },
61 },
62 // ...
63],
64 // ...
65}
66```
67
68## Basic usage
69
70At this point you should [set up a GraphQL API](https://www.sanity.io/docs/graphql) for your Sanity dataset, if you have not done so already. This will help the plugin in knowing which types and fields exists, so you can query for them even without them being present in any current documents.
71
72**You should redeploy the GraphQL API everytime you make changes to the schema that you want to use in Gatsby by running ```sanity graphql deploy``` from within your Sanity project directory**
73
74Explore http://localhost:8000/___graphql after running `gatsby develop` to understand the created data and create a new query and checking available collections and fields by typing `CTRL + SPACE`.
75
76## Options
77
78| Options | Type | Default | Description |
79| ------------- | ------- | --------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
80| projectId | string | | **[required]** Your Sanity project's ID |
81| dataset | string | | **[required]** The dataset to fetch from |
82| token | string | | Authentication token for fetching data from private datasets, or when using `overlayDrafts` [Learn more](https://www.sanity.io/docs/http-auth) |
83| graphqlTag | string | `default` | If the Sanity GraphQL API was deployed using `--tag <name>`, use this to specify the tag name. |
84| overlayDrafts | boolean | `false` | Set to `true` in order for drafts to replace their published version. By default, drafts will be skipped. |
85| watchMode | boolean | `false` | Set to `true` to keep a listener open and update with the latest changes in realtime. If you add a `token` you will get all content updates down to each keypress. |
86
87## Preview of unpublished content
88
89Sometimes you might be working on some new content that is not yet published, which you want to make sure looks alright within your Gatsby site. By setting the `overlayDrafts` setting to `true`, the draft versions will as the option says "overlay" the regular document. In terms of Gatsby nodes, it will _replace_ the published document with the draft.
90
91Keep 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.
92
93## Real-time content preview with watch mode
94
95While developing, it can often be beneficial to get updates without having to manually restart the build process. By setting `watchMode` to true, this plugin will set up a listener which watches for changes. When it detects a change, the document in question is updated in real-time and will be reflected immediately.
96
97If you add [a `token` with read rights](https://www.sanity.io/docs/http-auth#robot-tokens) and set `overlayDrafts` to true, each small change to the draft will immediately be applied.
98
99## GraphQL API
100
101By [deploying a GraphQL API](https://www.sanity.io/docs/graphql) for your dataset, we are able to introspect and figure out which schema types and fields are available and make informed choices based on this information.
102
103Previous versions did not _require_ this, but often lead to very confusing and unpredictable behavior, which is why we have now made it a requirement.
104
105## Using images
106
107Image fields will have the image URL available under the `field.asset.url` key, but you can also use [gatsby-image](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-image) for a smooth experience. It's a React component that enables responsive images and advanced image loading techniques. It works great with this source plugin, without requiring any additional build steps.
108
109There are two types of responsive images supported; _fixed_ and _fluid_. To decide between the two, ask yourself: "do I know the exact size this image will be?" If yes, you'll want to use _fixed_. If no and its width and/or height need to vary depending on the size of the screen, then you'll want to use _fluid_.
110
111### Fluid
112
113```js
114import React from 'react'
115import Img from 'gatsby-image'
116
117const Person = ({data}) => (
118 <article>
119 <h2>{data.sanityPerson.name}</h2>
120 <Img fluid={data.sanityPerson.profileImage.asset.fluid} />
121 </article>
122)
123
124export default Person
125
126export const query = graphql`
127 query PersonQuery {
128 sanityPerson {
129 name
130 profileImage {
131 asset {
132 fluid(maxWidth: 700) {
133 ...GatsbySanityImageFluid
134 }
135 }
136 }
137 }
138 }
139`
140```
141
142### Fixed
143
144```js
145import React from 'react'
146import Img from 'gatsby-image'
147
148const Person = ({data}) => (
149 <article>
150 <h2>{data.sanityPerson.name}</h2>
151 <Img fixed={data.sanityPerson.profileImage.asset.fixed} />
152 </article>
153)
154
155export default Person
156
157export const query = graphql`
158 query PersonQuery {
159 sanityPerson {
160 name
161 profileImage {
162 asset {
163 fixed(width: 400) {
164 ...GatsbySanityImageFixed
165 }
166 }
167 }
168 }
169 }
170`
171```
172
173### Available fragments
174
175These are the fragments available on image assets, which allows easy lookup of the fields required by gatsby-image in various modes:
176
177- `GatsbySanityImageFixed`
178- `GatsbySanityImageFixed_noBase64`
179- `GatsbySanityImageFluid`
180- `GatsbySanityImageFluid_noBase64`
181
182### Usage outside of GraphQL
183
184If you are using the raw fields, or simply have an image asset ID you would like to use gatsby-image for, you can import and call the utility functions `getFluidGatsbyImage` and `getFixedGatsbyImage`:
185
186```js
187import Img from 'gatsby-image'
188import {getFluidGatsbyImage, getFixedGatsbyImage} from 'gatsby-source-sanity'
189
190const sanityConfig = {projectId: 'abc123', dataset: 'blog'}
191const imageAssetId = 'image-488e172a7283400a57e57ffa5762ac3bd837b2ee-4240x2832-jpg'
192
193const fluidProps = getFluidGatsbyImage(imageAssetId, {maxWidth: 1024}, sanityConfig)
194
195<Img fluid={fluidProps} />
196```
197
198## Generating pages
199
200Sanity does not have any concept of a "page", since it's built to be totally agnostic to how you want to present your content and in which medium, but since you're using Gatsby, you'll probably want some pages!
201
202As with any Gatsby site, you'll want to create a `gatsby-node.js` in the root of your Gatsby site repository (if it doesn't already exist), and declare a `createPages` function. Within it, you'll use GraphQL to query for the data you need to build the pages.
203
204For instance, if you have a `project` document type in Sanity that you want to generate pages for, you could do something along the lines of this:
205
206```js
207exports.createPages = async ({graphql, actions}) => {
208 const {createPage} = actions
209
210 const result = await graphql(`
211 {
212 allSanityProject(filter: {slug: {current: {ne: null}}}) {
213 edges {
214 node {
215 title
216 description
217 tags
218 launchDate(format: "DD.MM.YYYY")
219 slug {
220 current
221 }
222 image {
223 asset {
224 url
225 }
226 }
227 }
228 }
229 }
230 }
231 `)
232
233 if (result.errors) {
234 throw result.errors
235 }
236
237 const projects = result.data.allSanityProject.edges || []
238 projects.forEach((edge, index) => {
239 const path = `/project/${edge.node.slug.current}`
240
241 createPage({
242 path,
243 component: require.resolve('./src/templates/project.js'),
244 context: {slug: edge.node.slug.current},
245 })
246 })
247}
248```
249
250The above query will fetch all projects that have a `slug.current` field set, and generate pages for them, available as `/project/<project-slug>`. It will use the template defined in `src/templates/project.js` as the basis for these pages.
251
252Most [Gatsby starters](https://www.gatsbyjs.org/starters/?v=2) have some example of building pages, which you should be able to modify to your needs.
253
254Remember to use the GraphiQL interface to help write the queries you need - it's usually running at http://localhost:8000/___graphql while running `gatsby develop`.
255
256## "Raw" fields
257
258Arrays and object types at the root of documents will get an additional "raw JSON" representation in a field called `_raw<FieldName>`. For instance, a field named `body` will be mapped to `_rawBody`. It's important to note that this is only done for top-level nodes (documents).
259
260Quite 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:
261
262```graphql
263{
264 allSanityProject {
265 edges {
266 node {
267 _rawTasks(resolveReferences: {maxDepth: 5})
268 }
269 }
270 }
271}
272```
273
274## Portable Text / Block Content
275
276Rich text in Sanity is usually represented as [Portable Text](https://www.portabletext.org/) (previously known as "Block Content").
277
278These data structures can be deep and a chore to query (specifying all the possible fields). As [noted above](#raw-fields), there is a "raw" alternative available for these fields which is usually what you'll want to use.
279
280You can install [block-content-to-react](https://www.npmjs.com/package/@sanity/block-content-to-react) from npm and use it in your Gatsby project to serialize Portable Text. It lets you use your own React components to override defaults and render custom content types. [Learn more about Portable Text in our documentation](https://www.sanity.io/docs/content-studio/what-you-need-to-know-about-block-text).
281
282## Using .env variables
283
284If you don't want to attach your Sanity project's ID to the repo, you can easily store it in .env files by doing the following:
285
286```js
287// In your .env file
288SANITY_PROJECT_ID = abc123
289SANITY_DATASET = production
290SANITY_TOKEN = my-super-secret-token
291
292// In your gatsby-config.js file
293require('dotenv').config({
294 path: `.env.${process.env.NODE_ENV}`
295})
296
297module.exports = {
298 // ...
299 plugins: [
300 {
301 resolve: 'gatsby-source-sanity',
302 options: {
303 projectId: process.env.SANITY_PROJECT_ID,
304 dataset: process.env.SANITY_DATASET,
305 token: process.env.SANITY_TOKEN
306 // ...
307 }
308 }
309 ]
310 // ...
311}
312```
313
314This example is based off [Gatsby Docs' implementation](https://www.gatsbyjs.org/docs/environment-variables/).
315
316## Credits
317
318Huge thanks to [Henrique Doro](https://github.com/hdoro) for doing the initial implementation of this plugin, and for donating it to the Sanity team. Mad props!
319
320Big thanks to the good people backing Gatsby for bringing such a joy to our developer days!