diff --git a/packages/workbox-background-sync/Queue.mjs b/packages/workbox-background-sync/Queue.mjs index e28063584..aac1eadca 100644 --- a/packages/workbox-background-sync/Queue.mjs +++ b/packages/workbox-background-sync/Queue.mjs @@ -132,17 +132,18 @@ class Queue { replayedRequests.push(replay); } + await this._runCallback('queueDidReplay', replayedRequests); + // If any requests failed, put the failed requests back in the queue - // and register for another sync. + // and rethrow the failed requests count. if (failedRequests.length) { await Promise.all(failedRequests.map((storableRequest) => { return this._queueStore.addEntry(storableRequest); })); - await this._registerSync(); + throw new WorkboxError('queue-replay-failed', + {name: this._name, count: failedRequests.length}); } - - await this._runCallback('queueDidReplay', replayedRequests); } /** diff --git a/packages/workbox-core/models/messages/messages.mjs b/packages/workbox-core/models/messages/messages.mjs index 686545d24..3a9bc8c10 100644 --- a/packages/workbox-core/models/messages/messages.mjs +++ b/packages/workbox-core/models/messages/messages.mjs @@ -128,6 +128,10 @@ export default { `registered.`; }, + 'queue-replay-failed': ({name, count}) => { + return `${count} requests failed, while trying to replay Queue: ${name}.`; + }, + 'duplicate-queue-name': ({name}) => { return `The Queue name '${name}' is already being used. ` + `All instances of backgroundSync.Queue must be given unique names.`; diff --git a/test/workbox-background-sync/node/lib/test-Queue.mjs b/test/workbox-background-sync/node/lib/test-Queue.mjs index 3b781d807..61bf1af1e 100644 --- a/test/workbox-background-sync/node/lib/test-Queue.mjs +++ b/test/workbox-background-sync/node/lib/test-Queue.mjs @@ -293,8 +293,10 @@ describe(`[workbox-background-sync] Queue`, function() { await queue.addRequest(new Request('/three')); await queue.addRequest(new Request('/four')); await queue.addRequest(new Request('/five')); - await queue.replayRequests(); // The 2nd and 4th requests should fail. + await expectError(() => { + return queue.replayRequests(); // The 2nd and 4th requests should fail. + }, 'queue-replay-failed'); const entries = await getObjectStoreEntries(); expect(entries.length).to.equal(2); @@ -302,28 +304,23 @@ describe(`[workbox-background-sync] Queue`, function() { expect(entries[1].storableRequest.url).to.equal('/four'); }); - it(`should re-register for a sync event if re-fetching fails`, + it(`should throw WorkboxError if re-fetching fails`, async function() { - sandbox.stub(self.registration, 'sync').value({ - register: sinon.stub().resolves(), - }); sandbox.stub(self, 'fetch') .onCall(1).rejects(new Error()) .callThrough(); + const failureURL = '/two'; const queue = new Queue('foo'); // Add requests for both queues to ensure only the requests from // the matching queue are replayed. await queue.addRequest(new Request('/one')); - await queue.addRequest(new Request('/two')); - - self.registration.sync.register.reset(); - await queue.replayRequests(); // The second request should fail. + await queue.addRequest(new Request(failureURL)); - expect(self.registration.sync.register.calledOnce).to.be.true; - expect(self.registration.sync.register.calledWith( - 'workbox-background-sync:foo')).to.be.true; + await expectError(() => { + return queue.replayRequests(); + }, 'queue-replay-failed'); }); it(`should invoke all replay callbacks`, async function() { @@ -376,7 +373,9 @@ describe(`[workbox-background-sync] Queue`, function() { await queue.addRequest(new Request('/three')); await queue.addRequest(new Request('/four')); - await queue.replayRequests(); + await expectError(() => { + return queue.replayRequests(); + }, 'queue-replay-failed'); expect(requestWillReplay.calledTwice).to.be.true;