58 lines
1.2 KiB
TypeScript
58 lines
1.2 KiB
TypeScript
import { env } from "~/env.mjs";
|
|
|
|
export const revalidate = async (path: string) => {
|
|
try {
|
|
const res = await fetch(
|
|
`${env.INTERNAL_BASE_URL}/api/revalidation?secret=${env.REVALIDATION_SECRET}&path=${encodeURIComponent(path)}`,
|
|
{
|
|
method: "GET",
|
|
},
|
|
);
|
|
if (!res.ok) {
|
|
const errorData = (await res.json()) as {
|
|
message: string;
|
|
success: boolean;
|
|
};
|
|
throw new Error(
|
|
`Failed to revalidate path ${path}: ${errorData.message}`,
|
|
);
|
|
}
|
|
} catch (error) {
|
|
console.error(`Error revalidating path ${path}:`, error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const revalidateMultiple = async (
|
|
paths: string[],
|
|
): Promise<{
|
|
success: string[];
|
|
failed: Array<{ path: string; error: string }>;
|
|
}> => {
|
|
const results = await Promise.allSettled(
|
|
paths.map((path) => revalidate(path)),
|
|
);
|
|
|
|
const success: string[] = [];
|
|
const failed: Array<{ path: string; error: string }> = [];
|
|
|
|
results.forEach((result, index) => {
|
|
const path = paths[index];
|
|
if (!path) {
|
|
return;
|
|
}
|
|
if (result.status === "fulfilled") {
|
|
success.push(path);
|
|
} else {
|
|
failed.push({
|
|
path,
|
|
error:
|
|
result.reason instanceof Error
|
|
? result.reason.message
|
|
: "Unknown error",
|
|
});
|
|
}
|
|
});
|
|
|
|
return { success, failed };
|
|
};
|