50
loading...
This website collects cookies to deliver better user experience
gatsby-config.js
's siteMetadata
array."categories": [
{
"title": "Blog",
"href": "/blog",
"description": "My blog. I write about my journey as a software engineering lead and content creator.",
"pageHeading": "From the blog",
"pageSubtitle": "Explore some of my blog posts as I embark on a journey to build my site from scratch using GatsbyJS and TailwindUI."
},
...
]
exports.createPages = async ({ graphql, actions, reporter }) => {
await createCategoryPages({ graphql, actions, reporter }); // a new async function I created
};
async function createCategoryPages({ graphql, actions, reporter }) {
const { createPage } = actions;
// logic here, see below
}
const result = await graphql(
`
{
site {
siteMetadata {
categories {
title
href
description
pageHeading
pageSubtitle
}
author {
name
href
}
}
}
}
`,
);
reporter.warn
or reporter.panicOnBuild
.const siteMetadata = result.data.site?.siteMetadata;
const categories = siteMetadata?.categories;
const author = siteMetadata?.author;
const component = path.resolve(`./src/components/category.js`);
categories.forEach((page) => {
createPage({
path: page.href,
component,
context: {
...page,
author,
},
});
});
component
will need to load a list of articles for each category. Since the site has articles in multiple categories, we need to pass something to construct the correct query. In Gatsby, this is achieved by passing data via the page's context. GraphQL page queries can make use of any variables passed by context.query {
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
filter: {
frontmatter: {
category: {
title: { eq: "A category title" }
}
}
}
) {
nodes {
...
}
}
}
$title
variable.query CategoryQuery($title: String!) {
allMarkdownRemark(
sort: { fields: [frontmatter___date], order: DESC }
filter: {
frontmatter: {
category: {
title: { eq: $title }
}
}
}
) {
nodes {
...
}
}
}
export default function Page({ data, pageContext }) {
const {
title,
description,
pageHeading,
pageSubtitle,
author,
} = pageContext;
const posts = data.allMarkdownRemark.nodes;
return (
<>
...
</>
);
}