Skip to content

Commit

Permalink
Merge pull request #41 from Studyo/sw-environment
Browse files Browse the repository at this point in the history
Ability to provide environment variables to service worker.
  • Loading branch information
jkleinsc authored Oct 27, 2016
2 parents ff4b393 + 2b73be3 commit 23c2e54
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ ENV.serviceWorker = {
skipWaiting: true,
swIncludeFiles: [
'bower_components/pouchdb/dist/pouchdb.js'
]
],
swEnvironment: {
foo: ENV.foo,
}
};
```
The following options are available:
Expand All @@ -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).
Expand Down
3 changes: 3 additions & 0 deletions lib/service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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()+"';");
Expand Down

0 comments on commit 23c2e54

Please sign in to comment.