Compare commits

...

1 Commits

Author SHA1 Message Date
Steven Hall d74c78af6f Add revalidatePaths 2025-05-24 11:19:36 +01:00
@@ -1,15 +1,16 @@
import { type NextRequest, NextResponse } from 'next/server'; import { type NextRequest, NextResponse } from 'next/server';
import { withVerifySignature } from '@v2/lib/routes'; import { withVerifySignature } from '@v2/lib/routes';
import { revalidateTag } from 'next/cache'; import { revalidatePath, revalidateTag } from 'next/cache';
interface JsonBody { interface JsonBody {
tags: string[]; tags: string[];
paths?: string[];
} }
/** /**
* Revalidate cached data based on tags. * Revalidate cached data based on tags.
* The body should be a JSON with { tags: string[] } * The body should be a JSON with { tags: string[], paths?: string[] }
*/ */
export async function POST(req: NextRequest) { export async function POST(req: NextRequest) {
return withVerifySignature<JsonBody>(req, async (body) => { return withVerifySignature<JsonBody>(req, async (body) => {
@@ -22,12 +23,27 @@ export async function POST(req: NextRequest) {
); );
} }
if (body.paths && !Array.isArray(body.paths)) {
return NextResponse.json(
{
error: 'paths must be an array',
},
{ status: 400 }
);
}
body.tags.forEach((tag) => { body.tags.forEach((tag) => {
// biome-ignore lint/suspicious/noConsole: we want to log here // biome-ignore lint/suspicious/noConsole: we want to log here
console.log(`Revalidating tag: ${tag}`); console.log(`Revalidating tag: ${tag}`);
revalidateTag(tag); revalidateTag(tag);
}); });
body.paths?.forEach((path) => {
// biome-ignore lint/suspicious/noConsole: we want to log here
console.log(`Revalidating path: ${path}`);
revalidatePath(path);
});
return NextResponse.json({ return NextResponse.json({
success: true, success: true,
}); });