I am trying to create a gatsby site where I have projects posted, each with tags. I followed a basic tutorial on creating a gatsby blog, and am now working through gatsby's tags as documented on their website. Along the way while changing variable names from path to slug to avoid reserved words, I seem to have messed up because I can no longer create blogpost pages, nor tag pages. Each post is enclosed in a folder named by the post title, holding an index.md file defining its content, date, tag etc.
The following error is output when I try to run gatsby develop,
ERROR #11323
Your site's "gatsby-node.js" must set the page path when creating a page.
The page object passed to createPage:
{
"slug": "/an-example-post",
"component": "C:\\Users\\Monolith\\Documents\\programming\\webdev\\Gatsby-Portfolio\\src\\templates\\post.js",
"context": {
"slug": "/an-example-post"
}
}
My gatsby-node.js
const path = require('path');
exports.createPages = ({actions, graphql}) => {
const {createPage} = actions;
const postTemplate = path.resolve('src/templates/post.js');
const tagTemplate = path.resolve("src/templates/tags.js")
//const postTemplate = require.resolve('src/templates/post.js');
//const tagTemplate = require.resolve("src/templates/tags.js")
return graphql(`{
allMarkdownRemark{
edges{
node {
html
id
frontmatter{
slug
title
}
}
}
}
tagsGroup: allMarkdownRemark(limit: 2000) {
group(field: frontmatter___tags) {
fieldValue
}
}
}`)
.then(res => {
if(res.errors){
return Promise.reject(res.errors);
}
res.data.allMarkdownRemark.edges.forEach( ({node}) => {
createPage({
slug: node.frontmatter.slug,
component: postTemplate,
context: {
slug:node.frontmatter.slug
},
})
})
// Extract tag data from query
const tags = res.data.tagsGroup.group
// Make tag pages
tags.forEach(tag => {
createPage({
// path: `/tags/${_.kebabCase(tag.fieldValue)}/`,
slug: `/tags/${(tag.fieldValue)}/`,
component: tagTemplate,
context: {
tag: tag.fieldValue,
},
})
})
})
}
As previously mentioned I was concerned the path variable being a reserved word could be an issue, but I got further using it than omitting it so for now it has stayed.
Example post.js
import React from 'react';
import { graphql } from 'gatsby';
import Layout from "../components/layout"
export default function Template({data}) {
const {markdownRemark: post} = data;
return(
<Layout>
<div>
<h1 className="postTitle">{post.frontmatter.title}</h1>
<div className="tagGroup">
{post.frontmatter.tags.map((tag, index) => (
<div key={index}>
<h2 className = "tagStyle">{tag}</h2>
</div>
))}
</div>
<p>{post.frontmatter.description}</p>
<p>{post.frontmatter.date}</p>
{/* <h2 className="tagStyle">{post.frontmatter.tags + " "}</h2> */}
<div dangerouslySetInnerHTML={{__html: post.html}} />
</div>
</Layout>
)
}
export const postQuery = graphql`
#query BlogPostByPath($slug: String!) {
query($slug: String!) {
markdownRemark(frontmatter: { slug: {eq: $slug} }) {
html
frontmatter {
slug
title
description
date
tags
}
}
}
`
The tags.js is very similar to gatsby's default with slight alterations for content. Here is the Graphql query I am using.
Tags.propTypes = {
pageContext: PropTypes.shape({
tag: PropTypes.string.isRequired,
}),
data: PropTypes.shape({
allMarkdownRemark: PropTypes.shape({
totalCount: PropTypes.number.isRequired,
edges: PropTypes.arrayOf(
PropTypes.shape({
node: PropTypes.shape({
frontmatter: PropTypes.shape({
title: PropTypes.string.isRequired,
// slug: PropTypes.string.isRequired,
}),
fields: PropTypes.shape({
slug: PropTypes.string.isRequired,
}),
}),
}).isRequired
),
}),
}),
}
export default Tags
export const pageQuery = graphql`
query($tag: String) {
allMarkdownRemark(
limit: 2000
sort: { fields: [frontmatter___date], order: DESC }
filter: { frontmatter: { tags: { in: [$tag] } } }
) {
totalCount
edges {
node {
fields {
slug
}
frontmatter {
title
}
}
}
}
}
`
If anyone has any information to help point me in the right direction I would greatly appreciate it. I have been running through gatsby's documentation for hours and have yet to make substantial progress.
You are passing a
tag
in your context but expecting aslug
:And:
The context is a way to pass data to your component template to use it to filter your data. Ideally should be a unique parameter (like a
slug
,id
, etc). In that way, in yourgatsby-node.js
you should fetch all your posts, pass a unique field by context, and use that variable in your template to get all your needed data of each post.In addition, you must provide a
path
, not aslug
to your function.You should change your
createPage
function to:Keep in mind that
fieldValue
should be a slug (or kind of) despite having different naming.I would recommmend reading Gatsby's tutorial: https://www.gatsbyjs.com/tutorial/part-seven/