0.0.1 • Published 5 years ago

@momsfriendlydevco/offline v0.0.1

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Offline ServiceWorker

Provide offline syncing functionality via a service worker.

This unit works by reading package declarations from the config, syncing those when internet is available and providing its own micro-ReST server when not.

It works by using the new Service Worker browser technology which sits between the requesting browser and the server which can mediate requests.

Syncing

The syncing process is made up of the following steps. FE is the front end, BE the backend and SW the service worker

  1. FE Send a message to the Service Worker to sync
  2. SW Setup a cache
  3. SW Fetch resources from the server
  4. BE Compute a list of resources - this is the flattened version of app.config.offline.packages[] specific to the active user with all dependencies resolved
  5. SW Compose a list of all resources to check
  6. SW Request each resource as a list, if the resource is a collection try to optimize this by fetching the collection IDs + version only then compare, if the resource is a URL just pull it

Config

As Service Workers are self contained JS files injecting the offline configuration is somewhat complex.

The easiest method is simply to use the supplied gulp build process while specifying a source file from where the config can be retrieved.

OFFLINE_CONFIG_FILE=./my-config.js
gulp build

In the above ./my-config.js is expected to export a JavaScript object which will be stringified and used as the offline config.

Config options:

PropertyTypeDescription
enabledbooleanWhether the service worker is enabled
cleanIntervalnumberTime in milliseconds between cleans
debugbooleanWhether the service worker should output debugging messages
debugFailuresbooleanWhether all failures should be cached in caches/failures
debugPrefixstringThe prefix to use when debugging
fetchOptionsobjectAdditional options to pass into fetch()
fetchAttemptsnumberThe number of retry attempts before failing sync on network errors
fetchConcurrentnumberNumber of concurrent requests allowed by Promise.allLimit()
forceOfflinebooleanIf true, treat all incoming connections as offline (inherited from caches/meta/flags/forceOffline{enabled: true} if its present)
autoSyncFirstnumberTime in milliseconds until first sync (falsy to disable)
autoSyncPollingOnlinenumberTime in milliseconds between polls after the first sync when we are online
autoSyncPollingOfflinenumberTime in milliseconds between polls after the first sync when we are offline
ignorearray <string>List of RegExps to skip caching for (these have to be strings to survive JSON encoding)
maxPostCyclesnumberMaximum number of recursive post attempts before giving up, set this to the ideal depth of A creates B creates C style loops that are possible
packagesarray <object>Package specification, see below

The base config file is used to set the above to appropriate defaults. The object resolved by the file at OFFLINE_CONFIG_FILE override those defaults.

Config Package declaration

The offline.packages object is composed as a collection with the following properties:

PropertyTypeDescription
idstringThe unique ID to identify the package
titlestringThe human friendly string to refer to the package
descriptionstringA longer description of the package
payloadarrayA list of resources that should be pulled within this package

Payload us an array of the following objects:

PropertyTypeDefaultDescription
typestringHow the resource is mediated. Enum of: "url", "collection"
urlstringThe URL to pull the resource from, use {{id}} as a specifier when iterating over a collection
versionbooleanCalculatedLook for versioning information when pulling the resource, set to falsy to force 304 validation instead of using __v values. This is assumed true for type == 'collection' and false otherwise
createbooleanfalse(If type==collection) specifies that posting to the URL will be handled as a document creation
countbooleantrue(If type==collection) specifies that the count of documents matching should be returned
querybooleantrue(If type==collection) specifies that ReST / Mongo / Sift style queries should provide a list of matching documents
postbooleanfalse(If type==collection) specifies that posting to the URL will be handled as a document update