Logo

Building a Blog Page with Prismic and Next.js: A Comprehensive Guide

Posted By

Hugo Carvalho

Published Date

2024-05-21

Read Time

5 min

Prerequisites:

  • Node.js: Make sure you have Node.js installed on your machine.
  • Prismic Account: Sign up for a free Prismic account at https://prismic.io/.
  • Basic Next.js Knowledge: Familiarity with Next.js concepts like routing and components is helpful.

Step 1: Setting Up Your Prismic Repository

  1. Create a Repository: Head over to https://prismic.io/ and create a new repository. Choose a name that reflects your blog's purpose.
  2. Define a Custom Type: Navigate to the Custom Types section and create a new type called blog_post. This type will define the structure of your blog posts. Add the following fields:title: (Rich Text) - The title of your blog post.summary_content: (Rich Text) - A brief summary of the post.featured_image: (Image) - A prominent image for the post.author_name: (Text) - The author's name.publication_date: (Date) - The date the post was published.
  3. Add Sample Content: Create a few sample blog posts in your Prismic repository to test your application later.

Step 2: Setting Up Your Next.js Project

  1. Initialize a Next.js Project: Open your terminal and run the following commands:
  2. npx create-next-app@latest my-blog cd my-blog
  3. Install Required Packages: Install the necessary packages for interacting with Prismic:
  4. npm install @prismicio/client @prismicio/react @prismicio/helpers next-slicezone

Step 3: Configure the Prismic Client

  1. Create Prismic Client: In the src directory, create a file named prismicio.js and add the following code:
  2. import * as prismic from '@prismicio/client'; export const repositoryName = 'your-repo-name'; // Replace with your Prismic repository name export const client = prismic.createClient(repositoryName, { accessToken: 'your-access-token', // Leave empty if your repository is public });

Step 4: Fetch Blog Posts

  1. Create Fetch Function: In the src directory, create a file named fetchBlogPosts.js and add the following code:
  2. import { client } from './prismicio'; export async function fetchBlogPosts() { const response = await client.getAllByType('blog_post'); return response.map((post) => ({ uid: post.uid, img: post.data.featured_image.url, title: post.data.title[0]?.text || 'Untitled', des: post.data.summary_content[0]?.text || 'No description available', author_name: post.data.author_name || 'Unknown author', publication_date: post.data.publication_date || 'Unknown date', })); }

Step 5: Configure Image Domains

  1. Update next.config.js: Configure Next.js to allow images from external domains by adding the following code to next.config.js:
  2. module.exports = { images: { remotePatterns: [ { protocol: 'https', hostname: 'images.prismic.io', pathname: '**', }, { protocol: 'https', hostname: 'images.unsplash.com', // Add any other image domains you use pathname: '**', }, ], }, };

Step 6: Create the Blog Component

  1. Create BlogArea Component: In the src/components/blog directory, create a file named BlogArea.js and add the following code:
  2. import React from 'react'; import Image from 'next/image'; import Link from 'next/link'; import { fetchBlogPosts } from '@/lib/fetchBlogPosts'; const BlogArea = ({ posts }) => { return ( <section> <div className="container"> <div className="row"> {posts.map((post, index) => ( <div key={index} className="col-md-4"> <Link href={`/blog/${post.uid}`}> <a className="blog-post"> <Image src={post.img} alt={post.title} width={500} height={300} layout="responsive" /> <h3>{post.title}</h3> <p>{post.des}</p> </a> </Link> </div> ))} </div> </div> </section> ); }; export default BlogArea;

Step 7: Create the Blog Page

  1. Create Blog Page: In the src/pages directory, create a file named blog.js and add the following code:
  2. import React from 'react'; import BlogArea from '@/components/blog/BlogArea'; import { fetchBlogPosts } from '@/lib/fetchBlogPosts'; const BlogPage = async () => { const posts = await fetchBlogPosts(); return ( <div> <h1>Our Blog</h1> <BlogArea posts={posts} /> </div> ); }; export default BlogPage;

Step 8: Add Styling

  1. Create a CSS File: Create a CSS file for your blog component or use a global stylesheet. Here's a simple example:
  2. .blog-post { border: 1px solid #ddd; padding: 15px; margin: 15px 0; text-align: center; } .blog-post h3 { font-size: 24px; margin: 10px 0; } .blog-post p { font-size: 16px; color: #666; }

Step 9: Test Your Application

  1. Run Your Development Server: Open your terminal and run:
  2. npm run dev
  3. Open Your Browser: Navigate to http://localhost:3000/blog to see your blog posts fetched from Prismic and displayed in your Next.js application.

Conclusion

You have successfully set up a dynamic blog using Prismic as your CMS and Next.js as your front-end framework. You learned how to fetch data from Prismic, configure image domains in Next.js, and create a component to display your blog posts. With this setup, you can easily manage your blog content through Prismic and render it dynamically in your Next.js application.

SEO Considerations:

  • Structured Data: Implement structured data markup to help search engines understand your content better.
  • Meta Tags: Use appropriate meta tags for title, description, and keywords.
  • Image Optimization: Optimize images for size and alt text.
  • Internal Linking: Link to other relevant blog posts within your content.

Further Enhancements:

  • Pagination: Implement pagination to handle large numbers of blog posts.
  • Search Functionality: Add a search bar to allow users to find specific posts.
  • Comments: Integrate a commenting system to encourage user engagement.
  • Social Sharing: Add social sharing buttons to make it easy for users to share your posts.

By following these steps and exploring further enhancements, you can create a powerful and engaging blog using the combined strengths of Prismic and Next.js.

We'd be interested in learning more about your project.