'How long does a service worker keeps its cache?
I just got into Service Workers and made a simple setup for my site.
The documentation is quite good, but I wasn't able to find anywhere how much a service workers keeps the caches? I'm doing cache busting for my js and css assets (something like bundle.[hash].js) and I'm not sure if I should make sure to clean up the old assets from the cache manually or will they just expire in some time?
Solution 1:[1]
Did you check the documentation?
http://www.html5rocks.com/en/tutorials/service-worker/introduction/#toc-how
When we're installed, the activation step will follow and this is a great opportunity for handling any management of old caches, which we'll cover during the service worker update section.
The page I linked to explains "How to Cache and Return Requests"
self.addEventListener('activate', function(event) {
var cacheWhitelist = ['pages-cache-v1', 'blog-posts-cache-v1'];
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
Solution 2:[2]
You can set the cache age by controlling cache-controller :
public
Expires: (sometime in the future, according session.cache_expire)
Cache-Control: public, max-age=(sometime in the future, according to session.cache_expire)
Last-Modified: (the timestamp of when the session was last saved)
and also,
<?php
header( 'Cache-Control: max-age=604800' );
/* now get and send images */
?>
see Here : enter link description here
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zerohero |
| Solution 2 | Mua Aye |

