LoginSignup
38
31

More than 5 years have passed since last update.

閲覧しているサイトのServiceWorkerの登録削除とCache APIのキャッシュを全て削除するJSコード

Posted at

この記事の内容

  • ServiceWorker・Cache APIとは

ServiceWorkerとCache APIを使ってオフラインでも動くWebアプリを作る
http://qiita.com/horo/items/175c8fd7513138308930

JavaScriptの処理で自在にキャッシュ削除とかが行えたら良いなということでメモとして記載します。

コード

下記をブラウザの開発ツールのコンソールより実行することで
ServiceWorkerの登録削除とCache APIのキャッシュを全て削除されます。

navigator.serviceWorker.getRegistrations().then(function(registrations) {
    // 登録されているworkerを全て削除する
    for(let registration of registrations) {
        registration.unregister();
    }
});
caches.keys().then(function(keys) {
    var promises = [];
    // キャッシュストレージを全て削除する
    keys.forEach(function(cacheName) {
        if (cacheName) {
            promises.push(caches.delete(cacheName));
        }
    });
});
38
31
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38
31