如何更新服务中的动态 url-worker.js

How to Update dynamic url in service-worker.js

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);
    });
}

如何在服务中获得动态 URL-worker.js?

我正在调用 'sync' 事件。我想动态地传递获取 URL..总是点击提交按钮我变得不同 URL,然后我想通过 'sync' 事件在获取方法中传递动态 URL。

请指导我。

您可以使用 IndexedDB or a wrapper such as localforage 在客户端和 Service Worker 之间共享信息。所以在你的 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);
      });
  }
}