diff --git a/src/app/_constants/environment.ts b/src/app/_constants/environment.ts index cf9cc09..881ccca 100644 --- a/src/app/_constants/environment.ts +++ b/src/app/_constants/environment.ts @@ -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; \ No newline at end of file diff --git a/src/app/api/revalidate/route.ts b/src/app/api/revalidate/route.ts new file mode 100644 index 0000000..8e56771 --- /dev/null +++ b/src/app/api/revalidate/route.ts @@ -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() }); +}