繁体   English   中英

如何在service-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);
    });
}

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

我正在呼叫“同步”事件。 我想动态传递获取URL。.总是单击提交按钮,我得到了不同的URL,然后我想通过'sync'事件在获取方法中传递动态URL。

请指导我。

您可以使用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);
      });
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM