Skip to content

Commit

Permalink
fix: remove sw cache storage entries when using self destroying option (
Browse files Browse the repository at this point in the history
  • Loading branch information
userquin committed Nov 25, 2023
1 parent 194fb59 commit df8b4b0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vite-plugin-pwa",
"type": "module",
"version": "0.17.0",
"packageManager": "pnpm@8.10.5",
"packageManager": "pnpm@8.11.0",
"description": "Zero-config PWA for Vite",
"author": "antfu <anthonyfu117@hotmail.com>",
"license": "MIT",
Expand Down
23 changes: 17 additions & 6 deletions src/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,27 @@ export async function generateRegisterSW(options: ResolvedVitePWAOptions, mode:
export async function generateServiceWorker(options: ResolvedVitePWAOptions, viteOptions: ResolvedConfig): Promise<BuildResult> {
if (options.selfDestroying) {
const selfDestroyingSW = `
self.addEventListener('install', function(e) {
self.addEventListener('install', (e) => {
self.skipWaiting();
});
self.addEventListener('activate', function(e) {
self.addEventListener('activate', (e) => {
self.registration.unregister()
.then(function() {
return self.clients.matchAll();
.then(() => self.clients.matchAll())
.then((clients) => {
clients.forEach((client) => {
if (client instanceof WindowClient)
client.navigate(client.url);
});
return Promise.resolve();
})
.then(function(clients) {
clients.forEach(client => client.navigate(client.url))
.then(() => {
self.caches.keys().then((cacheNames) => {
Promise.all(
cacheNames.map((cacheName) => {
return self.caches.delete(cacheName);
}),
);
})
});
});
`
Expand Down

0 comments on commit df8b4b0

Please sign in to comment.