2024-09-07 16:08:56 +02:00
|
|
|
/**
|
|
|
|
* This code is responsible for revalidating the cache when a post or author is updated.
|
|
|
|
*
|
|
|
|
* It is set up to receive a validated GROQ-powered Webhook from Sanity.io:
|
|
|
|
* https://www.sanity.io/docs/webhooks
|
|
|
|
*
|
|
|
|
* 1. Go to the API section of your Sanity project on sanity.io/manage or run `npx sanity hook create`
|
|
|
|
* 2. Click "Create webhook"
|
|
|
|
* 3. Set the URL to https://YOUR_NEXTJS_SITE_URL/api/revalidate
|
|
|
|
* 4. Dataset: Choose desired dataset or leave at default "all datasets"
|
|
|
|
* 5. Trigger on: "Create", "Update", and "Delete"
|
|
|
|
* 6. Filter: _type == "blog"
|
|
|
|
* 7. Projection: Leave empty
|
|
|
|
* 8. Status: Enable webhook
|
|
|
|
* 9. HTTP method: POST
|
|
|
|
* 10. HTTP Headers: Leave empty
|
|
|
|
* 11. API version: v2023-08-08
|
|
|
|
* 12. Include drafts: No
|
|
|
|
* 13. Secret: Set to the same value as SANITY_REVALIDATE_SECRET (create a random secret if you haven't yet)
|
|
|
|
* 14. Save the cofiguration
|
|
|
|
*/
|
|
|
|
|
2024-09-07 15:49:20 +02:00
|
|
|
import { isValidSignature, SIGNATURE_HEADER_NAME } from "@sanity/webhook";
|
2024-09-07 17:27:18 +02:00
|
|
|
import { client } from "@/lib/sanity";
|
2024-09-07 15:49:20 +02:00
|
|
|
import { revalidatePath } from "next/cache";
|
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
|
|
|
|
const secret = `${process.env.SANITY_WEBHOOK_SECRET}`;
|
|
|
|
|
|
|
|
export async function POST(req: NextRequest) {
|
|
|
|
const body = await req.json();
|
|
|
|
const rawBody = JSON.stringify(body);
|
|
|
|
|
|
|
|
if (
|
|
|
|
!(await isValidSignature(
|
|
|
|
rawBody,
|
2024-09-07 15:59:59 +02:00
|
|
|
req.headers.get(SIGNATURE_HEADER_NAME) ?? "",
|
2024-09-07 15:49:20 +02:00
|
|
|
secret.trim()
|
|
|
|
))
|
|
|
|
) {
|
|
|
|
return NextResponse.json({ message: "Invalid signature" }, { status: 401 });
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (body._type == "blog") {
|
2024-09-07 22:12:05 +02:00
|
|
|
if (body.slug.current) {
|
|
|
|
revalidatePath(`/blog/${body.slug.current}`);
|
|
|
|
revalidatePath("/sitemap.xml");
|
|
|
|
}
|
2024-09-07 17:27:18 +02:00
|
|
|
revalidatePath("/blog");
|
|
|
|
|
|
|
|
// Change in /blog/page/[id] route and in BlogCards component too!
|
|
|
|
const cardsPerPage = 6;
|
|
|
|
|
|
|
|
const totalPostsQuery = `count(*[_type == 'blog'])`;
|
2024-09-07 17:32:09 +02:00
|
|
|
const totalPosts: number = await client.fetch(
|
|
|
|
totalPostsQuery,
|
|
|
|
{},
|
|
|
|
{ cache: "no-store" }
|
|
|
|
);
|
2024-09-07 17:27:18 +02:00
|
|
|
|
|
|
|
const totalPages = Math.ceil(totalPosts / cardsPerPage);
|
|
|
|
for (let i = 1; i <= totalPages + 1; i++) {
|
|
|
|
revalidatePath(`/blog/page/${i.toString()}`);
|
|
|
|
}
|
|
|
|
|
2024-09-07 15:49:20 +02:00
|
|
|
return NextResponse.json({
|
2024-09-07 17:27:18 +02:00
|
|
|
message: `Revalidated "${body._type}" with slug "${body.slug.current}"`
|
2024-09-07 15:49:20 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return NextResponse.json({ message: "No managed type" });
|
|
|
|
} catch (err) {
|
|
|
|
return NextResponse.json(
|
|
|
|
{ message: "Error revalidating" },
|
|
|
|
{ status: 500 }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|