Skip to content

A platform for community discussion. Free, open, simple.

FIX: Ensure service-worker cache is cleaned correctly (#23204)

David Taylor61527f5eaf03
diff --git a/app/assets/javascripts/service-worker.js.erb b/app/assets/javascripts/service-worker.js.erb
index 08a5a120337..57207d30d0c 100644
--- a/app/assets/javascripts/service-worker.js.erb
+++ b/app/assets/javascripts/service-worker.js.erb
@@ -23,10 +23,25 @@ var authUrls = ["auth", "session/sso_login", "session/sso", "srv/status"].map(pa
var chatRegex = /\/chat\/channel\/(\d+)\//;
var inlineReplyIcon = "<%= UrlHelper.absolute("/images/push-notifications/inline_reply.png") %>";
-var cacheVersion = "1";
+const oldCacheNames = [
+ "discourse-1", "external-1"
+]
+
+oldCacheNames.forEach(cacheName => caches.delete(cacheName))
+
+var cacheVersion = "2";
var discourseCacheName = "discourse-" + cacheVersion;
var externalCacheName = "external-" + cacheVersion;
+const expirationOptions = {
+ maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
+ maxEntries: 250,
+ purgeOnQuotaError: true, // safe to automatically delete if exceeding the available storage
+ matchOptions: {
+ ignoreVary: true // Many discourse responses include `Vary` header. This option is required to ensure they are cleaned up correctly.
+ }
+}
+
// Chrome 97 shipped with broken samesite cookie handling when proxying requests through service workers
// https://bugs.chromium.org/p/chromium/issues/detail?id=1286367
var chromeVersionMatch = navigator.userAgent.match(/Chrome\/97.0.(\d+)/);
@@ -44,11 +59,7 @@ workbox.routing.registerRoute(
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [200] // opaque responses will return status code '0'
}), // for s3 secure uploads signed urls
- new workbox.expiration.ExpirationPlugin({
- maxAgeSeconds: 7* 24 * 60 * 60, // 7 days
- maxEntries: 250,
- purgeOnQuotaError: true, // safe to automatically delete if exceeding the available storage
- }),
+ new workbox.expiration.ExpirationPlugin(expirationOptions),
],
})
);
@@ -96,11 +107,7 @@ if (cdnUrls.length > 0) {
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [200] // opaque responses will return status code '0'
}),
- new workbox.expiration.ExpirationPlugin({
- maxAgeSeconds: 7* 24 * 60 * 60, // 7 days
- maxEntries: 250,
- purgeOnQuotaError: true, // safe to automatically delete if exceeding the available storage
- }),
+ new workbox.expiration.ExpirationPlugin(expirationOptions),
appendQueryStringPlugin
],
})
@@ -127,11 +134,7 @@ workbox.routing.registerRoute(
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [200] // opaque responses will return status code '0'
}),
- new workbox.expiration.ExpirationPlugin({
- maxAgeSeconds: 7* 24 * 60 * 60, // 7 days
- maxEntries: 250,
- purgeOnQuotaError: true, // safe to automatically delete if exceeding the available storage
- }),
+ new workbox.expiration.ExpirationPlugin(expirationOptions),
],
})
);