Get ready to create your own awesome blog using Next.js, Tailwind CSS, and Ghost CMS! This guide will take you through each step, making it easy to build a beautiful and functional blog. If you want to view the source code click here.
Step 1: Installation
use the following command to create a NextJs project
npx create-next-app@latest ghost-blog
Keep the default settings for the questions it asks.
now we need to install @tailwindcss/typography
cd ghost-blog
npm install @tailwindcss/typography
Step 2: Project Structure
the above picture depicts the structure of the project create the folders and file as shown.
Step 3: Initial Setup
Clear default code from app/page.js.
add @tailwindcss/typography plugin in tailwind css config file.
Add the Unsplash domain to the Next.js config file to display images from Unsplash. You can add any domain you like.
create .env file to store the api key and url of ghost cms.
don't include the 'https://' part in NEXT_PUBLIC_GHOST_API_URL variable.
Step 4: Building Components
Implement the Header component in Header.js to handle website navigation.
Create the Post component in Post.js to display individual blog posts and loop through multiple posts.
Step 5: Fetching Data from Ghost CMS
All the functions used to fetch data from Ghost CMS are written in the lib directory. There are two file posts.js and tags.js
The function getPosts() fetches blog posts from Ghost CMS. It makes a URL to get posts with details like images, titles, and summaries from the Ghost CMS API. After getting the posts, it checks if the response is okay. If it is, it changes the dates on the posts and sends back the list of posts. If there's a problem, it logs the error and sends back an empty list.
The function getPostsByTag(tag) fetches blog posts filtered by a specific tag from Ghost CMS. It constructs a URL to fetch posts with specific fields like feature image, title, excerpt, etc., based on the provided tag. After fetching the posts, it checks the response status; if successful, it formats the post dates and returns the array of filtered posts. If there's an error, it logs the error and returns an empty array.
The function getPostBySlug(slug) fetches a single blog post by its slug (a unique identifier) from Ghost CMS. It constructs a URL to fetch the post based on the provided slug. After fetching the post, it checks the response status; if successful, it formats the post date and returns the post object. If there's an error, it logs the error and returns an empty array. This function allows for efficient retrieval of a specific blog post by its slug for display in a Next.js application.
The below code belongs to tags.js
The function getTags() fetches tags from Ghost CMS. After fetching the tags, it checks the response status; if successful, it returns the array of tags. If there's an error, it logs the error and returns an empty array. This function enables efficient retrieval of tags for use in categorizing blog posts within a Next.js application.
Step 6: Creating Pages
In app/page.js
The above code imports a custom Header component, a Post component from components directory and the getPosts function from the posts library. In the Home component, it fetches posts using the getPosts function asynchronously. Once the posts are fetched, it is rendered with a header component. Each post is rendered using the Post component, passing the post data as a prop. The page layout is responsive, adjusting for different screen sizes.
In app/blog/page.js
In app/blog/[slug]/page.js
This code displays a single blog post. It imports the getPostBySlug function to fetch the post data. Inside the component, it asynchronously fetches the post data based on the slug provided in the URL parameters. Once the data is fetched, it renders a styled layout with the post's feature image, title, publication date, reading time, and the post content. The layout is responsive and adjusts for different screen sizes. The post content is displayed using HTML formatting, allowing for rich text display. Overall, this component creates a visually appealing and user-friendly layout for viewing individual blog post.
In app/tags/page.js
This code defines a Next.js component for displaying tags. It imports getTags function to fetch tag data. Inside the component, it asynchronously fetches the tags data. Once the data is fetched, it renders a styled layout with tags displayed as clickable links. Each tag link leads to a page showing posts associated with that tag. If there are no tags available, it displays a message saying "no tags yet". Overall, this component provides a user-friendly interface for navigating and exploring tags.
In app/tags/[slug]/page.js
This code defines a Next.js component for displaying blog posts associated with a specific tag. It imports getPostsByTag function to fetch posts based on the tag provided in the URL parameters. Inside the component, it asynchronously fetches posts associated with the tag. Each post is rendered using the Post component, passing the post data as a prop. The page layout is responsive, adjusting for different screen sizes. Overall, this component sets up a page to display blog posts filtered by a specific tag.
By following these steps, you'll have successfully built a fully functional blog using Next.js, Tailwind CSS, and Ghost CMS. Happy blogging!
Use the below command to run the application in localhost.