Reputation: 3816
I use workbox generateSW method to generate service worker and this is the config I use :
const workbox = require("workbox-build");
workbox.generateSW({
globDirectory: "./",
globIgnores: ["node_modules/**", "**/generator.js", "**/sw.js"],
globPatterns: ["**/*.{css,js}"],
swDest: "./sw.js",
sourcemap: false,
cleanupOutdatedCaches: true,
clientsClaim: true,
runtimeCaching: [
{
urlPattern: /\.(?:html|htm|xml)$/,
handler: "StaleWhileRevalidate",
options: {
cacheName: "runtimecaching name",
expiration: {
maxAgeSeconds: 1,
},
},
},
],
});
I couldn't find an expiration time to delete the old cache in the docs , so How can I cleanup the cache after sometime with workbox-build generateSW ?
Upvotes: 2
Views: 1125
Reputation: 2815
maxAgeSeconds
is not meant to auto cleanup old entries after some time. From the documentation:
The plugin will check and remove entries after each request or cache update.
One thing to note:
- Because it’s slow to open IndexedDB, expiration won’t occur until after the request is used. This means that an expired request may be used once, but will be expired after that.
- To alleviate this, the plugin will check the "Date" header of the cached response, if one exists and the date can be parsed, it’ll expire based on this as it doesn’t require an IndexedDB lookup.
So maxAgeSeconds
is the option to control time before the cached request is stale, and the old entry removal is triggered by the request.
To check and remove old entries after any request, use maxEntries
option:
After a cached response is used or a new request is added to the cache, the plugin will look at the configured cache and ensure that the number of cached entries doesn’t exceed the limit. If it does, the oldest entries will be removed.
If you still need to clean after any request and based on time instead of number of entries, you would need to create your own plugin based on ExpirationPlugin
and better use inject manifest mode.
Upvotes: 1