Skip to content

Hugow1/WP-Revalidate-NextJS-URL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Revalidate NextJS URL

This plugin is used to revalidate the NextJS URL when saving a post or page. It is used in conjunction with the NextJS revalidate api.

This plugin is intended for the use with headless WordPress and NextJS. It assumes you have setup on demand revalidation in NextJS.

This can be done by adding the following file to your NextJS project:

// pages/api/revalidate.js
export default async function handler(req, res) {
    const { path, token } = req.query;

    if (token !== process.env.SECRET_TOKEN) {
        return res.status(401).json({ message: "Invalid token" });
    } else if (path.length === 0) {
        return res.status(401).json({ message: "Path is required" });
    }

    try {
        await res.revalidate(path);
    } catch (err) {
        return res.status(500).send("Error revalidating page");
    }

    return res.status(200).json({
        revalidated: true,
        message: `Path ${path} revalidated successfully`,
    });
}

Make sure you set a SECRET_TOKEN in your environment variables.

Installation

Download the plugin as a zip file and upload it to your WordPress site. Activate the plugin. This will add a new settings page to your WordPress site. You will need to add a secret token to the settings page. This token will be used to authenticate the revalidation request. Also, you will need to add the URL to your NextJS project like this https://example.com/api/revalidate/ or http://localhost:3000/api/revalidate/

Example:

Screenshot 2022-11-29 at 14 50 24

Usage

Once the plugin is installed and activated with the correct setting, this plugin will automatically revalidate the NextJS URL when a post or page is saved.

About

Handy little plugin which helps you revalidate Nextjs pages when saving content.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages