jkleinsc / broccoli-serviceworker

ServiceWorker generator for Broccoli and Ember.js
MIT License
92 stars 27 forks source link
broccoli ember service-worker

broccoli-serviceworker

ServiceWorker generator for Broccoli and Ember.js. Derived from broccoli-manifest.

For more details on ServiceWorker check out the following:

Usage for Ember Cli

ember install broccoli-serviceworker

Configuration

By default the service worker will be generated for production builds and the service worker registration logic will be added to your index.html automatically. If you wish to add your own logic to the generated service worker, you can place that code in .js files in app/serviceworkers. Your code will have full access to Service Worker Toolbox as well as any included tools that you specify with the swIncludeFiles option. Additionally, you can further customize broccoli-serviceworker by setting configurations in your environment.js file:

//app/config/environment.js

ENV.serviceWorker = {
  enabled: true,
  debug: true,
  sourcemaps: true,
  precacheURLs: ['/mystaticresource'],
  excludePaths: ['test.*', 'robots.txt',],
  fallback: [
    '/online.html /offline.html'
  ],
  networkFirstURLs: [
    '/api/todos'
  ],
  includeRegistration: true,
  serviceWorkerFile: "service-worker.js",
  skipWaiting: true,
  swIncludeFiles: [
    'bower_components/pouchdb/dist/pouchdb.js'
  ],
  swEnvironment: {
    foo: ENV.foo,
  }
};

The following options are available:

Routing Options

The following options allow you to specify routes that use sw-toolbox's built-in handlers. Each of these options accepts an array of URLs that can be strings or regular expressions.

Hooks

The following hooks are available to your service worker code. Implement a hook by defining a function by the hook's name and it will be called.

Usage for Broccoli.js

npm install --save broccoli-serviceworker

Use broccoli-serviceworker as your last filter in the Brocfile.js like this

var writeServiceWorker = require('broccoli-serviceworker');

...

var completeTree = mergeTrees([appJs, appCss, publicFiles]);

module.exports = mergeTrees([completeTree, writeServiceWorker((completeTree)]);

Upgrade your index.html (see below) and you are done.

Options

You can the options specified above as the second argument to writeServiceWorker:


writeServiceWorker(completeTree, {
  serviceWorkerFile: "service-worker.js",
  excludePaths: ['test.*', 'online.html',],
  precacheURLs: ['/api/offlineStates'],
  fallback: [
    '/api/states /api/offlineStates'
  ],
  networkFirstURLs: [
    '/api/todos'
  ],
  skipWaiting: true
});

One additional option is available for usage with Broccoli.js:

Upgrade your index.html

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:

<!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>