How to Make Taxonomy Pages With Gatsby and Sanity.io
Publikováno: 21.5.2020
Learn how to make category pages with Gatsby and structured content from Sanity.io.
The post How to Make Taxonomy Pages With Gatsby and Sanity.io appeared first on CSS-Tricks.
In this tutorial, we’ll cover how to make taxonomy pages with Gatsby with structured content from Sanity.io. You will learn how to use Gatsby’s Node creation APIs to add fields to your content types in Gatsby’s GraphQL API. Specifically, we’re going to create category pages for the Sanity’s blog starter.
That being said, there is nothing Sanity-specific about what we’re covering here. You’re able to do this regardless of which content source you may have. We’re just reaching for Sanity.io for the sake of demonstration.
Get up and running with the blog
If you want to follow this tutorial with your own Gatsby project, go ahead and skip to the section for creating a new page template in Gatsby. If not, head over to sanity.io/create and launch the Gatsby blog starter. It will put the code for Sanity Studio and the Gatsby front-end in your GitHub account and set up the deployment for both on Netlify. All the configuration, including example content, will be in place so that you can dive right into learning how to create taxonomy pages.
Once the project is iniated, make sure to clone the new repository on GitHub to local, and install the dependencies:
git clone git@github.com:username/your-repository-name.git
cd your-repository-name
npm i
If you want to run both Sanity Studio (the CMS) and the Gatsby front-end locally, you can do so by running the command npm run dev
in a terminal from the project root. You can also cd
into the web folder and just run Gatsby with the same command.
You should also install the Sanity CLI and log in to your account from the terminal: npm i -g @sanity/cli && sanity login
. This will give you tooling and useful commands to interact with Sanity projects. You can add the --help
flag to get more information on its functionality and commands.
We will be doing some customization to the gatsby-node.js
file. To see the result of the changes, restart Gatsby’s development server. This is done in most systems by hitting CTRL
+ C
in the terminal and running npm run dev
again.
Getting familiar with the content model
Look into the /studio/schemas/documents
folder. There are schema files for our main content types: author, category, site settings, and posts. Each of the files exports a JavaScript object that defines the fields and properties of these content types. Inside of post.js
is the field definition for categories:
{
name: 'categories',
type: 'array',
title: 'Categories',
of: [
{
type: 'reference',
to: {
type: 'category'
}
}
]
},
This will create an array field with reference objects to category documents. Inside of the blog’s studio it will look like this:
Adding slugs to the category type
Head over to /studio/schemas/documents/category.js
. There is a simple content model for a category that consists of a title and a description. Now that we’re creating dedicated pages for categories, it would be handy to have a slug field as well. We can define that in the schema like this:
// studio/schemas/documents/category.js
export default {
name: 'category',
type: 'document',
title: 'Category',
fields: [
{
name: 'title',
type: 'string',
title: 'Title'
},
{
name: 'slug',
type: 'slug',
title: 'Slug',
options: {
// add a button to generate slug from the title field
source: 'title'
}
},
{
name: 'description',
type: 'text',
title: 'Description'
}
]
}
Now that we have changed the content model, we need to update the GraphQL schema definition as well. Do this by executing npm run graphql-deploy
(alternatively: sanity graphql deploy
) in the studio folder. You will get warnings about breaking changes, but since we are only adding a field, you can proceed without worry. If you want the field to accessible in your studio on Netlify, check the changes into git (with git add . && git commit -m"add slug field"
) and push it to your GitHub repository (git push origin master
).
Now we should go through the categories and generate slugs for them. Remember to hit the publish button to make the changes accessible for Gatsby! And if you were running Gatsby’s development server, you’ll need to restart that too.
Quick sidenote on how the Sanity source plugin works
When starting Gatsby in development or building a website, the source plugin will first fetch the GraphQL Schema Definitions from Sanity deployed GraphQL API. The source plugin uses this to tell Gatsby which fields should be available to prevent it from breaking if the content for certain fields happens to disappear. Then it will hit the project’s export endpoint, which streams all the accessible documents to Gatsby’s in-memory datastore.
In order words, the whole site is built with two requests. Running the development server, will also set up a listener that pushes whatever changes come from Sanity to Gatsby in real-time, without doing additional API queries. If we give the source plugin a token with permission to read drafts, we’ll see the changes instantly. This can also be experienced with Gatsby Preview.
Adding a category page template in Gatsby
Now that we have the GraphQL schema definition and some content ready, we can dive into creating category page templates in Gatsby. We need to do two things:
- Tell Gatsby to create pages for the category nodes (that is Gatsby’s term for “documents”).
- Give Gatsby a template file to generate the HTML with the page data.
Begin by opening the /web/gatsby-node.js
file. Code will already be here that can be used to create the blog post pages. We’ll largely leverage this exact code, but for categories. Let’s take it step-by-step:
Between the createBlogPostPages
function and the line that starts with exports.createPages
, we can add the following code. I’ve put in comments here to explain what’s going on:
// web/gatsby-node.js
// ...
async function createCategoryPages (graphql, actions) {
// Get Gatsby‘s method for creating new pages
const {createPage} = actions
// Query Gatsby‘s GraphAPI for all the categories that come from Sanity
// You can query this API on http://localhost:8000/___graphql
const result = await graphql(`{
allSanityCategory {
nodes {
slug {
current
}
id
}
}
}
`)
// If there are any errors in the query, cancel the build and tell us
if (result.errors) throw result.errors
// Let‘s gracefully handle if allSanityCatgogy is null
const categoryNodes = (result.data.allSanityCategory || {}).nodes || []
categoryNodes
// Loop through the category nodes, but don't return anything
.forEach((node) => {
// Desctructure the id and slug fields for each category
const {id, slug = {}} = node
// If there isn't a slug, we want to do nothing
if (!slug) return
// Make the URL with the current slug
const path = `/categories/${slug.current}`
// Create the page using the URL path and the template file, and pass down the id
// that we can use to query for the right category in the template file
createPage({
path,
component: require.resolve('./src/templates/category.js'),
context: {id}
})
})
}
Last, this function is needed at the bottom of the file:
// /web/gatsby-node.js
// ...
exports.createPages = async ({graphql, actions}) => {
await createBlogPostPages(graphql, actions)
await createCategoryPages(graphql, actions) // <= add the function here
}
Now that we have the machinery to create the category page node in place, we need to add a template for how it actually should look in the browser. We’ll base it on the existing blog post template to get some consistent styling, but keep it fairly simple in the process.
// /web/src/templates/category.js
import React from 'react'
import {graphql} from 'gatsby'
import Container from '../components/container'
import GraphQLErrorList from '../components/graphql-error-list'
import SEO from '../components/seo'
import Layout from '../containers/layout'
export const query = graphql`
query CategoryTemplateQuery($id: String!) {
category: sanityCategory(id: {eq: $id}) {
title
description
}
}
`
const CategoryPostTemplate = props => {
const {data = {}, errors} = props
const {title, description} = data.category || {}
return (
<Layout>
<Container>
{errors && <GraphQLErrorList errors={errors} />}
{!data.category && <p>No category data</p>}
<SEO title={title} description={description} />
<article>
<h1>Category: {title}</h1>
<p>{description}</p>
</article>
</Container>
</Layout>
)
}
export default CategoryPostTemplate
We are using the ID that was passed into the context in gatsby-node.js
to query the category content. Then we use it to query the title
and description
fields that are on the category type. Make sure to restart with npm run dev
after saving these changes, and head over to localhost:8000/categories/structured-content
in the browser. The page should look something like this:
Cool stuff! But it would be even cooler if we actually could see what posts that belong to this category, because, well, that’s kinda the point of having categories in the first place, right? Ideally, we should be able to query for a “pages” field on the category object.
Before we learn how to that, we need to take a step back to understand how Sanity’s references work.
Querying Sanity’s references
Even though we’re only defining the references in one type, Sanity’s datastore will index them “bi-directionally.” That means creating a reference to the “Structured content” category document from a post lets Sanity know that the category has these incoming references and will keep you from deleting it as long as the reference exists (references can be set as “weak” to override this behavior). If we use GROQ, we can query categories and join posts that have them like this (see the query and result in action on groq.dev):
*[_type == "category"]{
_id,
_type,
title,
"posts": *[_type == "post" && references(^._id)]{
title,
slug
}
}
// alternative: *[_type == "post" && ^._id in categories[]._ref]{
This ouputs a data structure that lets us make a simple category post template:
[
{
"_id": "39d2ca7f-4862-4ab2-b902-0bf10f1d4c34",
"_type": "category",
"title": "Structured content",
"posts": [
{
"title": "Exploration powered by structured content",
"slug": {
"_type": "slug",
"current": "exploration-powered-by-structured-content"
}
},
{
"title": "My brand new blog powered by Sanity.io",
"slug": {
"_type": "slug",
"current": "my-brand-new-blog-powered-by-sanity-io"
}
}
]
},
// ... more entries
]
That’s fine for GROQ, what about GraphQL?
Here‘s the kicker: As of yet, this kind of query isn’t possible with Gatsby’s GraphQL API out of the box. But fear not! Gatsby has a powerful API for changing its GraphQL schema that lets us add fields.
Using createResolvers
to edit Gatsby’s GraphQL API
createResolvers
Gatsby holds all the content in memory when it builds your site and exposes some APIs that let us tap into how it processes this information. Among these are the Node APIs. It’s probably good to clarify that when we are talking about “node” in Gatsby — not to be confused with Node.js. The creators of Gatsby have borrowed “edges and nodes” from Graph theory where “edges” are the connections between the “nodes” which are the “points” where the actual content is located. Since an edge is a connection between nodes, it can have a “next” and “previous” property.
The Node APIs are used by plugins first and foremost, but they can be used to customize how our GraphQL API should work as well. One of these APIs is called createResolvers
. It’s fairly new and it lets us tap into how a type’s nodes are created so we can make queries that add data to them.
Let’s use it to add the following logic:
- Check for ones with the
SanityCategory
type when creating the nodes. - If a node matches this type, create a new field called
posts
and set it to theSanityPost
type. - Then run a query that filters all posts that has lists a category that matches the current category’s ID.
- If there are matching IDs, add the content of the post nodes to this field.
Add the following code to the /web/gatsby-node.js
file, either below or above the code that’s already in there:
// /web/gatsby-node.js
// Notice the capitalized type names
exports.createResolvers = ({createResolvers}) => {
const resolvers = {
SanityCategory: {
posts: {
type: ['SanityPost'],
resolve (source, args, context, info) {
return context.nodeModel.runQuery({
type: 'SanityPost',
query: {
filter: {
categories: {
elemMatch: {
_id: {
eq: source._id
}
}
}
}
}
})
}
}
}
}
createResolvers(resolvers)
}
Now, let’s restart Gatsby’s development server. We should be able to find a new field for posts inside of the sanityCategory
and allSanityCategory
types.
Adding the list of posts to the category template
Now that we have the data we need, we can return to our category page template (/web/src/templates/category.js
) and add a list with links to the posts belonging to the category.
// /web/src/templates/category.js
import React from 'react'
import {graphql, Link} from 'gatsby'
import Container from '../components/container'
import GraphQLErrorList from '../components/graphql-error-list'
import SEO from '../components/seo'
import Layout from '../containers/layout'
// Import a function to build the blog URL
import {getBlogUrl} from '../lib/helpers'
// Add “posts” to the GraphQL query
export const query = graphql`
query CategoryTemplateQuery($id: String!) {
category: sanityCategory(id: {eq: $id}) {
title
description
posts {
_id
title
publishedAt
slug {
current
}
}
}
}
`
const CategoryPostTemplate = props => {
const {data = {}, errors} = props
// Destructure the new posts property from props
const {title, description, posts} = data.category || {}
return (
<Layout>
<Container>
{errors && <GraphQLErrorList errors={errors} />}
{!data.category && <p>No category data</p>}
<SEO title={title} description={description} />
<article>
<h1>Category: {title}</h1>
<p>{description}</p>
{/*
If there are any posts, add the heading,
with the list of links to the posts
*/}
{posts && (
<React.Fragment>
<h2>Posts</h2>
<ul>
{ posts.map(post => (
<li key={post._id}>
<Link to={getBlogUrl(post.publishedAt, post.slug)}>{post.title}</Link>
</li>))
}
</ul>
</React.Fragment>)
}
</article>
</Container>
</Layout>
)
}
export default CategoryPostTemplate
This code will produce this simple category page with a list of linked posts – just liked we wanted!
Go make taxonomy pages!
We just completed the process of creating new page types with custom page templates in Gatsby. We covered one of Gatsby’s Node APIs called createResolver
and used it to add a new posts
field to the category nodes.
This should give you what you need to make other types of taxonomy pages! Do you have multiple authors on your blog? Well, you can use the same logic to create author pages. The interesting thing with the GraphQL filter is that you can use it to go beyond the explicit relationship made with references. It can also be used to match other fields using regular expressions or string comparisons. It’s fairly flexible!
The post How to Make Taxonomy Pages With Gatsby and Sanity.io appeared first on CSS-Tricks.