First I'm gonna present the code I have and then explain the problem:
I'm having this on my gatsby-node.js:
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === `ContetnfulPost`) {
const slug = createFilePath({ node, getNode, basePath: `pages` })
createNodeField({
node,
name: `slug`,
value: slug,
})
}
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(`
query {
allContentfulPost {
edges {
node {
postTitle
slug
}
}
}
}
`)
const posts = result.data.allContentfulPost.edges
posts.forEach(({ node }, index) => {
createPage({
path: `${node.slug}`,
component: path.resolve(`./src/templates/blogPost.js`),
context: {
slug: node.slug,
prev: index === 0 ? null : posts[index - 1].node,
next: index === posts.length - 1 ? null : posts[index + 1].node,
},
})
})
}
Then I'm querying blog posts in 2 places: one as a blog page like this:
blog.js page:
function Blog() {
return (
<Layout>
<SEO title="Blog Page" />
<StaticQuery
query={graphql`
query blogQuery {
allContentfulPost {
edges {
node {
slug
postTitle
postImage {
file {
url
fileName
}
}
postContent {
postContent
}
postDate
}
}
}
}
`}
render={data => (
<ul>
{data.allContentfulPost.edges.map(({ node }) => {
return (
<BlogPostsContainer>
<Link to={`${node.slug}`}>
<BlogPostImage src={node.postImage.file.url} />
<PostTitle>{node.postTitle}</PostTitle>
</Link>
</BlogPostsContainer>
)
})}
</ul>
)}
/>
</Layout>
)
}
And the other, a react component which I use to display some of my posts in the homepage:
BlogSection.js
function BlogSection() {
return (
<BlogSectionWrapper>
<StaticQuery
query={graphql`
query homeBlogQuery {
allContentfulPost(limit: 2) {
edges {
node {
slug
postTitle
postImage {
file {
url
fileName
}
}
postContent {
postContent
}
postDate
}
}
}
}
`}
render={data => (
<ul>
{data.allContentfulPost.edges.map(({ node }) => {
return (
<HomePostsContainer>
<Link to={`${node.slug}`}>
<HomePostImage src={node.postImage.file.url} />
<PostTitle>{node.postTitle}</PostTitle>
</Link>
</HomePostsContainer>
)
})}
</ul>
)}
/>
<ButtonWrapper></ButtonWrapper>
<FlatButton
item={{
title: "See all posts",
icon: "/images/icons/book.svg",
link: "/blog",
}}
/>
</BlogSectionWrapper>
)
}
Now the problem is that when I click in a blog post from the home page (the ones rendered from the BlogSection.js component), the URL is normally http://localhost:8000/test-post.
But when I click on a post from the blog page (rendered from the blog.js file), the URL becomes http://localhost:8000/blog/test-post.
I also added Prev and Next buttons on each post and when I click on them, it just appends the slug in the URL, for instance when I'm on http://localhost:8000/test-post and click Next, it becomes http://localhost:8000/test-post/test-post2.
How can I keep the URL to show the post slug only or maybe with a /blog prefix for all the posts independent from where I click on it?
I tried this on the gatsby-node.js file but its not working:
posts.forEach(({ node }, index) => {
createPage({
path: `/blog/${node.slug}`, //<===== THIS LINE
component: path.resolve(`./src/templates/blogPost.js`),
context: {
slug: node.slug,
prev: index === 0 ? null : posts[index - 1].node,
next: index === posts.length - 1 ? null : posts[index + 1].node,
},
})
})