feat: add type=all to revalidate all category pages

This commit is contained in:
achmad
2026-05-29 02:10:49 +07:00
parent 23d2d31a27
commit 953a222b35
2 changed files with 23 additions and 3 deletions
+21 -1
View File
@@ -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 })
+2 -2
View File
@@ -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 .