2017-02-06 6 views
3

私はプログレッシブウェブアプリケーション、https://www.tavest.comを構築しました。 サービスワーカーもChromeにキャッシュされている理由を理解できません。 https://www.tavest.com/service-worker-tavest.jsサービスワーカーを変更すると、クロムは変更を検出しないため、サービスワーカーは更新されません。Service WorkerもChromeにキャッシュされていますか?

私は何度もページを更新しますが、それはまだ同じです。しかし、Mozillaではうまく動作します。 ここでサービスワーカー

if ('serviceWorker' in navigator && (window.location.protocol === 'https:')) { 
 
     navigator.serviceWorker.register('/service-worker-tavest.js') 
 
     .then(function(registration) { 
 
     // updatefound is fired if service-worker.js changes. 
 
     registration.onupdatefound = function() { 
 
      // updatefound is also fired the very first time the SW is installed, 
 
      // and there's no need to prompt for a reload at that point. 
 
      // So check here to see if the page is already controlled, 
 
      // i.e. whether there's an existing service worker. 
 
      if (navigator.serviceWorker.controller) { 
 
      // The updatefound event implies that registration.installing is set: 
 
      // https://slightlyoff.github.io/ServiceWorker/spec/service_worker/index.html#service-worker-container-updatefound-event 
 
      var installingWorker = registration.installing; 
 

 
      installingWorker.onstatechange = function() { 
 
       switch (installingWorker.state) { 
 
       case 'installed': 
 
        // At this point, the old content will have been purged and the 
 
        // fresh content will have been added to the cache. 
 
        // It's the perfect time to display a "New content is 
 
        // available; please refresh." message in the page's interface. 
 
        console.warn('New content is available, please refresh the page'); 
 
        
 
        break; 
 

 
       case 'redundant': 
 
        throw new Error('The installing ' + 
 
            'service worker became redundant.'); 
 

 
       default: 
 
        // Ignore 
 
       } 
 
      }; 
 
      } 
 
     }; 
 
     }).catch(function(e) { 
 
     console.error('Error during service worker registration:', e); 
 
     }); 
 
    }

をインストールするための私のコードだが、サービス労働者は普通のJSファイルであり、それためですあなたの助け

暖かいよろしく、

+0

[サービスワーカーのJavaScript更新頻度(24時間ごと)](http://stackoverflow.com/questions/38843970/service-worker-javascript-update-frequency-every-24-hours) –

答えて

3

をありがとうブラウザにキャッシュされます。ブラウザがサービスワーカーファイルのキャッシュを停止するように

あなたは/service-worker-tavest.jsからno-cacheヘッダーを設定することができます。そうすれば、新しいファイルがアップロードされたときにすぐにサービスワーカーを更新させることができます。ここで

はnginxの中でそれを行う方法です:

location /service-worker-tavest.js { 
    # Don't use browser cache for service worker file 
    add_header cache-control "no-store, no-cache, must-revalidate"; 
} 
+0

ありがとうおとこ!私が試してみましょう。 –

1

私はこのキャッシングについての答えを知っていると思います。 これはChromeの「サービス担当者のライフサイクル」によるものです。

https://www.youtube.com/watch?v=TF4AB75PyIc

結論:クロムはそれ自体でサービスワーカーが更新されますので、Chromeブラウザでのキャッシュは大丈夫です。

関連する問題