diff --git a/README.md b/README.md index 63ced6f..abfd9ac 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,10 @@ ENV.serviceWorker = { skipWaiting: true, swIncludeFiles: [ 'bower_components/pouchdb/dist/pouchdb.js' - ] + ], + swEnvironment: { + foo: ENV.foo, + } }; ``` The following options are available: @@ -52,6 +55,7 @@ The following options are available: * **fallback** - Array of URLs with fallbacks when the resource isn't available via network or cache. * **skipWaiting** - Allows a simple page refresh to update the app. Defaults to true. * **swIncludeFiles** - Array of files to include in the generated service worker. This is intended to allow inclusion of vendor files in your service worker. For example, if you wanted to run [PouchDB](http://pouchdb.com/) replication in a service worker, you need to include PouchDB in your service worker. +* **swEnvironment** - object that will be injected as-is in the service worker script as a top-level `swEnvironment` variable. ####Routing Options The following options allow you to specify routes that use [sw-toolbox's built-in handlers](https://github.com/GoogleChrome/sw-toolbox#built-in-handlers). Each of these options accepts an array of URLs that can be strings or [regular expressions](https://github.com/GoogleChrome/sw-toolbox#regular-expression-routes). diff --git a/lib/service-worker.js b/lib/service-worker.js index f6973a6..e740db2 100644 --- a/lib/service-worker.js +++ b/lib/service-worker.js @@ -39,6 +39,7 @@ var BroccoliServiceWorker = function BroccoliServiceWorker(inTree, options) { } this.swIncludeTree = options.swIncludeTree; this.swIncludeFiles = options.swIncludeFiles; + this.swEnvironment = options.swEnvironment || {}; }; BroccoliServiceWorker.prototype = Object.create(brocWriter.prototype); @@ -60,10 +61,12 @@ BroccoliServiceWorker.prototype.write = function(readTree, destDir) { }); var swIncludeFiles = this.swIncludeFiles; var swIncludeTree = this.swIncludeTree; + var swEnvironment = this.swEnvironment; return readSwIncludeTree(readTree, this.swIncludeTree).then(function(swIncludeDir){ return readTree(serviceWorkerTree).then(function (srcDir) { var lines = []; + lines.push("var swEnvironment = " + JSON.stringify(swEnvironment) + ";"); lines.push("importScripts('"+toolboxLocation+"');"); lines.push("var CACHE_PREFIX = 'brocsw-v';"); lines.push("var CACHE_VERSION = CACHE_PREFIX+'"+(new Date()).getTime()+"';");