Skip to content

Commit 36387ff

Browse files
authored
Add tag revalidation to basic-starter and graphql-starter (#820)
1 parent bac0ee8 commit 36387ff

File tree

2 files changed

+14
-10
lines changed
  • starters
    • basic-starter/app/api/revalidate
    • graphql-starter/app/api/revalidate

2 files changed

+14
-10
lines changed

starters/basic-starter/app/api/revalidate/route.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
import { revalidatePath } from "next/cache"
1+
import { revalidatePath, revalidateTag } from "next/cache"
22
import type { NextRequest } from "next/server"
33

44
async function handler(request: NextRequest) {
55
const searchParams = request.nextUrl.searchParams
66
const path = searchParams.get("path")
7+
const tags = searchParams.get("tags")
78
const secret = searchParams.get("secret")
89

910
// Validate secret.
1011
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
1112
return new Response("Invalid secret.", { status: 401 })
1213
}
1314

14-
// Validate path.
15-
if (!path) {
16-
return new Response("Invalid path.", { status: 400 })
15+
// Either tags or path must be provided.
16+
if (!path && !tags) {
17+
return new Response("Missing path or tags.", { status: 400 })
1718
}
1819

1920
try {
20-
revalidatePath(path)
21+
path && revalidatePath(path)
22+
tags?.split(",").forEach((tag) => revalidateTag(tag))
2123

2224
return new Response("Revalidated.")
2325
} catch (error) {

starters/graphql-starter/app/api/revalidate/route.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
import { revalidatePath } from "next/cache"
1+
import { revalidatePath, revalidateTag } from "next/cache"
22
import type { NextRequest } from "next/server"
33

44
async function handler(request: NextRequest) {
55
const searchParams = request.nextUrl.searchParams
66
const path = searchParams.get("path")
7+
const tags = searchParams.get("tags")
78
const secret = searchParams.get("secret")
89

910
// Validate secret.
1011
if (secret !== process.env.DRUPAL_REVALIDATE_SECRET) {
1112
return new Response("Invalid secret.", { status: 401 })
1213
}
1314

14-
// Validate path.
15-
if (!path) {
16-
return new Response("Invalid path.", { status: 400 })
15+
// Either tags or path must be provided.
16+
if (!path && !tags) {
17+
return new Response("Missing path or tags.", { status: 400 })
1718
}
1819

1920
try {
20-
revalidatePath(path)
21+
path && revalidatePath(path)
22+
tags?.split(",").forEach((tag) => revalidateTag(tag))
2123

2224
return new Response("Revalidated.")
2325
} catch (error) {

0 commit comments

Comments
 (0)