You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

PWA应用文件缓存时长、永久化、最大缓存及误删情况咨询

PWA Caching: Answers to Your Key Questions

Hey there! Let's break down all your PWA caching questions clearly—this stuff can get a bit nuanced, so I'll keep it straightforward:

1. Cache Duration for PWA Files

There’s no fixed "default" duration for cached PWA files—it’s entirely controlled by your Service Worker’s caching strategy and HTTP headers like Cache-Control.

  • If you use strategies like CacheFirst, files will stay in the cache indefinitely until you explicitly update the cache (e.g., by changing the cache name/version in your Service Worker) or the browser clears it.
  • You can set expiration rules manually too: for example, you could add timestamps to cache keys, or periodically check for new versions of assets and purge old ones in your Service Worker code.
  • HTTP headers like Cache-Control: max-age=86400 tell the browser how long to consider a file "fresh," but Service Workers can override this if you’ve configured them to prioritize cached assets.

2. Maximum Cache Capacity

There’s no universal limit, as it depends on two main factors:

  • Browser implementation: Most modern browsers (Chrome, Firefox, Edge) allocate a portion of the device’s total storage to web apps. For example, Chrome might limit a single site to tens or hundreds of MB, but this scales with the device’s available disk space.
  • Device storage: If the device is low on space, browsers will trigger "garbage collection" using an LRU (Least Recently Used) policy to delete older, less frequently accessed cached data.
  • You can request persistent storage (via navigator.storage.persist()) which asks the user for permission to keep cached data even when storage is low—this makes your cache more resilient, but it’s not a guarantee of infinite space.

3. Can Cached Files Be Saved Permanently?

Short answer: No, not by default. Even with persistent storage, there are scenarios where cached files can be deleted:

  • If the user manually clears their browser’s cache or site-specific data, all PWA cached files will be wiped.
  • If the user uninstalls the PWA (from their home screen/desktop), the associated cache is automatically removed.
  • In rare cases, browser updates or system-level storage cleanup might also clear cached data, though this is less common.

Persistent storage helps prevent automatic browser cleanup due to low space, but it doesn’t override user-initiated actions.

4. Will Cached Files Be Accidentally Cleared?

Yes, here are the most common scenarios:

  • User-initiated cache cleanup: When a user clears their browser’s cache (e.g., "Clear browsing data" in Chrome), PWA Cache Storage is included in this cleanup—no exceptions.
  • Low device storage: As mentioned earlier, browsers will automatically delete cached data from less frequently used sites when disk space is running low.
  • Browser settings changes: If a user revokes storage permissions for your PWA, the cache may be cleared.

The best way to mitigate this is to implement robust cache fallback logic (e.g., fall back to the network if a cached asset is missing) and inform users about offline functionality so they understand the value of keeping cache data intact.


内容的提问来源于stack exchange,提问作者Temp O'rary

火山引擎 最新活动