Web 服务人员发送了重复的post请求

Web 服务人员发送了重复的post请求,web,service-worker,workbox,background-sync,Web,Service Worker,Workbox,Background Sync,我发现这个问题是因为我在使用workbox后台同步时试图解决重复的post请求问题。我的网络应用程序有一个上传照片的功能。但每次我都会上传两次到数据库。以下是我的代码: const bgSyncQueue = new workbox.backgroundSync.Queue( 'photoSubmissions', { maxRetentionTime: 48 * 60,//48 hours callbacks: {

我发现这个问题是因为我在使用workbox后台同步时试图解决重复的post请求问题。我的网络应用程序有一个上传照片的功能。但每次我都会上传两次到数据库。以下是我的代码:

const bgSyncQueue = new workbox.backgroundSync.Queue(
        'photoSubmissions',
        {
            maxRetentionTime: 48 * 60,//48 hours
            callbacks: {
                queueDidReplay: function (requests) {
                    if (requests.length === 0) {
                        removeAllPhotoSubmissions();
                    }
                    else {
                        for(let request of requests) {
                            if (request.error === undefined && (request.response && request.response.status === 200)) {
                                removePhotoSubmission();
                            }
                        }
                    }
                }
            }
        });
    workbox.routing.registerRoute(
        new RegExp('.*\/Home\/Submit'),
        args => {
            const promiseChain = fetch(args.event.request.clone())
            .catch(err => {
                bgSyncQueue.addRequest(args.event.request);
                addPhotoSubmission();
                changePhoto();
            });
            event.waitUntil(promiseChain);
        },
        'POST'
    );

这可能是因为
获取(args.event.request.clone())
。如果我删除它,那么就不再有重复。我正在使用workbox 3.6.1。

终于找到了解决方案。下面是我的代码:

const photoQueue = new workbox.backgroundSync.Plugin('photoSubmissions', {
    maxRetentionTime: 48 * 60, // Retry for max of 48 Hours
    callbacks: {
        queueDidReplay: function (requests) {
            if (requests.length === 0) {
                removeAllPhotoSubmissions();
            }
            else {
                for(let request of requests) {
                    if (request.error === undefined && (request.response && request.response.status === 200)) {
                        removePhotoSubmission();
                    }
                }
            }
        }
    }
});

const myPhotoPlugin = {
    fetchDidFail: async ({originalRequest, request, error, event}) => {
                    addPhotoSubmission();
                    changePhoto();
    }
};

workbox.routing.registerRoute(
    new RegExp('.*\/Home\/Submit'),
    workbox.strategies.networkOnly({
        plugins: [
            photoQueue,
            myPhotoPlugin
        ]
    }),
    'POST'
);
我删除了
fetch
。如果我们仍然想自己控制,我们需要使用
respondWith()
。我已经测试过了,它正在工作。但我想用更多的工作箱方式来解决这个问题。我正在使用workbox 3.6.3,我创建了自己的插件,包括一个回调函数
fetchDidFail
来更新我的视图。以下是我找到的参考资料: 和。不再有重复的帖子了