Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use contentFor closes #2 #6

Merged
merged 1 commit into from
Nov 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
}
};