import { revalidatePath, revalidateTag } from "next/cache"; import { NextResponse } from "next/server"; import { PUBLIC_FACILITIES_CACHE_TAG } from "@/app/publicFacilities"; import { SITE_PAGE_CACHE_TAG } from "@/app/sitePages"; import { SITE_PAGE_SEO_CACHE_TAG } from "@/app/pageSeo"; export const runtime = "nodejs"; type RevalidatePayload = { includeFacilities?: boolean; includePlacePages?: boolean; includeSitePages?: boolean; }; const FACILITY_PAGE_PATHS = ["/", "/golfbaner", "/medlemskap", "/vtg"]; const SITE_PAGE_PATHS = ["/golfbaner", "/vtg", "/medlemskap", "/banebesok", "/meninger", "/simulatorer"]; function getExpectedSecret(): string { return String(process.env.PUBLIC_SESSION_SECRET || "").trim(); } function isAuthorized(request: Request): boolean { const expectedSecret = getExpectedSecret(); if (!expectedSecret) { return false; } const suppliedSecret = String(request.headers.get("x-teeoff-revalidate-secret") || "").trim(); return suppliedSecret === expectedSecret; } function revalidateFacilityPages(includePlacePages: boolean): void { revalidateTag(PUBLIC_FACILITIES_CACHE_TAG, "max"); for (const path of FACILITY_PAGE_PATHS) { revalidatePath(path); } revalidatePath("/golfbaner/[slug]", "page"); revalidatePath("/sted/[slug]", "page"); if (includePlacePages) { revalidatePath("/sted/[slug]", "page"); } } function revalidateSitePageContent(): void { revalidateTag(SITE_PAGE_CACHE_TAG, "max"); revalidateTag(SITE_PAGE_SEO_CACHE_TAG, "max"); for (const path of SITE_PAGE_PATHS) { revalidatePath(path); } } function revalidatePlacePagesOnly(): void { revalidatePath("/sted/[slug]", "page"); } export async function POST(request: Request) { if (!isAuthorized(request)) { return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); } let payload: RevalidatePayload = {}; try { payload = (await request.json()) as RevalidatePayload; } catch { payload = {}; } const includeFacilities = payload.includeFacilities !== false; const includePlacePages = payload.includePlacePages === true; const includeSitePages = payload.includeSitePages === true; if (includeFacilities) { revalidateFacilityPages(includePlacePages); } else if (includePlacePages) { revalidatePlacePagesOnly(); } if (includeSitePages) { revalidateSitePageContent(); } return NextResponse.json({ revalidated: true, includeFacilities, includePlacePages, includeSitePages, }); }