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.
offline.fresh(key), then return offline.get(key).offline.set(key, value, expiry) and return value.status === 0), attempt offline.get(key).value is 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:
offline.get() does not automatically remove items by default.
There is an optional argument to achieve that effect.MAX_DATE/2.
This enables expunging non-expiring values by insertion time but they still effectively never expire.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.
value = offline.get(key, expunge)Retrieves a previously stored value, optionally removing stale data.
string key: the storage keyboolean expunge (optional): if should auto-remove expired itemsobject|array|string|number|boolean|null: the storage valueoffline.set(key, value, expiry)Stores the key-value pair with a freshness time limit.
string key: the storage keyobject|array|string|number|boolean|null value: the storage valuenumber expiry (optional): the number of seconds before value is staleisFresh = offline.fresh(key)Tests the expiration of the data. Returns false if the value is stale or missing.
string key: the storage keyboolean: if the value has expired or is missingoffline.expire(key)If still fresh then marks the data as now expired.
string key: the storage keyoffline.remove(key)Removes the data stored at key.
string key: the storage keyoffline.flush(prefix)Removes all data with the given key prefix.
string prefix (optional): the storage keyoffline.enableWarnings(enabled)Enables or disables log warnings on quota or insertion errors.
boolean enabled: true to enable warningshasLocalStorage = offline.supported()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.