简体   繁体   English

具有背景同步模块的Workbox Service Worker

[英]Workbox Service Worker with Background Sync module

I've been using Workbox for a few days and i correctly set it up for generating a service worker from a source and dont let Workbox generate it for me. 我已经使用Workbox几天了,并且正确地设置了它以便从源生成服务工作者,并且不要让Workbox为我生成它。

It works ok but now im trying to include the workbox-background-sync module for storing some failed POST requests and i cant get it to work. 它工作正常,但现在我试图包括用于存储一些失败的POST请求的workbox-background-sync模块,但我无法使其正常工作。 After running the SW, i get " Uncaught TypeError: Cannot read property 'QueuePlugin' of undefined " on the first backgroundSync line (line number 9). 运行SW后,在第一backgroundSync行(第9行)上收到“ Uncaught TypeError:无法读取未定义的属性'QueuePlugin' ”。 This is the generated ServiceWorker file: 这是生成的ServiceWorker文件:

importScripts('workbox-sw.prod.v2.1.2.js');
importScripts('workbox-background-sync.prod.v2.0.3.js');

const workbox = new WorkboxSW({
  skipWaiting: true,
  clientsClaim: true
})

let bgQueue = new workbox.backgroundSync.QueuePlugin({
  callbacks: {
    replayDidSucceed: async(hash, res) => {
    self.registration.showNotification('Background sync demo', {
    body: 'Product has been purchased.',
    icon: '/images/shop-icon-384.png',
  });
},
replayDidFail: (hash) => {},
requestWillEnqueue: (reqData) => {},
requestWillDequeue: (reqData) => {},
},
});

const requestWrapper = new workbox.runtimeCaching.RequestWrapper({
  plugins: [bgQueue],
});

const route = new workbox.routing.RegExpRoute({
  regExp: new RegExp('^https://jsonplaceholder.typicode.com'),
  handler: new workbox.runtimeCaching.NetworkOnly({requestWrapper}),
});

const router = new workbox.routing.Router();
router.registerRoute({route});

workbox.router.registerRoute(
  new RegExp('.*\/api/catalog/available'),
  workbox.strategies.networkFirst()
);

workbox.router.registerRoute(
  new RegExp('.*\/api/user'),
  workbox.strategies.networkFirst()
);

workbox.router.registerRoute(
  new RegExp('.*\/api/security-element'),
  workbox.strategies.networkOnly()
);

workbox.precache([]);

I've tried to load it with workbox.loadModule('workbox-background-sync'), a workaround i've found on github, but it still not working. 我试图用workbox.loadModule('workbox-background-sync')加载它,这是我在github上发现的一种解决方法,但仍然无法正常工作。 Also, trying with self.workbox = new WorkboxSW() , with the same faith. 另外,以相同的信念尝试self.workbox = new WorkboxSW()

PS: Is there a way i can hook a function AFTER a strategy like networkFirst has failed and its going to respond with cache? PS:有没有办法可以在诸如networkFirst之类的策略失败并且能够响应缓存挂接函数? because i want to know, if im getting a cached response i would like to tell the user that by modifying the incoming response and handle it later in Vue for example. 因为我想知道,如果即时通讯收到缓存的响应,我想告诉用户通过修改传入的响应并稍后在Vue中进行处理。 Thanks for reading! 谢谢阅读!

我解决了它..它实际上是非常愚蠢的,我是accesing的const workbox ,而不是实例化一个新workbox.backgroundSync,我固定它通过简单地重新命名const workboxconst workboxSW

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

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