We detected that your browser language is set to English. Click "Continue" to display the page in set language.

English
kapsys logo
EN
  • EN
  • DE
kapsys logo
EN
  • EN
  • DE
close-icon
  • Image-12-300x300 (2).png
  • Image-13-300x300 (1).png
Kapsys © 2024
EN
  • EN
  • DE
Legal Notice
Next.js and Strapi
User Experience

Building a Blog with Next.js and Strapi: End-To-End Guide For A Modern Web Application

04 October 2023 by Daria Andrieieva

Are you looking to create a blog that's visually appealing, robust, flexible, and easy to maintain? Look no further! In this comprehensive guide, Kapsys will walk you through building a modern blog using Next.js and Strapi. 

By the end of this tutorial, you'll have a fully functional blog that can be easily customized to suit your needs.

thank-you icon

Thank you

We’ve received your message. Someone from our team will contact you soon by email

ContinueBack to main page

Sign up to our blog to stay tuned about the latest industry news.

Why Next.js and Strapi?

Before diving into the step-by-step process, let's understand why Next.js and Strapi are ideal for building a modern blog.

Strapi: The Headless CMS

Strapi is an open-source headless content management system (CMS) that provides a powerful and customizable backend for your application. It's perfect for managing your blog's content, allowing you to create, edit, and organize your posts, categories, and media effortlessly. Strapi also provides a RESTful API, which makes it easy to integrate with any frontend technology like Next.js.
 

Read: What Is Strapi, And Why Is It A Leading Headless CMS?

Next.js: The react framework

Next.js is a popular React framework that offers server-side rendering (SSR) and static site generation (SSG) capabilities. This ensures that your blog is fast, SEO-friendly, and highly customizable. Next.js allows you to build dynamic, interactive web applications with ease.

Now that we've got a clear picture of why we're using Next.js and Strapi, let's start building our modern blog.

Prerequisites

Before we begin with Next.js and Strapi, make sure you have the following requirements in place:

  • Node.js: Ensure that you have Node.js installed on your system. You can download it from nodejs.org.
     

  • Strapi CLI: Install the Strapi CLI globally by running npm install -g strapi@latest.
     

  • Next.js: Make sure you have Next.js installed globally using npm install -g next.
     

  • Text Editor/IDE: Choose a text editor or integrated development environment (IDE) that you are comfortable with. Popular choices include Visual Studio Code, Sublime Text, and Atom.

Step 1: Setting Up Strapi

Let's start by setting up Strapi as our backend CMS. Follow these steps:

  • Create a New Strapi Project: Open your terminal and run the following command to create a new Strapi project:

npx create-strapi-app my-blog --quickstart
  • Configure Your Strapi Project: Strapi will guide you through the initial setup process, where you can configure your database, create an admin user, and define your content structure. Follow the prompts to complete the setup.
     

  • Start the Strapi Development Server: Once the setup is complete, start the Strapi development server:

cd my-blog

npm run develop
  • Access the Strapi Admin Panel: Open your web browser and navigate to http://localhost:1337/admin. Log in using the admin credentials you created earlier.

Step 2: Creating Content in Strapi

Now that your Strapi backend is set up, it's time to create the content for your blog. We'll start by defining the content types for our blog posts and categories.

  1. Create a Content Type for Blog Posts: In the Strapi admin panel, go to "Content-Types Builder" and click "Create new collection type." Name it "Blog Post" and add fields like title, content, image, and category. Customize the fields according to your blog's needs.
     

  2. Create a Content Type for Categories: Create another " Category " collection type to organize your blog posts. Add a field for the name to represent the category name.
     

  3. Create Blog Posts and Categories: You can start creating blog posts and assigning them to categories using the Strapi admin panel.

Step 3: Setting Up Next.js

With our content ready in Strapi, it's time to set up the front end using Next.js.

  • Create a Next.js App: Open your terminal and run the following command to create a new Next.js app:

npx create-next-app my-blog-frontend
  • Install Dependencies: Change into your Next.js project directory and install the required dependencies:

cd my-blog-frontend
npm install axios react-query

 

We're using Axios for API requests to our Strapi backend and React Query for handling data fetching and caching.
 

  • Configure Environment Variables: Create a .env.local file in your project root and add the following: 

NEXT_PUBLIC_API_URL=http://localhost:1337


This environment variable points to your Strapi backend URL. Make sure it matches the URL where your Strapi server is running.

  • Set Up API Functions: Create an api.js file in your project to define functions for fetching data from the Strapi API:

// api.js
import axios from 'axios';

const API_URL = process.env.NEXT_PUBLIC_API_URL;

export const getBlogPosts = async () => {
  const response = await axios.get(`${API_URL}/blog-posts`);
  return response.data;
};

export const getCategories = async () => {
  const response = await axios.get(`${API_URL}/categories`);
  return response.data;
};
  • Create Pages: In Next.js, pages are automatically routed. Create the necessary pages for your blog, such as index.js, blog/[slug].js, and category/[slug].js.
     

  • Fetching Data: Use React Query to fetch data from your Strapi backend and display it in your Next.js pages. For example, in your index.js:

// pages/index.js
import { useQuery } from 'react-query';
import { getBlogPosts } from '../api';

function HomePage() {
  const { data: blogPosts, error } = useQuery('blogPosts', getBlogPosts);

  if (error) {
    return <div>Error: {error.message}</div>;
  }

  return (
    <div>
      {blogPosts.map((post) => (
        <div key={post.id}>
          <h2>{post.title}</h2>
          <p>{post.content}</p>
        </div>
      ))}
    </div>
  );
}

export default HomePage;

 

  • Style Your Blog: You can use CSS or a CSS-in-JS library like styled components. Customize the appearance to match your brand and design preferences.

Step 4: Integrating Next.js and Strapi

Now that you have both your Strapi backend and Next.js frontend set up, it's time to integrate them.

  • API Integration: In your Next.js pages, use the API functions you defined earlier to fetch and display data from your Strapi backend.
     

  • Dynamic Routing: Implement dynamic routing to display individual blog posts and category pages. Use the getStaticPaths and getStaticProps functions provided by Next.js for this purpose.
     

  • Creating a Blog Post Page: Create a blog/[slug].js page to display individual blog posts based on their slugs. Use getStaticPaths to generate paths for all blog posts and getStaticProps to fetch data for the specific position.
     

  • Creating a Category Page: Similarly, create a category/[slug].js page to display blog posts filtered by category. Implement dynamic routing to generate paths for all types and fetch posts for a specific variety.
     

  • Linking to Blog Posts and Categories: Add navigation links to your blog posts and categories on the homepage and category pages. Use Next.js' Link component to create efficient client-side navigation.
     

  • Pagination: Implement pagination to display a limited number of blog posts per page. This enhances user experience and improves website performance.

Integration of Next.js and Strapi is a straightforward process that requires a lot of your attention, but the results of it are so worth it. 

next js blogs

Step 5: Styling and Customization

While using Next.js and Strapi you can apply custom styles and layout components to make your blog visually appealing and unique. Consider the following:

  • Design and Layout: Create a cohesive design for your blog using CSS or CSS frameworks like Tailwind CSS. Customize the layout, fonts, colors, and overall aesthetics.
     

  • Components: Build reusable components for common elements like headers, footers, navigation menus, and postcards to maintain consistency throughout your blog.
     

  • Responsive Design: Ensure your blog is responsive and looks great on various devices, including desktops, tablets, and mobile phones.
     

  • Animations: Add subtle energies to make your blog more engaging and user-friendly.

Step 6: Deployment

Congratulations, you've built your modern blog with Next.js and Strapi! The final step is to deploy it for the world to see. Here are some deployment options:

  • Vercel: Vercel is an excellent choice for deploying Next.js applications. It offers easy integration with Git and provides automatic deployments.
     

    Read: Web Design and Development With Strapi, Next.js, and Vercel
     

  • Netlify: Netlify is another popular option that supports continuous integration and deployment from your Git repository.
     

  • Heroku: If you prefer deploying your Strapi backend separately, you can deploy it on platforms like Heroku. Ensure you update your frontend's environment variables to the deployed backend URL.
     

  • Custom Server: For more control, you can deploy your Next.js and Strapi applications on a custom server or cloud infrastructure like AWSGoogle Cloud, or Azure.

Conclusion

In this comprehensive guide, we've walked you through building a modern blog using Next.js and Strapi. You've learned how to set up Strapi as your headless CMS, create content, configure Next.js as your frontend framework, and integrate the two seamlessly. You can launch your unique and high-quality blog online with customization, styling, and deployment.
 

Building a blog with Next.js and Strapi offers the perfect combination of content management and front-end flexibility, empowering you to create a blog that stands out in the digital landscape. Get creative, experiment with different features, and start sharing your thoughts and ideas with the world through your beautifully crafted blog. Happy blogging!
 

Keep up with all software trends with Kapsys!