From d8257bac3fa9f656a92d699c851a6096e74ec199 Mon Sep 17 00:00:00 2001 From: Rob Decker Date: Sat, 16 Nov 2024 12:36:30 -0700 Subject: [PATCH] feat(basic-starter): add tag revalidation to basic-starter and graphql-starter --- starters/basic-starter/app/api/revalidate/route.ts | 12 +++++++----- starters/graphql-starter/app/api/revalidate/route.ts | 12 +++++++----- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/starters/basic-starter/app/api/revalidate/route.ts b/starters/basic-starter/app/api/revalidate/route.ts index 9882273af..2a7e4ec0f 100644 --- a/starters/basic-starter/app/api/revalidate/route.ts +++ b/starters/basic-starter/app/api/revalidate/route.ts @@ -1,9 +1,10 @@ -import { revalidatePath } from "next/cache" +import { revalidatePath, revalidateTag } from "next/cache" import type { NextRequest } from "next/server" async function handler(request: NextRequest) { const searchParams = request.nextUrl.searchParams const path = searchParams.get("path") + const tags = searchParams.get("tags") const secret = searchParams.get("secret") // Validate secret. @@ -11,13 +12,14 @@ async function handler(request: NextRequest) { return new Response("Invalid secret.", { status: 401 }) } - // Validate path. - if (!path) { - return new Response("Invalid path.", { status: 400 }) + // Either tags or path must be provided. + if (!path && !tags) { + return new Response("Missing path or tags.", { status: 400 }) } try { - revalidatePath(path) + path && revalidatePath(path) + tags?.split(",").forEach((tag) => revalidateTag(tag)) return new Response("Revalidated.") } catch (error) { diff --git a/starters/graphql-starter/app/api/revalidate/route.ts b/starters/graphql-starter/app/api/revalidate/route.ts index 9882273af..2a7e4ec0f 100644 --- a/starters/graphql-starter/app/api/revalidate/route.ts +++ b/starters/graphql-starter/app/api/revalidate/route.ts @@ -1,9 +1,10 @@ -import { revalidatePath } from "next/cache" +import { revalidatePath, revalidateTag } from "next/cache" import type { NextRequest } from "next/server" async function handler(request: NextRequest) { const searchParams = request.nextUrl.searchParams const path = searchParams.get("path") + const tags = searchParams.get("tags") const secret = searchParams.get("secret") // Validate secret. @@ -11,13 +12,14 @@ async function handler(request: NextRequest) { return new Response("Invalid secret.", { status: 401 }) } - // Validate path. - if (!path) { - return new Response("Invalid path.", { status: 400 }) + // Either tags or path must be provided. + if (!path && !tags) { + return new Response("Missing path or tags.", { status: 400 }) } try { - revalidatePath(path) + path && revalidatePath(path) + tags?.split(",").forEach((tag) => revalidateTag(tag)) return new Response("Revalidated.") } catch (error) {