workbox-background-sync
Summary
- Types
Types
BackgroundSyncPlugin
A class implementing the fetchDidFail lifecycle callback. This makes it easier to add failed requests to a background sync Queue.
Properties
- constructor
function
The
constructorfunction looks like:(name: string, options?: QueueOptions) => {...}- name
string
See the
workbox-background-sync.Queuedocumentation for parameter details. - options
QueueOptions optional
- returns
Queue
A class to manage storing failed requests in IndexedDB and retrying them later. All parts of the storing and replaying process are observable via callbacks.
Properties
- constructor
function
Creates an instance of Queue with the given options
The
constructorfunction looks like:(name: string, options?: QueueOptions) => {...}- name
string
The unique name for this queue. This name must be unique as it's used to register sync events and store requests in IndexedDB specific to this instance. An error will be thrown if a duplicate name is detected.
- options
QueueOptions optional
- returns
- name
string
- getAll
function
Returns all the entries that have not expired (per
maxRetentionTime). Any expired entries are removed from the queue.The
getAllfunction looks like:() => {...}- returns
Promise<QueueEntry[]>
- popRequest
function
Removes and returns the last request in the queue (along with its timestamp and any metadata). The returned object takes the form:
{request, timestamp, metadata}.The
popRequestfunction looks like:() => {...}- returns
Promise<QueueEntry>
- pushRequest
function
Stores the passed request in IndexedDB (with its timestamp and any metadata) at the end of the queue.
The
pushRequestfunction looks like:(entry: QueueEntry) => {...}- entry
QueueEntry
- returns
Promise<void>
- registerSync
function
Registers a sync event with a tag unique to this instance.
The
registerSyncfunction looks like:() => {...}- returns
Promise<void>
- replayRequests
function
Loops through each request in the queue and attempts to re-fetch it. If any request fails to re-fetch, it's put back in the same position in the queue (which registers a retry for the next sync event).
The
replayRequestsfunction looks like:() => {...}- returns
Promise<void>
- shiftRequest
function
Removes and returns the first request in the queue (along with its timestamp and any metadata). The returned object takes the form:
{request, timestamp, metadata}.The
shiftRequestfunction looks like:() => {...}- returns
Promise<QueueEntry>
- size
function
Returns the number of entries present in the queue. Note that expired entries (per
maxRetentionTime) are also included in this count.The
sizefunction looks like:() => {...}- returns
Promise<number>
- unshiftRequest
function
Stores the passed request in IndexedDB (with its timestamp and any metadata) at the beginning of the queue.
The
unshiftRequestfunction looks like:(entry: QueueEntry) => {...}- entry
QueueEntry
- returns
Promise<void>
QueueOptions
Properties
- forceSyncFallback
boolean optional
- maxRetentionTime
number optional
- onSync
OnSyncCallback optional
QueueStore
A class to manage storing requests from a Queue in IndexedDB, indexed by their queue name for easier access.
Most developers will not need to access this class directly; it is exposed for advanced use cases.
Properties
- constructor
function
Associates this instance with a Queue instance, so entries added can be identified by their queue name.
The
constructorfunction looks like:(queueName: string) => {...}- queueName
string
- returns
- deleteEntry
function
Deletes the entry for the given ID.
WARNING: this method does not ensure the deleted entry belongs to this queue (i.e. matches the
queueName). But this limitation is acceptable as this class is not publicly exposed. An additional check would make this method slower than it needs to be.The
deleteEntryfunction looks like:(id: number) => {...}- id
number
- returns
Promise<void>
- getAll
function
Returns all entries in the store matching the
queueName.The
getAllfunction looks like:() => {...}- returns
Promise<QueueStoreEntry[]>
- popEntry
function
Removes and returns the last entry in the queue matching the
queueName.The
popEntryfunction looks like:() => {...}- returns
Promise<QueueStoreEntry>
- pushEntry
function
Append an entry last in the queue.
The
pushEntryfunction looks like:(entry: UnidentifiedQueueStoreEntry) => {...}- entry
UnidentifiedQueueStoreEntry
- returns
Promise<void>
- shiftEntry
function
Removes and returns the first entry in the queue matching the
queueName.The
shiftEntryfunction looks like:() => {...}- returns
Promise<QueueStoreEntry>
- size
function
Returns the number of entries in the store matching the
queueName.The
sizefunction looks like:() => {...}- returns
Promise<number>
- unshiftEntry
function
Prepend an entry first in the queue.
The
unshiftEntryfunction looks like:(entry: UnidentifiedQueueStoreEntry) => {...}- entry
UnidentifiedQueueStoreEntry
- returns
Promise<void>
StorableRequest
A class to make it easier to serialize and de-serialize requests so they can be stored in IndexedDB.
Most developers will not need to access this class directly; it is exposed for advanced use cases.
Properties
- constructor
function
Accepts an object of request data that can be used to construct a
Requestbut can also be stored in IndexedDB.The
constructorfunction looks like:(requestData: RequestData) => {...}- requestData
RequestData
An object of request data that includes the
urlplus any relevant properties of [requestInit]https://fetch.spec.whatwg.org/#requestinit.
- returns
- clone
function
Creates and returns a deep clone of the instance.
The
clonefunction looks like:() => {...}- returns
- toObject
function
Returns a deep clone of the instances
_requestDataobject.The
toObjectfunction looks like:() => {...}- returns
RequestData
- toRequest
function
Converts this instance to a Request.
The
toRequestfunction looks like:() => {...}- returns
Request
- fromRequest
function
Converts a Request object to a plain object that can be structured cloned or JSON-stringified.
The
fromRequestfunction looks like:(request: Request) => {...}- request
Request
- returns
Promise<StorableRequest>