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