Workbox 4.1 backgroundSync:Queue 和插件
Workbox 4.1 backgroundSync: Queue and Plugin
我需要获取令牌才能继续后台同步,然后我将循环 queue,构建一个新的 Request 添加令牌到 headers 并发送。
我正在使用插件注册离线提交的数据,它正在 workbox-background-sync
> requests
下注册。循环 while (entry = await queue.shiftRequest())
显然没有结果,因为我无法进入循环,所以我肯定做错了。我是否应该将代码从 Plugin 移至 Queue(因为我需要 shiftRequest())?如果是这样,我如何让我的路线在 IndexedDB 中注册东西以进行后台同步?
这就是我正在尝试的:
const queue = new workbox.backgroundSync.Queue();
const bgSyncPlugin = new workbox.backgroundSync.Plugin('bgsync', {
onSync: async (q) => {
// this will log
console.log("Background sync started", q);
let entry;
while (entry = await queue.shiftRequest()) {
// this will never appear in the log
console.log("Hurray", entry);
}
}
});
workbox.routing.registerRoute(
new RegExp('/suggestion/post'),
new workbox.strategies.NetworkOnly({
plugins: [bgSyncPlugin]
}),
'POST'
);
我遇到了同样的问题。此处描述了解决方案:
https://github.com/GoogleChrome/workbox/issues/1982#issuecomment-475645712
我需要获取令牌才能继续后台同步,然后我将循环 queue,构建一个新的 Request 添加令牌到 headers 并发送。
我正在使用插件注册离线提交的数据,它正在 workbox-background-sync
> requests
下注册。循环 while (entry = await queue.shiftRequest())
显然没有结果,因为我无法进入循环,所以我肯定做错了。我是否应该将代码从 Plugin 移至 Queue(因为我需要 shiftRequest())?如果是这样,我如何让我的路线在 IndexedDB 中注册东西以进行后台同步?
这就是我正在尝试的:
const queue = new workbox.backgroundSync.Queue();
const bgSyncPlugin = new workbox.backgroundSync.Plugin('bgsync', {
onSync: async (q) => {
// this will log
console.log("Background sync started", q);
let entry;
while (entry = await queue.shiftRequest()) {
// this will never appear in the log
console.log("Hurray", entry);
}
}
});
workbox.routing.registerRoute(
new RegExp('/suggestion/post'),
new workbox.strategies.NetworkOnly({
plugins: [bgSyncPlugin]
}),
'POST'
);
我遇到了同样的问题。此处描述了解决方案: https://github.com/GoogleChrome/workbox/issues/1982#issuecomment-475645712