清除 CDN 缓存
启用Smart CDN后,Supabase 存储在文件更新或删除时会自动使缓存失效。不过,有些情况下你可能需要手动清理特定对象或整个桶的CDN缓存。缓存清除API允许你立即排队所有CDN边缘节点的缓存内容失效。
🌐 With Smart CDN enabled, Supabase Storage automatically invalidates the cache when files are updated or deleted. However, there are scenarios where you may need to manually purge the CDN cache for specific objects or entire buckets. The cache purge API allows you to immediately queue cache content invalidation across all CDN edge nodes.
手动清除缓存在你需要确保更新尽快传播,或者想要为了调试而清理缓存时非常有用。一旦清除,下次对该对象的请求将直接从源服务器获取,并且CDN缓存会重新填充。
🌐 Manual cache purging is useful when you need to ensure that updates are propagated as soon as possible, or when you want to clear the cache for debugging purposes. Once purged, the next request for that object will be served from the origin server, and the CDN cache will be repopulated.
清理缓存需要 密钥。服务器会拒绝使用旧的匿名密钥或用户 JWT 发出的请求。切勿在客户端代码中暴露你的密钥。
🌐 Cache purging requires the secret key. The server rejects calls made with the legacy anon key or a user JWT. Never expose your secret key in client-side code.
CDN 缓存清理功能适用于 专业版及以上。
🌐 CDN cache purge is available for Pro Plan and above.
清除一个对象 #
🌐 Purge a single object
你可以通过提供对象的确切路径来清除特定文件的 CDN 缓存。这个操作不支持通配符或递归。你必须指定想要失效的文件的完整路径。
🌐 You can purge the CDN cache for a specific file by providing the exact path to the object. This operation does not support wildcards or recursion. You must specify the complete path of the file you want to invalidate.
1import { createClient } from '@supabase/supabase-js'23// Create Supabase client with secret key4const supabase = createClient('your_project_url', 'your_secret_key')56// Purge cache for a single object7async function purgeCachedObject() {8 const { data, error } = await supabase.storage9 .from('bucket_name')10 .purgeCache('folder_name/file_name.png')1112 if (error) {13 // Handle error14 } else {15 // Handle success16 console.log(data.message) // 'success'17 }18}清空整个桶 #
🌐 Purge an entire bucket
在需要使存储桶中所有缓存对象失效的情况下,你可以清除整个存储桶的缓存。当进行批量更新或对存储桶做重大更改时,这很有用。
🌐 For scenarios where you need to invalidate all cached objects in a bucket, you can purge the entire bucket's cache. This is useful when performing bulk updates or major changes to your storage bucket.
1import { createClient } from '@supabase/supabase-js'23// Create Supabase client with secret key4const supabase = createClient('your_project_url', 'your_secret_key')56// Purge cache for an entire bucket7async function purgeBucketCache() {8 const { data, error } = await supabase.storage.purgeBucketCache('bucket_name')910 if (error) {11 // Handle error12 } else {13 // Handle success14 console.log(data.message) // 'success'15 }16}缓存传播 #
🌐 Cache propagation
清理缓存后,最多可能需要 60 秒,无效信息才能传播到全球所有 CDN 边缘节点。在此期间,根据用户被分配到的边缘节点,有些用户可能仍会收到缓存内容。
🌐 After purging the cache, it can take up to 60 seconds for the invalidation to propagate across all CDN edge nodes worldwide. During this time, some users may still receive cached content depending on which edge node they are routed to.
请注意,清除 CDN 缓存不会影响浏览器缓存。如果用户在浏览器中本地缓存了该资源,他们将继续看到缓存的版本,直到浏览器缓存根据上传时设置的 cacheControl 值过期。
🌐 Keep in mind that purging the CDN cache does not affect browser caches. If users have the asset cached locally in their browser, they will continue to see the cached version until the browser cache expires based on the cacheControl value set during upload.