Initializing a plugin
Ember Service Worker plugins are just Ember CLI addons to begin with. Begin with initializing a new Ember addon.
The first step to turn the regular Ember CLI addon into a plugin is to open up
package.json and add
"ember-service-worker-plugin" as keyword to it. This is
ember-service-worker addon only picks up addons that have this
keyword. An abridged version of
package.json should look like this:
Adding Service Worker code
To add code to the generated Service Worker, you need to create a
service-worker folder in the root of your project. You will also at least need
to have an
index.js file in it, this will be the entry point file that the
generated Service Worker will include.
The pattern to include modules goes like this:
So if you want to import the
hashing-cacher plugin the import statement would be:
Adding Service Worker registration code
Adding code to the Service Worker registration script works just like adding
code to the Service Worker script, except that the code needs to be in the
service-worker-registration folder instead.
Programmaticaly customizing the output
If you need to modify or transform your service worker (registration) code, you
can do that using the
treeForServiceWorkerRegistration hooks in the
index.js file that is in the
root of the project.
The first argument to the hooks is the tree containing the contents of your
service-worker-registration directory. The second argument
is the tree that contains the fully built app.