-
Notifications
You must be signed in to change notification settings - Fork 822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add helper method to cleanup cache + expiration data in IDB #1365
Comments
This all sounds like a good idea to me. |
This is how I achieved it: (Note I am on Workbox 2.1.3) const scope = self.registration.scope;
const cacheKeys = await self.caches.keys()
const workboxPrecachedAssetsCacheKeyRegExp = new RegExp(`^${cacheId}-workbox-precaching-revisioned`);
const runtimeCacheData = cacheKeys.filter(key => !workboxPrecachedAssetsCacheKeyRegExp.test(key));
await Promise.all(runtimeCacheData.map(cacheKey => caches.delete(cacheKey)));
const idbDeletePromises = Promise.all(runtimeCacheData.map(cacheKey => new Promise((resolve, reject) => {
const dbKey = `workbox-cache-expiration-${scope}-${cacheKey}`;
const request = indexedDB.deleteDatabase(dbKey);
request.onerror = reject;
request.onsuccess = resolve
})));
await idbDeletePromises; |
Does workbox close connections to indexedDB? I am having issues where my The suggestions online recommend closing all open IndexedDB requests before calling Thanks! |
I was able to (hackily) close all connections via monkey-patching the Then before deleting indexedDBs, I close all open connections, then do Code is like this: // Taken from : https://gist.github.com/cdegit/22a2aa0cba36171d0c6a023a40127f1c
const overrideFunction = function(object: any, key: any, override: any) {
let originalFn: any;
const applyOverride = function() {
// Use call to make sure the this is correct
return override.call(this, originalFn, arguments);
};
// If the function already exists,
// grab a reference to it before we override its getter
if (typeof object[key] === 'function') {
originalFn = object[key];
}
try {
Object.defineProperty(object, key, {
get: function() {
if (originalFn) {
// Capture the arguments and pass them to the override
return applyOverride;
}
},
set: function(fn) {
originalFn = fn;
}
});
} catch (e) {
console.log('Unable to override function');
console.log(e);
}
};
const connections: Array<IDBOpenDBRequest> = [];
overrideFunction(indexedDB, 'open', function(originalFn: any, originalArguments: any) {
const result = originalFn.apply(this, originalArguments);
connections.push(result);
console.log(connections);
return result;
});
// .. later
function onLogout() {
connections.forEach(connection => connection.result.close());
connections.length = 0;
clearCachesAndIndexedDBs();
} |
No, Workbox deliberately keeps connections to IDB open while the SW remains running, which unfortunately necessitates the types of hacks that you put together. The "official" method for cleaning up the IDB + cache storage pair will have to take that into account. |
Thanks Jeff, looking forward to an official implementation 👍 |
Awesome! looking forward to hooking this up! |
Library Affected:
workbox-cache-expiration
Issue or Feature Request Description:
Extracting a feature request from #1354 (comment), @josephliccini and I write:
The text was updated successfully, but these errors were encountered: