-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
441 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git | ||
node_modules | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"node": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
broccoli-serviceworker | ||
================= | ||
|
||
ServiceWorker generator for Broccoli and Ember.js. Derived from [broccoli-manifest](https://github.com/racido/broccoli-manifest). | ||
|
||
For more details on ServiceWorker check out the following: | ||
* [ServiceWorker spec](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/) | ||
* https://github.com/slightlyoff/ServiceWorker | ||
* [Is ServiceWorker ready?](https://jakearchibald.github.io/isserviceworkerready/) | ||
|
||
Usage for Ember Cli | ||
------------------- | ||
|
||
`npm install --save-dev broccoli-serviceworker` | ||
|
||
```JavaScript | ||
//app/config/environment.js | ||
|
||
ENV.serviceWorker = { | ||
enabled: true, | ||
serviceWorkerFile: "service-worker.js", | ||
excludePaths: ['tests/', 'online.html',], | ||
includePaths: ['/'], | ||
fallback: [ | ||
'/online.html offline.html' | ||
], | ||
dynamicCache: [ | ||
'/api/todos' | ||
] | ||
}; | ||
``` | ||
|
||
Upgrade your `index.html` (see below) and you are done. | ||
|
||
Usage for Broccoli.js | ||
--------------------- | ||
|
||
`npm install --save broccoli-serviceworker` | ||
|
||
Use `broccoli-serviceworker` as your last filter in the `Brocfile.js` like this | ||
|
||
```JavaScript | ||
var writeServiceWorker = require('broccoli-serviceworker'); | ||
|
||
... | ||
|
||
var completeTree = mergeTrees([appJs, appCss, publicFiles]); | ||
|
||
module.exports = mergeTrees([completeTree, writeServiceWorker((completeTree)]); | ||
``` | ||
Options | ||
------- | ||
You can pass some options as the second argument to `writeServiceWorker`: | ||
```JavaScript | ||
|
||
writeServiceWorker(completeTree, { | ||
serviceWorkerFile: "service-worker.js", | ||
excludePaths: ['tests/', 'online.html',], | ||
includePaths: ['/'], | ||
fallback: [ | ||
'/online.html offline.html' | ||
], | ||
dynamicCache: [ | ||
'/api/todos' | ||
] | ||
}); | ||
``` | ||
Files can be filtered using regular expressions. | ||
```JavaScript | ||
{ | ||
excludePaths: ['index.html', new RegExp(/.\.map$/)], | ||
includePaths: [''] | ||
} | ||
``` | ||
Upgrade your index.html | ||
----------------------- | ||
In order to use the generated serviceworker, you will need to register the serviceworker. You can do so with the following code: | ||
```HTML | ||
<!DOCTYPE html> | ||
<html> | ||
... | ||
<script> | ||
if ('serviceWorker' in navigator) { | ||
navigator.serviceWorker.register('./service-worker.js', {scope: './'}) | ||
.catch(function(error) { | ||
alert('Error registering service worker:'+error); | ||
}); | ||
} else { | ||
alert('service worker not supported'); | ||
} | ||
</script> | ||
</html> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var mergeTrees = require('broccoli-merge-trees'); | ||
var funnel = require('broccoli-funnel'); | ||
|
||
var serviceWorker = require('./x'); | ||
|
||
module.exports = { | ||
name: 'broccoli-serviceworker', | ||
|
||
included: function (app) { | ||
this.app = app; | ||
this.initializeOptions(); | ||
}, | ||
|
||
initializeOptions: function () { | ||
var appOptions = this.app.project.config(this.app.env); | ||
var options = appOptions.serviceWorker || {}; | ||
|
||
var defaultOptions = { | ||
enabled: this.app.env === 'production', | ||
excludePaths: ['test.*'], | ||
includePaths: ['/'], | ||
}; | ||
|
||
for (var option in defaultOptions) { | ||
if (!options.hasOwnProperty(option)) { | ||
options[option] = defaultOptions[option]; | ||
} | ||
} | ||
this.serviceWorkerOptions = options; | ||
}, | ||
|
||
postprocessTree: function (type, tree) { | ||
var options = this.serviceWorkerOptions; | ||
|
||
if (type === 'all' && options.enabled) { | ||
var serviceWorkerTree = funnel(tree, { | ||
exclude: options.excludePaths | ||
}); | ||
return mergeTrees([tree, serviceWorker(serviceWorkerTree, options)]); | ||
} | ||
|
||
return tree; | ||
}, | ||
|
||
treeFor: function() {} | ||
}; |
Oops, something went wrong.