From 36387ff7a3667205df5d1c0981e5e15b5529c474 Mon Sep 17 00:00:00 2001 From: Rob Decker Date: Mon, 18 Nov 2024 14:11:21 -0700 Subject: [PATCH] Add tag revalidation to basic-starter and graphql-starter (#820) --- 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) {