2016-11-08 4 views
0
self.addEventListener('sync', function(event) { 
    console.log('firing: sync'); 

    if (event.tag == 'image-fetch') { 
     console.log('sync event fired'); 
     event.waitUntil(fetchurl()); 
    } 
}); 

function fetchurl() 
{ 
    console.log('firing: doSomeStuff()'); 

    /* fetch dynamic url */ 
    fetch('https://localhost/Service-Workers-BackgroundSync/server.php') 
    .then(function(response) { 
     return response; 
    }) 
    .then(function(text) { 
     console.log('Request successful', text); 
    }) 
    .catch(function(error) { 
     console.log('Request failed', error); 
    }); 
} 

service-worker.jsで動的URLを取得するにはどうすればよいですか?service-worker.jsの動的URLを更新する方法

私は 'sync'イベントを呼び出しています。 URLを動的に取得します。常に[送信]ボタンをクリックします。別のURLを取得しています。その後、同期イベントを使用してフェッチメソッドで動的URLを渡します。

私をご案内ください。

答えて

1

IndexedDBまたはlocalforageなどのラッパーを使用して、クライアントとService Workerの間で情報を共有できます。だからあなたのサービスワーカーでは、この方法でデータを読むことができます:

importScripts('./lib/localforage.js'); 

function fetchurl() 
{ 
    console.log('firing: doSomeStuff()'); 
    /* get dynamic url from IndexedDB using localForage */ 
    localforage.getItem('dynamicUrl') 
    .then(function (url) { 
    fetch(url) 
     .then(function(response) { 
     return response; 
     }) 
     .then(function(text) { 
     console.log('Request successful', text); 
     }) 
     .catch(function(error) { 
     console.log('Request failed', error); 
     }); 
    } 
} 
関連する問題