mardi 23 octobre 2018

Duplicate post request sent by Service Worker

I just found this issue because I was trying to solve the duplicate post request issue when I am using workbox-background-sync. There is a function of my web app to upload the photos. But every time I did uploaded twice to the database. Here is the code I have:

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

It may because the fetch(args.event.request.clone()). If I remove it, then there is no duplication anymore. I am using workbox 3.6.1 .




Aucun commentaire:

Enregistrer un commentaire