Skip to content

Commit 6abef2c

Browse files
authoredOct 15, 2020
fix(blog-starter): don't pass data via context - pass identifiers (#27359)
* fix(blog-starter): don't pass data via context - pass identifiers * don't use fragment * use nodes and not edges * drop console.log
1 parent eb9bac8 commit 6abef2c

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed
 

‎starters/blog/gatsby-node.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ exports.createPages = async ({ graphql, actions, reporter }) => {
1212
`
1313
{
1414
allMarkdownRemark(
15-
sort: { fields: [frontmatter___date], order: DESC }
15+
sort: { fields: [frontmatter___date], order: ASC }
1616
limit: 1000
1717
) {
1818
nodes {
19+
id
1920
fields {
2021
slug
2122
}
22-
frontmatter {
23-
title
24-
}
2523
}
2624
}
2725
}
@@ -44,16 +42,16 @@ exports.createPages = async ({ graphql, actions, reporter }) => {
4442

4543
if (posts.length > 0) {
4644
posts.forEach((post, index) => {
47-
const previous = index === posts.length - 1 ? null : posts[index + 1]
48-
const next = index === 0 ? null : posts[index - 1]
45+
const previousPostId = index === 0 ? null : posts[index - 1].id
46+
const nextPostId = index === posts.length - 1 ? null : posts[index + 1].id
4947

5048
createPage({
5149
path: post.fields.slug,
5250
component: blogPost,
5351
context: {
54-
slug: post.fields.slug,
55-
previous,
56-
next,
52+
id: post.id,
53+
previousPostId,
54+
nextPostId,
5755
},
5856
})
5957
})

‎starters/blog/src/templates/blog-post.js

+24-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import Bio from "../components/bio"
55
import Layout from "../components/layout"
66
import SEO from "../components/seo"
77

8-
const BlogPostTemplate = ({ data, pageContext, location }) => {
8+
const BlogPostTemplate = ({ data, location }) => {
99
const post = data.markdownRemark
1010
const siteTitle = data.site.siteMetadata?.title || `Title`
11-
const { previous, next } = pageContext
11+
const { previous, next } = data
1212

1313
return (
1414
<Layout location={location} title={siteTitle}>
@@ -67,13 +67,17 @@ const BlogPostTemplate = ({ data, pageContext, location }) => {
6767
export default BlogPostTemplate
6868

6969
export const pageQuery = graphql`
70-
query BlogPostBySlug($slug: String!) {
70+
query BlogPostBySlug(
71+
$id: String!
72+
$previousPostId: String
73+
$nextPostId: String
74+
) {
7175
site {
7276
siteMetadata {
7377
title
7478
}
7579
}
76-
markdownRemark(fields: { slug: { eq: $slug } }) {
80+
markdownRemark(id: { eq: $id }) {
7781
id
7882
excerpt(pruneLength: 160)
7983
html
@@ -83,5 +87,21 @@ export const pageQuery = graphql`
8387
description
8488
}
8589
}
90+
previous: markdownRemark(id: { eq: $previousPostId }) {
91+
fields {
92+
slug
93+
}
94+
frontmatter {
95+
title
96+
}
97+
}
98+
next: markdownRemark(id: { eq: $nextPostId }) {
99+
fields {
100+
slug
101+
}
102+
frontmatter {
103+
title
104+
}
105+
}
86106
}
87107
`

0 commit comments

Comments
 (0)
Please sign in to comment.