How to Use Strapi with Next.js Step by Step
Integrating Strapi with Next.js is one of the most powerful combinations for developers who want to build high-performance, SEO-friendly, and content-driven web applications. Strapi provides a flexible, headless CMS, while Next.js offers a robust React-based framework with server-side rendering (SSR) and static site generation (SSG) capabilities. In this step-by-step guide, we’ll walk you through the entire process of connecting Strapi with Next.js, from setup to deployment, and cover essential SEO best practices to ensure your project ranks well in search results.
What Are Strapi and Next.js?
Understanding Strapi
Strapi is an open-source headless CMS that enables developers to build customizable APIs quickly. It supports both REST and GraphQL, allowing developers to manage content easily and deliver it to any front-end framework. Strapi’s admin panel is built with React, and it gives developers and content editors a clean interface for managing collections, single types, and media assets.
Understanding Next.js
Next.js, developed by Vercel, is a React framework that provides features like server-side rendering, static site generation, and API routes out of the box. These capabilities make Next.js ideal for building SEO-optimized and high-performing websites that load fast and rank better on search engines.
Why Use Strapi with Next.js?
The combination of Strapi and Next.js creates a modern, decoupled architecture that separates content management from the presentation layer. This approach improves scalability, enhances performance, and simplifies content delivery. Here are a few reasons to use Strapi with Next.js:
- SEO Benefits: With SSR and SSG, Next.js ensures search engines can easily crawl and index your pages.
- Scalability: Strapi’s API-driven content architecture allows easy scaling for multiple frontends.
- Performance: Next.js pages load faster due to pre-rendering and incremental static regeneration.
- Flexibility: Developers can build dynamic pages and fetch content seamlessly using Strapi’s REST or GraphQL endpoints.
- Ease of Use: Strapi’s admin dashboard allows non-technical users to manage content easily.
Step-by-Step Guide: How to Use Strapi with Next.js
Step 1: Setting Up the Strapi Backend
First, install Strapi and create a new project. You can use either npm or yarn for installation.
npx create-strapi-app my-strapi-app --quickstart
This command will create a Strapi project and start a local development server at http://localhost:1337
. The --quickstart
flag automatically configures a SQLite database and runs Strapi in development mode.
After setup, open your browser and navigate to the admin panel. Create your first admin account and explore the Strapi dashboard.
Step 2: Creating Content Types in Strapi
Next, create a new content type (e.g., Articles) to manage dynamic content for your Next.js site.
- Go to Content-Types Builder in the Strapi dashboard.
- Click Create new collection type.
- Name it Article.
- Add fields like Title (Text), Slug (UID), Content (Rich Text), and Image (Media).
Save your changes and restart the Strapi server. You can now create and publish articles from the Content Manager section.
Step 3: Setting Up Permissions and Public Access
To allow your Next.js frontend to access Strapi content, you must enable API permissions.
- Go to Settings → Roles → Public.
- Scroll down to the Article section and enable find and findOne.
- Click Save to apply changes.
Step 4: Setting Up Next.js
Create a new Next.js project in a separate folder:
npx create-next-app my-next-app
Once the setup completes, navigate into the directory:
cd my-next-app
Step 5: Fetching Data from Strapi
Next, install Axios (or use the native fetch API) to fetch data from Strapi:
npm install axios
Create a new file lib/api.js
to manage API requests:
import axios from 'axios';
export const fetchArticles = async () => {
const res = await axios.get('http://localhost:1337/api/articles?populate=*');
return res.data.data;
};
Step 6: Displaying Strapi Content in Next.js
Modify the pages/index.js
file to fetch and render the articles:
import { fetchArticles } from '../lib/api';
export default function Home({ articles }) {
return (
<div>
<h1>Strapi + Next.js Blog</h1>
{articles.map((article) => (
<div key={article.id}>
<h2>{article.attributes.title}</h2>
<p>{article.attributes.content}</p>
</div>
))}
</div>
);
}
export async function getStaticProps() {
const articles = await fetchArticles();
return { props: { articles }, revalidate: 10 };
}
This setup uses Static Site Generation (SSG) to fetch and pre-render content at build time. The revalidate
property ensures incremental static regeneration, so your pages automatically update with new content.
Step 7: Dynamic Routes for Articles
To create individual article pages, set up dynamic routes:
mkdir pages/articles
Then create pages/articles/[slug].js
:
import { fetchArticles } from '../../lib/api';
import Link from 'next/link';
export default function Article({ article }) {
return (
<article>
<h1>{article.attributes.title}</h1>
<div>{article.attributes.content}</div>
<Link href="/">Back to home</Link>
</article>
);
}
export async function getStaticPaths() {
const articles = await fetchArticles();
const paths = articles.map((article) => ({
params: { slug: article.attributes.slug },
}));
return { paths, fallback: false };
}
export async function getStaticProps({ params }) {
const articles = await fetchArticles();
const article = articles.find(
(a) => a.attributes.slug === params.slug
);
return { props: { article } };
}
Step 8: SEO Optimization Tips
To ensure your Next.js + Strapi website ranks higher on search engines, follow these SEO checklist points:
- Use Meta Tags: Configure dynamic
<Head>
tags for titles, descriptions, and canonical URLs. - Optimize Slugs: Use descriptive, keyword-rich slugs in Strapi for better indexing.
- Schema Markup: Add JSON-LD structured data to improve search visibility.
- Image Optimization: Use the Next.js
Image
component for optimized image delivery. - Performance: Enable caching and use ISR to keep content fresh and fast.
- Internal Linking: Link related pages and articles to improve navigation and SEO signals.
Deployment Considerations
You can deploy your Strapi backend to services like Render, DigitalOcean, or Heroku, and host your Next.js app on Vercel for seamless performance. Remember to update the API URLs and configure environment variables securely.
Integrating Strapi with Next.js for Headless CMS Projects
When building enterprise-level applications or marketing websites, Strapi with Next.js gives you complete control over both content and performance. Teams can scale content delivery across multiple channels while maintaining SEO standards and fast load times.
Common Mistakes to Avoid
- Not setting up API permissions correctly in Strapi.
- Using client-side rendering instead of SSR or SSG for SEO pages.
- Ignoring content slug fields and relying on IDs.
- Forgetting to revalidate static pages after new content is added.
- Not using proper meta tags or sitemap generation.
FAQ: Using Strapi with Next.js
1. Is Strapi free to use with Next.js?
Yes, Strapi is open-source and completely free. You can use it with Next.js to build production-grade applications at no cost. Enterprise support is available if needed.
2. Should I use REST or GraphQL with Strapi and Next.js?
Both options work well. REST is simpler to set up, while GraphQL provides more flexibility and control over data queries. Choose based on your project complexity.
3. How can I improve SEO for a Strapi + Next.js website?
Use SSR or SSG in Next.js, create clean slugs, add meta tags dynamically, and ensure your site’s structure is well-linked. Also, implement schema markup and optimize images.
4. Can I deploy Strapi and Next.js together?
Yes, but it's best to deploy them separately — Strapi as a backend API on a platform like Render or DigitalOcean, and Next.js on Vercel or Netlify for optimal performance.
5. What are some alternatives to Strapi for Next.js?
Alternatives include Sanity, Contentful, and Hygraph (GraphCMS). However, Strapi offers greater flexibility for self-hosting and customization.
Conclusion
Building a website using Strapi with Next.js gives developers and marketers the best of both worlds — a flexible CMS and a blazing-fast frontend. By following the steps in this guide, you can easily set up a modern, SEO-optimized web application ready for scalability and performance.
For professional assistance in building, optimizing, and marketing your Next.js projects, reach out to WEBPEAK, a full-service digital marketing company providing expert Web Development, Digital Marketing, and SEO services.