feat: add type=all to revalidate all category pages
This commit is contained in:
@@ -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 })
|
||||
|
||||
Reference in New Issue
Block a user