Skip to content

Commit 5a894f7

Browse files
authored
Temporary fix for full cache storage (codesandbox#713)
* Fix QuotaExceeded on service worker * Add estimator * Better clearing mechanism
1 parent b186b7f commit 5a894f7

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

packages/common/registerServiceWorker.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,35 @@ function registerValidSW(swUrl, sendNotification) {
7373
);
7474
}
7575
}
76+
} else if (installingWorker.state === 'redundant') {
77+
if ('storage' in navigator && 'estimate' in navigator.storage) {
78+
navigator.storage.estimate().then(results => {
79+
const percentUsed = results.usage / results.quota * 100;
80+
// Let's assume that if we're using 95% of our quota, then this failure
81+
// was due to quota exceeded errors.
82+
// TODO: Hardcoding a threshold stinks.
83+
if (percentUsed >= 0.95) {
84+
// Get rid of the existing SW so that we're not stuck
85+
// with the previously cached content.
86+
registration.unregister();
87+
88+
// Let's assume that we have some way of doing this without inadvertantly
89+
// blowing away storage being used on the origin by something other than
90+
// our service worker.
91+
// I don't think that the Clear-Site-Data: header helps here, unfortunately.
92+
self.caches.keys().then(names => {
93+
names.forEach(name => {
94+
self.caches.delete(name);
95+
});
96+
});
97+
98+
// TODO clear indexeddb
99+
}
100+
});
101+
} else {
102+
// What about browsers that don't support navigator.storage.estimate()?
103+
// There's no way of guessing why the service worker is redundant.
104+
}
76105
}
77106
};
78107
};

0 commit comments

Comments
 (0)