All Blog Articles

Sync Data to Frontend | Payload CMS + NextJS - Part 5

Media
Sandro WegmannMay 14, 2025
Visual guide to implementing real-time data synchronization between Payload CMS and Next.js using page-specific revalidation.

Welcome back, everyone! Today, we’re diving into the exciting world of syncing your Payload CMS backend with your Next.js frontend. This walkthrough will help you understand the importance of keeping your data in sync, the strategies you can implement, and how to set everything up efficiently. So, let’s get started!


This article is the written step-by-step companion to our detailed video tutorial available on YouTube: Sync Data to Frontend | Payload CMS + NextJS Part 5. For visual learners or those who prefer following along with video instruction, the video covers the same content but offers additional context and real-time demonstrations of each step in the process.

Table of Contents


Why Sync Your Backend and Frontend?

Before we delve into the technical aspects, let's discuss why syncing is essential. If you've ever worked with WordPress, you might have noticed how quickly changes reflect on your website. You make a few tweaks, and voilà—everything updates almost instantaneously. But when it comes to Payload CMS, things work a bit differently.

Payload is a headless CMS, meaning it doesn’t generate the actual webpage. Instead, you need to do this using Next.js. Data is fetched through either a REST API or a GraphQL API, which allows for a more flexible and powerful approach. This separation of backend and frontend comes with a plethora of benefits, such as:

  • Utilizing data across different platforms like mobile apps or IoT devices.
  • Improved performance due to reduced loading times.
  • Enhanced security, as the frontend and backend can be managed independently.


Strategies for Syncing

When it comes to keeping your Payload CMS backend and Next.js frontend in sync, there are three primary strategies you can implement:

1. Server-Side Rendering (SSR)

With server-side rendering, every time a request is made to your website, the server generates a new HTML page. This means that users are always served the most current data. While this method ensures real-time updates, it can be resource-intensive, as the server has to re-render the page for every request.

2. Incremental Static Regeneration (ISR)

Incremental static regeneration allows you to generate a page at build time and cache it on the server. When a request comes in, the cached page is served to the user, but the server simultaneously triggers a re-render in the background. This means users get fast loading times while still benefiting from updated content, which can be set to refresh at intervals of your choosing, like every ten seconds or two hours.

3. Static Site Generation (SSG)

Static site generation is similar to ISR but doesn’t automatically regenerate the page. Instead, it’s built once at build time, and any updates require a manual refresh. This method is best suited for content that doesn’t change frequently.

Choosing the Right Strategy

Throughout your project, you may find that a combination of these strategies works best. For instance, SSR and ISR handle real-time data well, while SSG is ideal for static content. It’s essential to assess your project's needs and choose the appropriate method accordingly.


Types of Refresh Mechanisms

Now that we’ve discussed the strategies, let’s look at how you can implement refresh mechanisms:

Page-Specific Refresh

This method involves refreshing only a specific page, such as a blog article. You can achieve this by creating a custom Next.js endpoint that triggers the revalidation of that particular page. This is a more efficient approach than refreshing the entire site.

Full Site Rebuild

A full site rebuild ensures that all pages are regenerated. While this guarantees that every piece of content is up-to-date, it can be time-consuming and resource-intensive, taking anywhere from one to ten minutes, depending on the size of your site. You can implement this using tools like Vercel deploy hooks.


When to Use Each Method

Choosing between a page-specific refresh and a full site rebuild depends on the scenario:

  • If you’re updating a single blog article, a page-specific refresh is ideal.
  • If the updated content affects multiple components across the site, consider a full rebuild.
  • For admin changes that don’t impact the frontend, no refresh is necessary.


Demonstration: Implementing Page-Specific Refresh

Let’s jump into some code! We’ll implement a page-specific refresh in our Next.js application. For this, we’ll create a custom endpoint that triggers the revalidation of a specific page.

nextjs-getstaticprops-page-revalidation-code.jpg

Start by creating a new file called revalidate.js in the pages/api directory. This file will handle the revalidation requests.

const handler = (req, res) => {
    const { slug, secret } = req.query;

    if (secret !== process.env.FRONTEND_SECRET) {
        return res.status(401).json({ message: "Invalid token" });
    }

    if (!slug) {
        return res.status(400).json({ message: "Missing slug" });
    }

    // Trigger revalidation
    res.revalidate(slug)
        .then(() => res.json({ revalidated: true }))
        .catch(() => res.status(500).send("Error revalidating"));
};


export default handler;

In this code snippet, we’re extracting the slug and secret from the request. If the secret doesn’t match, we return a 401 error. If everything checks out, we call the res.revalidate method to trigger the revalidation process.


Setting Up Environment Variables

Next, we need to define our frontend secret in the .env.local file. This secret will be used to secure our endpoint:

FRONTEND_SECRET=your_secret_here

Additionally, we need to specify the frontend URL:

FRONTEND_URL=http://localhost:3000


Utility Functions for Updating Frontend

To make the process more manageable, we’ll create a utility function that can be called from our admin panel or hooks. This function will send a request to our revalidation endpoint:

import axios from 'axios';

const updateFrontend = async (slug) => {
    const response = await axios.get(`${process.env.FRONTEND_URL}/api/revalidate?slug=${slug}&secret=${process.env.FRONTEND_SECRET}`);
    return response.data;
};

This function checks for the necessary environment variables and sends a GET request to our revalidation endpoint. If successful, it will log a message indicating that the revalidation has been triggered.

Hooking Up to Payload Collections

Now, let’s tie everything together by connecting our update function to the Payload collections. We’ll add a hook that triggers our update function after changes are made to a page.

const afterChangeHook = async ({ doc }) => {
    await updateFrontend(doc.slug);
};


payload.collections.pages.hooks.afterChange.push(afterChangeHook);

This hook will ensure that whenever a page is updated in the backend, the frontend will also be revalidated automatically.

Testing the Setup

With everything in place, it’s time to test our implementation. Make some changes in the backend and save them. You should see a console log indicating that revalidation has been triggered. Refresh the page, and voilà! The new content should now be displayed.

payload-nextjs-frontend-revalidation-result.jpg


Conclusion

Today, we covered how to sync your Payload CMS backend with your Next.js frontend effectively. We explored different strategies for syncing, implemented a page-specific refresh mechanism, and learned how to set up environment variables and utility functions.

By keeping your data in sync, you ensure that users always see the most current information, enhancing the overall user experience. If you have any questions or would like to dive deeper into any aspect of this process, feel free to reach out in the comments below. Happy coding!

FAQ

What is Payload CMS?

Payload CMS is a headless content management system that allows developers to manage content in a flexible way, separate from the frontend presentation.

How does Next.js work with Payload CMS?

Next.js fetches data from Payload CMS using REST or GraphQL APIs, allowing developers to create dynamic and performant web applications.

What is the difference between SSR, ISR, and SSG?

SSR (Server-Side Rendering) regenerates the page on each request, ISR (Incremental Static Regeneration) serves cached pages while updating in the background, and SSG (Static Site Generation) generates pages at build time without automatic updates.

Can I use Payload CMS with other frontend frameworks?

Yes, Payload CMS can be integrated with any frontend framework that supports REST or GraphQL APIs, making it versatile for various projects.

For more information and documentation on Payload CMS, check out Payload CMS Documentation.

Thanks for reading, and see you in the next article!