工作箱离线同步请求失败

时间:2019-06-28 12:28:15

标签: reactjs service-worker workbox workbox-webpack-plugin

我想添加应用程序的脱机功能。在脱机模式下从缓存中获取记录很好,但是在添加资源时会遇到问题。如下图所示,在添加资源时,请求被添加到队列中,并且稍后也会处理,但是

  1. 我需要拦截请求,这意味着我想返回成功(200) 原始请求,这样我的“无法提取”错误将被删除
  2. 需要更新缓存,以便列表将返回有保证的数据
  3. 正在调用背景同步,但API未添加新资源,可能缺少某些内容
importScripts(  'https://storage.googleapis.com/workbox-cdn/releases/4.3.1/workbox-sw.js');

if (workbox) {
  console.log(`Yay! Workbox is loaded ?`);
} else {
  console.log(`Boo! Workbox didn't load ?`);
}

const showNotification = () => {
  // eslint-disable-next-line no-restricted-globals
  workbox.registration.showNotification('Background sync success!', {
    body: '?`?`?`'
  });
};

const bgSyncPlugin = new workbox.backgroundSync.Plugin('todoQueue', {
  maxRetentionTime: 1,
  callbacks: {
    queueDidReplay: showNotification
  },
});

workbox.routing.registerRoute(
  /\.(?:js|css|html)$/,
  workbox.strategies.networkFirst()
);

workbox.routing.registerRoute(
  'http://localhost:3000',
  workbox.strategies.networkFirst()
);

workbox.routing.registerRoute(
  new RegExp('http://localhost:1337/api/(.)*'),
  workbox.strategies.networkFirst({
  }),
  'GET'
);

workbox.routing.registerRoute(
  new RegExp('http://localhost:1337/api/(.)*'),
  workbox.strategies.networkFirst({
    plugins: [bgSyncPlugin]
  }),
  'POST'
);

request dedbug 预先感谢

0 个答案:

没有答案