From 953a222b352d965c0f96696d505796f91fb68cda Mon Sep 17 00:00:00 2001 From: achmad Date: Fri, 29 May 2026 02:10:49 +0700 Subject: [PATCH] feat: add type=all to revalidate all category pages --- app/api/revalidate/route.ts | 22 +++++++++++++++++++++- scripts/revalidate.sh | 4 ++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/app/api/revalidate/route.ts b/app/api/revalidate/route.ts index 7899064..cbd2268 100644 --- a/app/api/revalidate/route.ts +++ b/app/api/revalidate/route.ts @@ -1,6 +1,6 @@ import { revalidatePath } from 'next/cache' import { NextRequest, NextResponse } from 'next/server' -import { createDirectus, rest, staticToken, readItem } from '@directus/sdk' +import { createDirectus, rest, staticToken, readItem, readItems } from '@directus/sdk' export async function POST(req: NextRequest) { const body = await req.json() @@ -14,6 +14,26 @@ export async function POST(req: NextRequest) { return NextResponse.json({ revalidated: true, path: '/' }) } + if (body.type === 'all') { + const directus = createDirectus(process.env.DIRECTUS_URL!) + .with(staticToken(process.env.DIRECTUS_TOKEN!)) + .with(rest()) + + revalidatePath('/') + + const categories = await directus.request( + readItems('categories', { fields: ['slug'] }) + ) as { slug: string }[] + + const paths = ['/'] + for (const cat of categories) { + revalidatePath(`/${cat.slug}`) + paths.push(`/${cat.slug}`) + } + + return NextResponse.json({ revalidated: true, paths }) + } + const { article_id } = body if (!article_id) { return NextResponse.json({ error: 'Missing article_id' }, { status: 400 }) diff --git a/scripts/revalidate.sh b/scripts/revalidate.sh index efe9807..96d52e7 100755 --- a/scripts/revalidate.sh +++ b/scripts/revalidate.sh @@ -4,7 +4,7 @@ set -euo pipefail URL="${1:-https://kotobane.achmad.dev}" SECRET="${REVALIDATE_SECRET:-1964edNGrm1xqE2oznfeahdheP7oAfwAob4fIxe1Gzo}" -echo "==> Revalidating $URL ..." +echo "==> Revalidating homepage + all categories on $URL ..." curl -s -X POST "$URL/api/revalidate" \ -H "Content-Type: application/json" \ - -d "{\"secret\":\"$SECRET\",\"type\":\"homepage\"}" | jq . + -d "{\"secret\":\"$SECRET\",\"type\":\"all\"}" | jq .