fix(cache): revalidate storefront after admin mutations
Deploy Canina / deploy (push) Successful in 2m35s
Deploy Canina / deploy (push) Successful in 2m35s
This commit is contained in:
1 parent
2e9a0e30c1
commit
ff6f04fe46
7 files changed
+205
-49
No files matched your search
@@ -12,69 +12,41 @@ export async function POST(request: NextRequest) {
|
||||
async function handleRevalidate(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
let secret = searchParams.get('secret') || request.headers.get('x-revalidate-secret');
|
||||
let tag = searchParams.get('tag');
|
||||
let path = searchParams.get('path');
|
||||
let tags: unknown[] = searchParams.getAll('tag');
|
||||
let paths: unknown[] = searchParams.getAll('path');
|
||||
|
||||
// Check auth header if bearer token provided
|
||||
const authHeader = request.headers.get('authorization');
|
||||
if (authHeader && authHeader.startsWith('Bearer ')) {
|
||||
secret = authHeader.substring(7);
|
||||
}
|
||||
if (authHeader?.startsWith('Bearer ')) secret = authHeader.substring(7);
|
||||
|
||||
if (request.method === 'POST') {
|
||||
try {
|
||||
const body = await request.json().catch(() => ({}));
|
||||
if (body && typeof body === 'object') {
|
||||
if (body.secret) secret = body.secret;
|
||||
if (body.tag) tag = body.tag;
|
||||
if (body.path) path = body.path;
|
||||
}
|
||||
} catch {
|
||||
// Ignore body read error
|
||||
const body = await request.json().catch(() => ({}));
|
||||
if (body && typeof body === 'object') {
|
||||
if (body.secret) secret = body.secret;
|
||||
if (body.tag) tags = [body.tag];
|
||||
if (Array.isArray(body.tags)) tags = body.tags;
|
||||
if (body.path) paths = [body.path];
|
||||
if (Array.isArray(body.paths)) paths = body.paths;
|
||||
}
|
||||
}
|
||||
|
||||
const expectedSecret = process.env.REVALIDATION_SECRET;
|
||||
if (!expectedSecret) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Revalidation service is not configured' },
|
||||
{ status: 503 }
|
||||
);
|
||||
return NextResponse.json({ success: false, message: 'Revalidation service is not configured' }, { status: 503 });
|
||||
}
|
||||
|
||||
if (!secret || secret !== expectedSecret) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Invalid revalidation secret token' },
|
||||
{ status: 401 }
|
||||
);
|
||||
return NextResponse.json({ success: false, message: 'Invalid revalidation secret token' }, { status: 401 });
|
||||
}
|
||||
|
||||
if (!tag && !path) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Missing tag or path parameter for cache revalidation' },
|
||||
{ status: 400 }
|
||||
);
|
||||
const cleanTags = [...new Set(tags.filter((value): value is string => typeof value === 'string' && value.length > 0))];
|
||||
const cleanPaths = [...new Set(paths.filter((value): value is string => typeof value === 'string' && value.startsWith('/')))];
|
||||
if (!cleanTags.length && !cleanPaths.length) {
|
||||
return NextResponse.json({ success: false, message: 'Missing tag or path parameter for cache revalidation' }, { status: 400 });
|
||||
}
|
||||
|
||||
const revalidated: { tag?: string; path?: string } = {};
|
||||
|
||||
if (tag) {
|
||||
revalidateTag(tag, 'default');
|
||||
revalidated.tag = tag;
|
||||
for (const tag of cleanTags) revalidateTag(tag, 'default');
|
||||
for (const path of cleanPaths) {
|
||||
try { revalidatePath(path, 'page'); } catch { revalidatePath(path); }
|
||||
}
|
||||
|
||||
if (path) {
|
||||
try {
|
||||
revalidatePath(path, 'page');
|
||||
} catch {
|
||||
revalidatePath(path);
|
||||
}
|
||||
revalidated.path = path;
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
revalidated: true,
|
||||
...revalidated,
|
||||
now: Date.now(),
|
||||
});
|
||||
return NextResponse.json({ revalidated: true, tags: cleanTags, paths: cleanPaths, now: Date.now() });
|
||||
}
|
||||
Reference in new issue
Block a user