All Blog Articles

Building a Website with Payload CMS and NextJS: Dynamic Pages from Collections - Part 3

Media
Sandro WegmannMay 9, 2025
Transform Payload CMS collections into dynamic NextJS pages with two powerful implementation methods.

In this article, we will delve into how to auto-generate pages from a collection in the backend using Payload CMS and Next.js. This process is vital for developers who want to streamline their workflow and enhance the user experience on their websites. The original video by AllAboutPayload covers two methods: a simple and an advanced approach. We'll explore both methods, providing you with a comprehensive understanding of how to implement these techniques.

This step-by-step guide is based on AllAboutPayload's comprehensive YouTube tutorial available at https://www.youtube.com/watch?v=_pwBm0T7eEg. If you prefer visual learning, check out the original video for real-time demonstrations and additional context on generating dynamic pages from Payload CMS collections in NextJS. The video complements this written guide with helpful visual examples of both implementation methods.

Table of Contents

Understanding the Basics

The main idea is to create a collection of blog posts in Payload CMS and convert them into static pages automatically. This method is particularly useful for sites that require dynamic content but want to maintain the performance benefits of static pages.


Setting Up Your Project

To begin, we need to set up a new collection in our Payload CMS project. This collection will be called blog posts. The first step involves creating a new dynamic route in our front end. This is achieved by creating a folder and a dynamic route slug JS file. This file serves as a template for rendering blog posts.


Creating the Blog Posts Collection

Navigate to your Payload CMS dashboard and create a new collection named blog posts. This collection will allow you to manage your blog content seamlessly.

Make sure to set the access roles to read, which is an error function that always returns true. This is crucial because by default, Payload only allows access to collections when users are logged in. For a public blog, you want everyone to be able to read the posts.

payload-cms-blog-posts-collection-setup.jpg


Creating the Dynamic Route

Next, create a new folder in your pages directory and name it blog posts. Within this folder, create a dynamic route file named [...slug].js. This will allow you to access each blog post via a URL structure like /blog-posts/first-post.


Implementing the Simple Method

In this section, we'll look at the simpler method of generating static pages. This method is straightforward but has some limitations.

Creating the React Component

In your dynamic route file, you will create a React component that utilizes the data fetched from the blog posts collection. Here’s a basic structure of what your component should look like:

import { useEffect } from 'react';
import { useRouter } from 'next/router';

const BlogPost = ({ post }) => {
    return (
        
{post.title}
{post.body}

    );
};

export default BlogPost;

In this component, you’ll render the title and body of the blog post. However, note that this method is fixed, meaning the aesthetics and structure cannot be easily modified from the CMS.


Fetching Blog Posts

To fetch the blog posts from the API, you will need to implement two functions: getStaticPaths and getStaticProps. The getStaticPaths function will fetch all blog posts and return their slugs, while getStaticProps will retrieve specific post data based on the slug.

export async function getStaticPaths() {
    const res = await fetch('YOUR_API_URL');
    const posts = await res.json();
    const paths = posts.map(post => ({
        params: { slug: post.slug },
    }));
    return { paths, fallback: false };
}

export async function getStaticProps({ params }) {
    const res = await fetch(`YOUR_API_URL/${params.slug}`);
    const post = await res.json();
    return { props: { post } };
}
nextjs-dynamic-routes-getstaticpaths-implementation.jpg


Limitations of the Simple Method

While the simple method is effective, it has its drawbacks:

  • Fixed Aesthetics: Changes to how the blog post is displayed must be made in the code, which can be cumbersome.
  • Static URL Prefix: The prefix for accessing blog posts is fixed, limiting flexibility.

Moving to the Advanced Method

For those looking for more flexibility, the advanced method is an excellent choice. This approach involves creating a page templates collection in addition to the blog posts collection.

Creating Page Templates

The page templates will allow you to customize how your blog posts are displayed dynamically. Start by creating a new collection named page templates in your Payload CMS dashboard.


In this collection, you will define fields similar to the blog posts collection, but you will also add a field for selecting the data collection that the template will apply to. In this case, it will be the blog posts collection.

Implementing the Dynamic Route for Templates

Once your page templates collection is set up, you will need to adjust your dynamic route to fetch both the page templates and the blog posts. This will allow you to render the blog posts according to the specified layout in the page template.

export async function getStaticPaths() {
    const res = await fetch('YOUR_API_URL/page-templates');
    const templates = await res.json();
    // Fetch blog posts and map for paths
}

export async function getStaticProps({ params }) {
    // Fetch the specific page template and blog post
}


Using EJS Placeholders

One of the most powerful features of this advanced method is the use of EJS placeholders. These placeholders allow you to dynamically insert data into your templates. For example, you can use <%= title %> to represent the title of a blog post in your template layout.

payload-cms-page-template-creation-interface.jpg


Conclusion

In summary, we explored how to auto-generate pages from a collection in Payload CMS using Next.js. The simple method provides a straightforward approach, while the advanced method offers greater flexibility and customization through page templates and EJS placeholders. Each method has its pros and cons, and the choice depends on your project requirements.

For more detailed instructions and code snippets, check out the original video by AllAboutPayload. If you have any questions, need help with implementation, or have suggestions for future topics, feel free to reach out!

FAQ

1. What is Payload CMS?

Payload CMS is a headless content management system designed for developers to create and manage content efficiently.

2. What is Next.js?

Next.js is a React framework that enables server-side rendering and static site generation, making it ideal for building high-performance web applications.

3. Can I use the simple method for production?

Yes, the simple method is suitable for production use, but it may lack the flexibility needed for more complex projects.

4. What are the benefits of using EJS placeholders?

EJS placeholders allow for dynamic content rendering in templates, making it easier to customize how data is displayed without modifying the code directly.

5. How can I get started with Payload CMS and Next.js?

To get started, follow the setup instructions provided in the original video and experiment with both methods to see which best fits your needs.