Constructor

Queue

new Queue(input)

Creates an instance of Queue with the given options

Parameter

input

Optional

Object

Values in input have the following properties:

Parameter

maxRetentionTime

Optional

Number

Time for which a queued request will live in the queue(irrespective of failed/success of replay).

callbacks

Optional

Object

Callbacks for successfull/failed replay of a request as well as modifying before enqueue/dequeue-ing.

callbacks.replayDidSucceed

Optional

Fuction

Invoked with params (hash:string, response:Response) after a request is successfully replayed.

callbacks.replayDidFail

Optional

Fuction containing string

Invoked with param (hash:string) after a replay attempt has failed.

callbacks.requestWillEnqueue

Optional

Fuction containing Object

Invoked with param (reqData:Object) before a failed request is saved to the queue. Use this to modify the saved data.

callbacks.requestWillDequeue

Optional

Fuction containing Object

Invoked with param (reqData:Object) before a failed request is retrieved from the queue. Use this to modify the data before the request is replayed.

queueName

Optional

string

Queue name inside db in which requests will be queued.

broadcastChannel

Optional

BroadcastChannel

BroadcastChannel which will be used to publish messages when the request will be queued.

Example

When you want to push the requests manually
let bgQueue = new workbox.backgroundSync.Queue();
self.addEventListener('fetch', function(e) {
  if (!e.request.url.startsWith('https://jsonplaceholder.typicode.com')) {
    return;
  }

  const clone = e.request.clone();
  e.respondWith(fetch(e.request).catch((err) => {
    bgQueue.pushIntoQueue({
      request: clone,
    });
    throw err;
  }));
});

Methods

getResponse

getResponse(id) returns Object

Sets the dbName, which is used to store the queue and requests defaults to bgQueueSyncDB.

Parameter

id

String

The ID of the request.

Returns

Object Fetched response of the request.

pushIntoQueue

pushIntoQueue(input) returns Promise

This function pushes a given request into the IndexedDb Queue.

Parameter

input

Object

Values in input have the following properties:

Parameter

request

Request

The request which is to be queued

Returns

Promise Promise which resolves when the request is pushed in the queue.

replayRequests

replayRequests() returns Promise

Replays all the requests in the queue, this can be used for custom timing of replaying requests may be in an environment where sync event is not supported.

Returns

Promise A listener for when the requests have been replayed.