Skip to content

Commit

Permalink
Merge pull request #6 from miguelcobain/content-for
Browse files Browse the repository at this point in the history
use contentFor closes #2
  • Loading branch information
jkleinsc committed Nov 23, 2015
2 parents 38f21ef + 452fc21 commit 74382b7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ENV.serviceWorker = {
```

Upgrade your `index.html` (see below) and you are done.
The service worker bootstrap logic will be added to your index.html automatically, using contentFor hooks.

Usage for Broccoli.js
---------------------
Expand Down Expand Up @@ -82,7 +83,8 @@ Files can be filtered using regular expressions.
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:
In order to use the generated serviceworker, you will need to register the serviceworker. This is done automatically if using as an Ember.js addon.
If you're not using Ember.js, you can use the following code:
```HTML
<!DOCTYPE html>
<html>
Expand Down
27 changes: 22 additions & 5 deletions lib/ember-addon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ var serviceWorker = require('./service-worker.js');

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 || {};
Expand All @@ -29,10 +29,10 @@ module.exports = {
}
}
this.serviceWorkerOptions = options;
},
},

postprocessTree: function (type, tree) {
var options = this.serviceWorkerOptions;
var options = this.serviceWorkerOptions;

if (type === 'all' && options.enabled) {
var serviceWorkerTree = funnel(tree, {
Expand All @@ -44,5 +44,22 @@ module.exports = {
return tree;
},

treeFor: function() {}
treeFor: function() {},

contentFor: function(type, config) {
if (config.environment !== 'test' && type === 'body-footer') {
var lines = [];
lines.push('<script>');
lines.push('if (\'serviceWorker\' in navigator) {');
lines.push(' navigator.serviceWorker.register(\'./service-worker.js\', {scope: \'./\'})');
lines.push(' .catch(function(error) {');
lines.push(' alert(\'Error registering service worker:\'+error);');
lines.push(' });');
lines.push('} else {');
lines.push(' alert(\'service worker not supported\');');
lines.push('}');
lines.push('</script>');
return lines.join('\n');
}
}
};

0 comments on commit 74382b7

Please sign in to comment.