mirror of
https://github.com/vercel/commerce.git
synced 2025-05-15 14:06:59 +00:00
41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import { isValidSignature, SIGNATURE_HEADER_NAME } from '@sanity/webhook';
|
|
import { revalidatePath } from 'next/cache';
|
|
import { headers } from 'next/headers';
|
|
import { NextRequest, NextResponse } from 'next/server';
|
|
|
|
const SANITY_WEBHOOK_SECRET = `${process.env.SANITY_WEBHOOK_SECRET}`;
|
|
|
|
export async function POST(request: NextRequest) {
|
|
// Await the response from our request.
|
|
const requestData = await request.json();
|
|
|
|
const path = request.nextUrl.searchParams.get('path') || '/'
|
|
|
|
console.log(`===== Path: ${path}`)
|
|
|
|
// Get headers.
|
|
const headersList = headers();
|
|
|
|
// Get Sanity webhook signature header name.
|
|
const signature = `${headersList.get(SIGNATURE_HEADER_NAME)}`;
|
|
const isValid = isValidSignature(JSON.stringify(requestData), signature, SANITY_WEBHOOK_SECRET);
|
|
|
|
// Log out validity of request.
|
|
console.log(`===== Is the webhook request valid? ${isValid}`);
|
|
|
|
// If not valid, return.
|
|
if (!isValid) {
|
|
NextResponse.json({ success: false, message: 'Invalid signature' });
|
|
return;
|
|
}
|
|
|
|
const slug = requestData.slug;
|
|
const locale = requestData.locale;
|
|
const type = requestData.type;
|
|
const pathToRevalidate = slug;
|
|
|
|
revalidatePath(slug);
|
|
|
|
console.log(`===== Revalidated path: ${pathToRevalidate}`);
|
|
return NextResponse.json({ revalidated: true, now: Date.now() });
|
|
} |