diff --git a/README.md b/README.md index 91d805c..0803050 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,8 @@ writeServiceWorker(completeTree, { ], dynamicCache: [ '/api/todos' - ] + ], + skipWaiting: true }); ``` diff --git a/lib/service-worker.js b/lib/service-worker.js index d04d1ea..2b21ca5 100644 --- a/lib/service-worker.js +++ b/lib/service-worker.js @@ -12,6 +12,7 @@ var BroccoliServiceWorker = function BroccoliServiceWorker(inTree, options) { this.inTree = inTree; options = options || {}; this.addPolyfill = options.addPolyfill || true; + this.skipWaiting = options.skipWaiting || false; this.debug = options.debug || true; this.dynamicCache = options.dynamicCache || []; this.excludePaths = options.excludePaths || ['tests/*']; @@ -26,6 +27,7 @@ BroccoliServiceWorker.prototype.constructor = BroccoliServiceWorker; BroccoliServiceWorker.prototype.write = function(readTree, destDir) { var addPolyfill = this.addPolyfill; + var skipWaiting = this.skipWaiting; var debug = this.debug; var dynamicCache = this.dynamicCache; var fallback = this.fallback; @@ -77,7 +79,7 @@ BroccoliServiceWorker.prototype.write = function(readTree, destDir) { if (!stat.isFile() && !stat.isSymbolicLink()) return; - lines.push(createArrayLine("'"+file+"'", idx, array.length)); + lines.push(createArrayLine(" '"+file+"'", idx, array.length)); }); lines.push("];"); @@ -87,6 +89,11 @@ BroccoliServiceWorker.prototype.write = function(readTree, destDir) { //ServiceWorker code derived from examples at https://github.com/GoogleChrome/samples/tree/gh-pages/service-worker addDebugLine("'Handling install event. Resources to pre-fetch:', urlsToPrefetch", debug, lines); + + if (skipWaiting) { + lines.push(" if (self.skipWaiting) { self.skipWaiting(); }"); + } + lines.push(" event.waitUntil("); lines.push(" caches.open(CURRENT_CACHES['prefetch']).then(function(cache) {"); lines.push(" return cache.addAll(urlsToPrefetch.map(function(urlToPrefetch) {");