我需要在继续进行后台同步之前获取令牌,然后我将循环队列,构建一个新的Request,并将令牌添加到头部并发送它。
我正在使用插件来注册离线提交的数据,并且它是在workbox-background-sync
> requests
下注册的。循环while (entry = await queue.shiftRequest())
显然没有结果,因为我无法进入循环,所以我肯定做错了。我应该将代码从插件移动到队列(因为我需要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