Skip to content

Commit

Permalink
Add tag revalidation to basic-starter and graphql-starter (#820)
Browse files Browse the repository at this point in the history
  • Loading branch information
robdecker authored Nov 18, 2024
1 parent bac0ee8 commit 36387ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
12 changes: 7 additions & 5 deletions starters/basic-starter/app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
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.
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
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) {
Expand Down
12 changes: 7 additions & 5 deletions starters/graphql-starter/app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
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.
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
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) {
Expand Down

0 comments on commit 36387ff

Please sign in to comment.