Table of contents
Introduction
On-demand revalidation is a powerful feature that allows us to dynamically update static content in Next.js applications. This technique bridges the gap between static site generation (SSG) and server-side rendering (SSR), offering the best of both worlds.
Together, these tools enable a flexible, performant, and easy-to-manage content update system. This article will guide you through setting up on-demand revalidation, ensuring your Next.js app always serves the most up-to-date content from your Directus CMS.
For this guide, I'll be assuming you already have your Next.js setup with App router and your Directus instance running.
Setting up the environment
Let's start by creating an .env
file at the root of your Next.js app and add REVALIDATION_SECRET
key with a secret value.
Setting up the re-validation API route in Next.js
Now let's create the API route that will handle our re-validation requests.
Create a new file at: app/api/revalidate_tag/route.ts
, then place this script inside:
Setting up Directus Flow
In your Directus frontend, head to Settings -> Flows -> Create Flow (top right).
In Flow Setup, choose a descriptive name for your flow. I'll be naming mine "Revalidate Posts"
Now in Trigger Setup:
- Choose Event Hook.
- Set Type to Action (non-blocking).
- In Scope, set when you want to trigger the flow. In my case I will be doing it on
items.create
,items.update
anditems.delete
- In Collections, select the collection/s that should be targeted. In my case this will be
Posts
.
Now press save and we should be inside the Flow dashboard. We're now at the final step of this guide (finally).
Let's create a new operation by clicking the + symbol to the right of our newly created trigger. In the operation modal, choose a descriptive name for your operation. I will be using "Revalidation request".
- Select
Webhook / Request URL
. - For Method, choose
POST
- For URL, you'll want this to point to your Next.js API url, for example "http://127.0.0.1:3000/api/revalidate_tag"
- In Request Body add (remember you can change tag to whatever tag you have setup).
That's it! Let's now test things out to see if this is working as expected.
Testing the Setup
Since the dev server has no cache, we need to create a build and run the build server:
Now go to your Directus frontend and create/delete/update a new post (or whatever item for the collection you're targeting), refresh your Next.js page and your changes should be visible almost instantly!