Next.js Discord

Discord Forum

How can i update the static pages if any changes in headless cms posts?

Answered
Japanese Bobtail posted this in #help-forum
Open in Discord
Japanese BobtailOP
Hi there. I still generate almost all pages from my headless cms with generateStaticParams. This works fine, because i've zero loading time in production. My question: How can i update the pages if a post getting deleted or a new post is there? I then have to run build on every change or is there a better solution?

Thanks.
Answered by Ray
create a route handler and call revalidatePath or revalidateTag. And setup a webhook on your cms to make a request to your route handler whenever you change the content
View full answer

12 Replies

Answer
Japanese BobtailOP
I've created a route.js file in app/api with this code:

export async function GET(request) {
return new Response("Hello, Next.js!", {
status: 200,
headers: {}
});
}

But i cant visit it: http://localhost:3000/api/route
@Ray you will need to use `revalidatePath` or `revalidateTag`
Japanese BobtailOP
Yes for sure. Its only for testing. How can i visit this file? http://localhost:3000/api/route dont work.
@Ray what is the path of the route.js
Japanese BobtailOP
src\app\api\route.js
@Ray then it should be http://localhost:3000/api
Japanese BobtailOP
Thank you so much sir. I will now create the route.
@Ray create a route handler and call `revalidatePath` or `revalidateTag`. And setup a webhook on your cms to make a request to your route handler whenever you change the content
Japanese BobtailOP
Thats correct?

import { revalidatePath } from "next/cache";

export async function GET(request) {
const token = request?.nextUrl?.searchParams?.get("t") || "";
if (token !== process.env.REVALIDATE_TOKEN) {
return new Response("access denied", {
status: 400
});
}

try {
revalidatePath("/");
revalidatePath("/blog");
revalidatePath("/products");
} catch (error) {
return new Response(webhook error: ${error.message}, {
status: 400
});
}

return new Response("success", {
status: 200
});
}
@Ray look good
Japanese BobtailOP
Its crazy damn good. Thank you so much sir and if you want a coffee, gimme a notice.