offline.js enables access to recently retrieved data when the application becomes disconnected.
It can also act as a simple expiring data cache, or as a wrapper for localStorage access.
This library does not depend on anything else besides DOM Storage. If localStorage is not available, then it will simply not ever hold anything in cache. Nothing needs to change from the client perspective.
To achieve both an expiring cache (for recently requested data) as well as an offline data cache, this is the pattern to integrate into your remote data access.
To achieve just an offline data cache, skip step 1 or set expiry to zero.
- If
offline.fresh(key), then returnoffline.get(key). - Otherwise, execute remote API call.
- If remote API call was successful, store
offline.set(key, value, expiry)and returnvalue. - If remote API call failed (e.g.,
status === 0), attemptoffline.get(key). - If
valueis missing, notify caller of offline/remote error.
This library began as a fork of the excellent lscache by Pamela Fox.
While trying to implement the offline usage pattern, it became clear that some breaking changes needed to be made to the interface.
Rather than confuse the two libraries, offline.js deviates from lscache.js to solve a related but slightly different problem:
- Additional methods were added to allow insight into freshness of cached items and the expiring of items without removing.
offline.get()does not automatically remove items by default. There is an optional argument to achieve that effect.- Data stored without expiry times are given an expiry of
MAX_DATE/2. This enables expunging non-expiring values by insertion time but they still effectively never expire. - The storage format has been altered to make a little easier to read with smaller keys and expiry values.
- All data values are JSON encoded (including strings, unlike in lscache) to avoid ambiguity with JSON-like string values.
- The additional space required is more than made up for by shorter keys and expiry values.
- The concept of "buckets" has been removed in favor of being able to
flush()items with a prefix. The purpose is the same but the usage is simpler and less error prone.
Initially, this was going to incorporate hooks for HTML 5 Online/Offline events.
Ultimately, this seemed to only be partially useful and potentially broken.
Even in browsers where navigator.onLine is implemented to spec, it doesn't apply to the situation where the remote API is unavailable.
The decision has therefore been made to allow the application to decide what is considered "offline".
Typically, this is implemented as a status of 0 as opposed to the server response of the 400-500 range.
Retrieves a previously stored value, optionally removing stale data.
stringkey: the storage keybooleanexpunge (optional): if should auto-remove expired items
object|array|string|number|boolean|null: the storage value
Stores the key-value pair with a freshness time limit.
stringkey: the storage keyobject|array|string|number|boolean|nullvalue: the storage valuenumberexpiry (optional): the number of seconds before value is stale
Tests the expiration of the data. Returns false if the value is stale or missing.
stringkey: the storage key
boolean: if the value has expired or is missing
If still fresh then marks the data as now expired.
stringkey: the storage key
Removes the data stored at key.
stringkey: the storage key
Removes all data with the given key prefix.
stringprefix (optional): the storage key
Enables or disables log warnings on quota or insertion errors.
booleanenabled: true to enable warnings
Tests the support of localStorage.
boolean: if localStorage is supported.
Run the unit tests.
offline.js is licensed under the Apache License, Version 2.0.
Copyright © 2013, Stephen M. McKamey.
offline.js is a derivative work of lscache which is also licensed under the Apache License, Version 2.0.
Copyright © 2011, Pamela Fox.