0

I have a PWA that is used to send orders. It has to work also in offline mode. Usually 10 - 15 orders are taken each day, they are put into a Workbox queue and when there is finally internet at the end of the day, the requests are all resent. The problem is sometimes some requests disappear. Not always, but every few days 1-2 requests get lost without a trace. Seemingly these requests don't even reach my backend as per logs. Here is my queue:

const queue = new workbox.backgroundSync.Queue('orderQueue', {
maxRetentionTime: 10 * 24 * 60,
onSync: async ({ queue }) => {
    let entry;

    if (queue.size() !== 0) {
        try {
            const queueContents = await queue.getAll();
            const queueStateAtTimeOfResend = await Promise.all(queueContents.map(req => parseReadableStreamToJson(req.request.clone().body)));
            channel.postMessage({
                type: 'logQueueResendAttempt',
                queueStateAtAttempt: JSON.stringify(queueStateAtTimeOfResend)
            });
        } catch (e) {
            console.log(e);
        }
    }

    while (entry = await queue.shiftRequest()) {
            const fetchResponse = await fetch(entry.request.clone());
            if (!fetchResponse.ok) {
                throw new Error();
            }

            channel.postMessage({ type: 'successfullyFinishedReplaying' });
        } catch (error) {
            await queue.unshiftRequest(entry);
            channel.postMessage({ type: 'failedToFinishReplaying' });
            throw error;
        }
    }
},
});

I throw an error when I get an error status code back, so the requests are put back to the queue. When I test the application myself it seems to work fine, but sadly the error happens only in production. I log the state of the queue at resend attempt, however when there are missing orders, there are no logs as well, which makes me think it is some kind of connectivity issue. Still, whether its no connection or bad status code, nothing should go missing. What can cause the disappearing requests? Thanks.

JohnnyK
  • 33
  • 1
  • 5

0 Answers0