Include core assets in serviceworker cache (#11559)
This commit is contained in:
parent
e063605600
commit
575449b890
1 changed files with 32 additions and 0 deletions
|
|
@ -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 %>
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue