【问题标题】:Duplicate post request sent by Service WorkerService Worker 发送的重复发布请求
【发布时间】:2018-10-23 16:02:31
【问题描述】:

我刚刚发现了这个问题,因为我在使用 workbox-background-sync 时试图解决重复的发布请求问题。我的网络应用程序有一个上传照片的功能。但每次我确实两次上传到数据库。这是我的代码:

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'
    );

这可能是因为fetch(args.event.request.clone())。如果我删除它,则不再有重复。我正在使用工作箱 3.6.1。

【问题讨论】:

    标签: web service-worker workbox background-sync


    【解决方案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方式来解决这个问题。我正在使用 workbox 3.6.3,并创建了自己的插件以包含回调函数 fetchDidFail 来更新我的视图。以下是我找到的参考资料: one 和 two。不再有重复的帖子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 2018-04-11
      • 2019-10-15
      相关资源
      最近更新 更多