Skip to content

🎥 Make videos programmatically with React

Elements: Purge preview cache after upload (#10628)

Igor Samokhovets988a8e291f1b
diff --git a/packages/docs/upload-element-preview.ts b/packages/docs/upload-element-preview.ts
index 694fc11e63..87fec2ecc8 100644
--- a/packages/docs/upload-element-preview.ts
+++ b/packages/docs/upload-element-preview.ts
@@ -162,6 +162,12 @@ if (!Bun.env.AWS_ACCESS_KEY_ID || !Bun.env.AWS_SECRET_ACCESS_KEY) {
);
}
+if (!Bun.env.CLOUDFLARE_API_TOKEN || !Bun.env.CLOUDFLARE_ZONE_ID) {
+ throw new Error(
+ 'Clearing the CDN cache requires CLOUDFLARE_API_TOKEN and CLOUDFLARE_ZONE_ID in packages/docs/.env.',
+ );
+}
+
const client = new S3Client({
accessKeyId: Bun.env.AWS_ACCESS_KEY_ID,
secretAccessKey: Bun.env.AWS_SECRET_ACCESS_KEY,
@@ -199,6 +205,30 @@ for (const asset of assets) {
console.log(`Uploaded ${publicUrl} (${file.size} bytes)`);
}
+const purgeResponse = await fetch(
+ `https://api.cloudflare.com/client/v4/zones/${Bun.env.CLOUDFLARE_ZONE_ID}/purge_cache`,
+ {
+ method: 'POST',
+ headers: {
+ Authorization: `Bearer ${Bun.env.CLOUDFLARE_API_TOKEN}`,
+ 'Content-Type': 'application/json',
+ },
+ body: JSON.stringify({
+ files: assets.map((asset) => `https://remotion.media${asset.localUrl}`),
+ }),
+ },
+);
+const purgeResult = (await purgeResponse.json()) as {
+ success: boolean;
+ errors?: unknown;
+};
+if (!purgeResponse.ok || !purgeResult.success) {
+ throw new Error(
+ `Could not clear the CDN cache: HTTP ${purgeResponse.status} ${JSON.stringify(purgeResult.errors)}`,
+ );
+}
+
+console.log('Cleared the CDN cache for both preview URLs.');
console.log('Upload verified. Replace the review URLs with:');
console.log(`https://remotion.media${expectedPosterUrl}`);
console.log(`https://remotion.media${expectedVideoUrl}`);