如何在清理缓存后恢复 Blazor WASM "offline-cache"?

How to reinstate the Blazor WASM "offline-cache" after a cleanup of the caches?

基本上我有一个升级流程,在 localStorage 和 IndexedDB 清理之间我还执行缓存清理:

window.clearCache = async () => {
    const cachesNames = await caches.keys()
    for (let i = 0; i < cachesNames.length; i++)
        await caches.delete(cachesNames[i])
}

然后我执行 Navigation.NavigateTo(Navigation.Uri, forceLoad: true); 强制重新加载网站。

重新加载后,我遇到了这种情况,通常的 blazor-resources-/ 如果自动填充了所有资源,同时 offline-cache-<random> 仍然是空的。

有没有办法再次调用那些缓存这些资源的步骤?

恢复离线WASM缓存,请采取不同的策略。我会 推荐 不同的策略并强制重新加载缓存,使用 CACHE_VERSION & .

Step 1: Create a new js file

Step 2: register it in navigator.serviceWorker object.

例如yourCustomCacheTriggerReloader.js 文件应该有一个像

这样的变量
 // use this variable to update and trigger your forced upload
 // to the client after you delete you files.
 `const CACHE_VERSION = 1.0`. 

Step 3: when you delete, in your script/or manually, please update CACHE_VERSION value. That will tell/force the browser/client to reload the latest files.


示例来自 here

// In service-worker.published.js
const CACHE_VERSION = '2021.05.22.001'  // Increment each time before deployment.
const cacheNamePrefix = 'offline-cache-';
const cacheName = `${cacheNamePrefix}${CACHE_VERSION}`;

//Note: The original one does not work. Replace 
//self.assetsManifest.version withCACHE_VERSION
const cacheName = ${cacheNamePrefix}${self.assetsManifest.version};