Include core assets in serviceworker cache (#11559)

This commit is contained in:
Ben Halpern 2020-11-24 08:58:06 -05:00 committed by GitHub
parent e063605600
commit 575449b890
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -137,6 +137,22 @@
return;
}
// Fetch and/or JS and CSS assets for better persistence than memory cache.
if (url.pathname.startsWith("/assets/") || url.pathname.startsWith("/packs/")) {
event.respondWith(
caches.open(staticCacheName).then(function (cache) {
return cache.match(event.request).then(function (response) {
return (
response ||
fetch(event.request).then(function (response) {
cache.put(event.request, response.clone());
return response;
})
);
});
})
);
}
if (url.origin === location.origin) {
if (event.clientId === "" && // Not fetched via AJAX after page load.
event.request.method == "GET" && // Don't fetch on POST, DELETE, etc.
@ -187,6 +203,22 @@
"/shell_bottom",
]));
}
// Periodically delete assets when they pile up
if (Math.random() > 0.97) {
caches.open(staticCacheName).then(function(cache) {
cache.keys().then(function(keys) {
if (keys.length > 100) {
keys.forEach(function(r) {
if (r.url.includes("/assets/") || r.url.includes("/packs/")) {
cache.delete(r);
};
});
}
});
});
}
}
});
<% end %>