-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve: use API to revalidate blogs on publish (#82)
* improve: use API to revalidate blogs on publish Signed-off-by: james-a-morris <[email protected]> * nit: EOL * improve: specific page Signed-off-by: james-a-morris <[email protected]> --------- Signed-off-by: james-a-morris <[email protected]>
- Loading branch information
1 parent
a860428
commit 7467472
Showing
3 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import { revalidatePath } from "next/cache"; | ||
import { CONTENTFUL_REVALIDATE_SECRET } from "@/app/_constants"; | ||
|
||
export async function POST(request: NextRequest) { | ||
const requestHeaders = new Headers(request.headers); | ||
const secret = requestHeaders.get("x-vercel-reval-key"); | ||
|
||
if (secret !== CONTENTFUL_REVALIDATE_SECRET) { | ||
return NextResponse.json({ message: "Invalid secret" }, { status: 401 }); | ||
} | ||
|
||
const body = await request.json(); | ||
const slug = body?.fields?.slug?.["en-US"]; | ||
|
||
if (typeof slug !== "string") { | ||
return NextResponse.json({ message: "Invalid request body" }, { status: 400 }); | ||
} | ||
|
||
// Revalidate blog post and all related pages | ||
revalidatePath(`/blog/${slug}`); | ||
// Revalidate home page | ||
revalidatePath("/blog"); | ||
|
||
return NextResponse.json({ revalidated: true, slug, now: Date.now() }); | ||
} |