The PWA ticket service worker architecture
The 2048 and Podstr apps have relied on a single script. The PWA ticket app uses more complex techniques like importing libraries to drive the logic.
Service workers can load external scripts using the importScripts
method. This function is available in the global scope and accepts an array of URLs. These are additional scripts and work much like the node.js require
system:
self.importScripts("js/libs/localforage.min.js", "js/app/libs/api.js", "sw/response-mgr.js", "sw/push-mgr.js", "sw/invalidation-mgr.js", "sw/date-mgr.js" );
The first two scripts are also used in the client code. localForage
is an IndexedDB
wrapper and the API script manages access to the API and authentication token. The Mustache
library file is imported in the ResponseManager
module and I will cover how it is used later.
The remaining scripts are common service worker libraries to help with caching strategies, such as cache invalidation and push management...