Skip to content

Commit 18bf781

Browse files
committed
manually createPage for tag pages
1 parent 51a4894 commit 18bf781

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

gatsby-node.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ exports.onCreatePage = async ({ page, actions }) => {
8585
if (page.path.startsWith('/blog')) {
8686
return;
8787
}
88+
if (page.path.startsWith('/tags')) {
89+
return;
90+
}
8891

8992
const { createPage, deletePage } = actions
9093
deletePage(page)
@@ -228,8 +231,8 @@ exports.createPages = async ({ graphql, actions }) => {
228231
}
229232
}
230233
}
231-
tagsGroup: allMarkdownRemark {
232-
group(field: frontmatter___tags) {
234+
allBlogPost {
235+
group(field: tags) {
233236
fieldValue
234237
}
235238
}
@@ -244,6 +247,17 @@ exports.createPages = async ({ graphql, actions }) => {
244247
throw result.errors
245248
}
246249

250+
const tags = result.data.allBlogPost.group.map(group => group.fieldValue)
251+
tags.forEach(tag => {
252+
createPage({
253+
path: `/tags/${tag.toLowerCase()}/`,
254+
component: path.resolve("./src/templates/{BlogPost.tags}.tsx"),
255+
context: {
256+
tag,
257+
},
258+
})
259+
})
260+
247261
const markdownPages = result.data.allMarkdownRemark.edges
248262

249263
// foundation: [

src/pages/tags/{BlogPost.tags}.tsx renamed to src/templates/{BlogPost.tags}.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as React from "react"
22
import type { PageProps } from "gatsby"
33
import { graphql } from "gatsby"
4-
import Layout from "../../components/Layout"
5-
import BlogPostPreview from "../../components/BlogPostPreview"
6-
import BlogSidebar from "../../components/BlogSidebar"
4+
import Layout from "../components/Layout"
5+
import BlogPostPreview from "../components/BlogPostPreview"
6+
import BlogSidebar from "../components/BlogSidebar"
77

88
export const query = graphql`
9-
query TagPage($tags: [String!]!) {
9+
query TagPage($tag: String!) {
1010
allBlogPost(
11-
filter: { tags: { in: $tags } }
11+
filter: { tags: { in: [$tag] } }
1212
) {
1313
nodes {
1414
id
@@ -20,8 +20,8 @@ export const query = graphql`
2020

2121
type Props = PageProps<GatsbyTypes.TagPageQuery, GatsbyTypes.SitePageContext>
2222

23-
const TagPage: React.FC<Props> = ({ data, params }) => {
24-
const currentTag = params.tags!
23+
const TagPage: React.FC<Props> = ({ data, pageContext }) => {
24+
const currentTag = pageContext.tag!
2525
return (
2626
<Layout title={`Blog: ${currentTag} | GraphQL`} pageContext={{}}>
2727
<section>

0 commit comments

Comments
 (0)