Although Ghost by default, provides very impressing themes; but I decided to use a simplified custom UI theme based on Gatsby.js, which also provides blazing fast static pages.
In this article we'll see how to add post preview navigation to Gatsby.js. Lets get on.
What is a post preview navigation?
Often time when you reach end of an article or a post, you see a navigation to previous or next article, which makes up for a good user experience and really keeps your visitor hooked on to what your website has to offer.
Basically, it is just option to navigate to previous or next post from the same post, without having to go back to index or catalogue page.
Lets begin with cloning this template from the GitHub to your local machine:
git clone https://github.com/TryGhost/gatsby-starter-ghost.git
cd gatsby-starter-ghost
Now download all the project dependencies using NPM (which will take a minute to download):
npm install
Once all the dependencies are installed, Gatsby will be installed on your machine:
Lets see Gatsby in action all you have to do is run following command:
gatsby develop
gatsby develop creates a local server localhost:8000 to run your website
You will see following web page on your browser:
Now if you go to an article, you will see there is a primitive way to get to next page using a hyperlink, and no way at all to go back to previous post; we are gonna change that in a bit!!
The code!!
We will begin with changing how posts are created in gatsby-node.js file.
modify the existing Create post pages section with following code:
here we provided get prev and next node's slug from the current node using below:
const prev = index === 0 ? false : posts[index - 1].node const next = index === posts.length - 1 ? false : posts[index + 1].node
Once this is done, we will have to see how to add this in our UI.
For that create a component PostPreview.js in component/common directory
Now One last thing, add PostPreview.js into src/templates/post.js
Now save all the changes, Gatsby will automatically hot reload and update these changes.
Enhancing the Navigation
Now that we have working navigation links for next and previous post, we can now focus on beautifying the navigation links.
We will be adding following things to existing links. i.e:
Post Titles
Post Images
Total reading time
So far we only had the link to next/prev posts, we don't have above fields in our data. So first we need to have these data to enhance our UI.
Let' head back to gatsby-node.js, where we will change our graphql query to get these extra items:
Before changes:
After changes:
Now download all the project dependencies using NPM (which will take a minute to download):
Now we will change the post createPages to get these changes
modify the existing Create post pages section with following code:
We have removed the logic conditions of how we were fetching our next/prev link urls earlier with the ones we are now getting from graphql queries
Upgrading Navigation UI
Lets modify our PostPreview UI with better Bootstrap classes.
Lets see Gatsby in action all you have to do is run following command:
After the hot reload you will see the following cards has been added for next/prev navigation along with more details like title, image and reading time.