feat(api): POST /api/cache/clear + flush images on asset webhooks
Two changes: - New POST /api/cache/clear (auth: REVALIDATE_TOKEN). Wipes the TTL content cache, the image disk + memory cache, and forwards to the RustyCMS /api/transform/cache/invalidate endpoint when an API key is configured. Single trigger to flush everything. - /api/revalidate now also calls clearImageCache() on asset.* events so a re-upload (focal change, alt edit, replacement) doesn't keep serving the previous transformed image from disk. clearImageCache() in image-cache.server.ts wipes the in-memory map and removes every file under IMAGE_CACHE_DIR.
This commit is contained in:
@@ -218,3 +218,27 @@ export function resolveExtension(params: ImageTransformParams, contentType?: str
|
||||
if (contentType) return extForContentType(contentType);
|
||||
return 'jpg';
|
||||
}
|
||||
|
||||
/**
|
||||
* Wipe in-memory + on-disk image cache. Returns counts cleared.
|
||||
*/
|
||||
export async function clearImageCache(): Promise<{ memory: number; disk: number }> {
|
||||
const memory = memoryCache.size;
|
||||
memoryCache.clear();
|
||||
let disk = 0;
|
||||
try {
|
||||
const dir = ensureCacheDir();
|
||||
const files = await readdir(dir);
|
||||
for (const f of files) {
|
||||
try {
|
||||
await unlink(join(dir, f));
|
||||
disk++;
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
/* ignore */
|
||||
}
|
||||
return { memory, disk };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user