Skip to content

Commit

Permalink
improve: use API to revalidate blogs on publish
Browse files Browse the repository at this point in the history
Signed-off-by: james-a-morris <[email protected]>
  • Loading branch information
james-a-morris committed Oct 15, 2024
1 parent 27ac366 commit 9fc634b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app/_constants/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export const GOOGLE_ANALYTICS_TAG_ID = process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_
// Server side environment variables
export const CONTENTFUL_SPACE_ID = process.env.CONTENTFUL_SPACE_ID;
export const CONTENTFUL_ACCESS_TOKEN = process.env.CONTENTFUL_ACCESS_TOKEN;
export const CONTENTFUL_REVALIDATE_SECRET = process.env.CONTENTFUL_REVALIDATE_SECRET;
17 changes: 17 additions & 0 deletions src/app/api/revalidate/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { NextRequest, NextResponse } from "next/server";
import { revalidatePath } from "next/cache";
import { CONTENTFUL_REVALIDATE_SECRET } from "@/app/_constants";

export 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 });
}

// revalidate ALL data. for home page and the articles pages
revalidatePath("/blog", "layout");

return NextResponse.json({ revalidated: true, now: Date.now() });
}

0 comments on commit 9fc634b

Please sign in to comment.