Tune caching: shorter revalidate, lighter fetches, on-demand purge.
Use paginated list fetches with stripped list payloads, recent-item helpers for carousels, fetch tags + POST /api/revalidate, and 5-minute page ISR instead of 1 hour. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
f341b010c0
commit
02c7dd51d2
@@ -0,0 +1,69 @@
|
||||
import { revalidatePath, revalidateTag } from "next/cache";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
type RevalidateBody = {
|
||||
/** Revalidate a fetch cache tag (portfolios, blogs, portfolio:slug, blog:slug, …). */
|
||||
tag?: string;
|
||||
/** Revalidate a path (/portfolios/foo, /, …). */
|
||||
path?: string;
|
||||
/** Shorthand: portfolios | blogs | all */
|
||||
scope?: "portfolios" | "blogs" | "all";
|
||||
};
|
||||
|
||||
/**
|
||||
* On-demand cache purge — call after CMS publish/update.
|
||||
*
|
||||
* POST /api/revalidate
|
||||
* Header: x-revalidate-secret: <REVALIDATE_SECRET>
|
||||
* Body (JSON, optional): { "scope": "all" } | { "tag": "portfolios" } | { "path": "/portfolios/item" }
|
||||
*
|
||||
* nginx may still serve cached HTML for up to its TTL (5m) unless purged separately.
|
||||
*/
|
||||
export async function POST(req: NextRequest) {
|
||||
const secret = process.env.REVALIDATE_SECRET;
|
||||
if (!secret) {
|
||||
return NextResponse.json({ ok: false, error: "REVALIDATE_SECRET not configured" }, { status: 503 });
|
||||
}
|
||||
|
||||
const provided =
|
||||
req.headers.get("x-revalidate-secret") ?? req.nextUrl.searchParams.get("secret");
|
||||
if (provided !== secret) {
|
||||
return NextResponse.json({ ok: false, error: "Unauthorized" }, { status: 401 });
|
||||
}
|
||||
|
||||
let body: RevalidateBody = {};
|
||||
try {
|
||||
body = (await req.json()) as RevalidateBody;
|
||||
} catch {
|
||||
body = {};
|
||||
}
|
||||
|
||||
const scope = body.scope ?? "all";
|
||||
const revalidated: string[] = [];
|
||||
|
||||
if (body.tag) {
|
||||
revalidateTag(body.tag);
|
||||
revalidated.push(`tag:${body.tag}`);
|
||||
} else if (body.path) {
|
||||
revalidatePath(body.path);
|
||||
revalidated.push(`path:${body.path}`);
|
||||
} else if (scope === "portfolios" || scope === "all") {
|
||||
revalidateTag("portfolios");
|
||||
revalidatePath("/");
|
||||
revalidatePath("/portfolios");
|
||||
revalidatePath("/aboutus");
|
||||
revalidatePath("/our-services");
|
||||
revalidated.push("scope:portfolios");
|
||||
}
|
||||
|
||||
if (scope === "blogs" || scope === "all") {
|
||||
revalidateTag("blogs");
|
||||
revalidateTag("blogs:news");
|
||||
revalidatePath("/news");
|
||||
revalidatePath("/blog");
|
||||
revalidatePath("/news-and-blog");
|
||||
revalidated.push("scope:blogs");
|
||||
}
|
||||
|
||||
return NextResponse.json({ ok: true, revalidated, at: new Date().toISOString() });
|
||||
}
|
||||
Reference in New Issue
Block a user