UNPKG

3.9 kBMarkdownView Raw
1---
2layout: doc_page
3title: Configuration
4order: 3
5sections:
6 - anchor: versioning
7 title: Versioning
8 - anchor: registration
9 title: Registration
10 - anchor: disabling-the-service-worker
11 title: Disabling the Service Worker
12 - anchor: customizing-the-sw.js-filename
13 title: Customizing the sw.js Filename
14---
15
16### Versioning
17
18The service worker in the browser is updated when the `sw.js` file has changed.
19The problem is that not every update to your app will result in a change to the
20`sw.js` file. Therefore ember-service-worker has a few configuration options to
21force a change to the `sw.js` file when building. This is done by setting the
22`versionStrategy` property in your `ember-cli-build.js` file. An example:
23
24```js
25var EmberApp = require('ember-cli/lib/broccoli/ember-app');
26
27module.exports = function(defaults) {
28 var app = new EmberApp(defaults, {
29 'ember-service-worker': {
30 versionStrategy: 'every-build'
31 }
32 });
33
34 return app.toTree();
35};
36```
37
38In the example the strategy is set to `every-build`, this is a very safe
39strategy, because it will force a change to your `sw.js` file everytime you do a
40new build. This is also the strategy what Ember Service Worker defaults to.
41
42There are two other options to this: `project-revision` and `project-version`.
43
44The `project-revision` option will create a checksum based on the contents of
45your application folder using the [hash-for-dep](https://github.com/stefanpenner/hash-for-dep)
46package and use that to version the `sw.js` file.
47
48The `project-version` option will use the version number specified in your
49`package.json` file to version the `sw.js` file.
50
51### Registration
52
53There are various ways to inject the service worker registration script. By
54default the registration file `sw-registration.js` is loaded using a simple
55script tag in the bottom of the body tag. You can change this by setting the
56`registrationStrategy` property in your `ember-cli-build.js` file. For example:
57
58```js
59var EmberApp = require('ember-cli/lib/broccoli/ember-app');
60
61module.exports = function(defaults) {
62 var app = new EmberApp(defaults, {
63 'ember-service-worker': {
64 registrationStrategy: 'inline'
65 }
66 });
67
68 return app.toTree();
69};
70```
71
72In the example the strategy is set to `inline`, this will write the contents of
73the registration script into the `index.html` file instead. If your registration
74script is small, this saves you an extra http request.
75
76In addition to `inline` strategy there is also the `async` option. This will add
77the async property to the injected script tag, which will make the loading
78behavior of the registration script async.
79
80### Disabling the Service Worker
81
82If you like to run or build your app without the Service Worker functionality
83you can set the `enabled` property to false in your `ember-cli-build.js` file or
84using the `SW_DISABLED` environment variable when executing the `ember`
85command.
86
87To disable the Service Worker in your `ember-cli-build.js` file:
88
89```js
90var EmberApp = require('ember-cli/lib/broccoli/ember-app');
91
92module.exports = function(defaults) {
93 var app = new EmberApp(defaults, {
94 'ember-service-worker': {
95 enabled: false
96 }
97 });
98
99 return app.toTree();
100};
101```
102
103To disable the Service Worker on the command line:
104
105```sh
106SW_DISABLED=true ember serve
107```
108
109### Customizing the sw.js Filename
110
111The common filename for the file that contains all of the service worker code is `sw.js`. If you require a different filename to be generated, you can specify this in the configuration.
112
113To change the filename used from the `sw.js` default, update your `ember-cli-build.js` file:
114
115```js
116var EmberApp = require('ember-cli/lib/broccoli/ember-app');
117
118module.exports = function(defaults) {
119 var app = new EmberApp(defaults, {
120 'ember-service-worker': {
121 serviceWorkerFilename: 'customfilename.js'
122 }
123 });
124
125 return app.toTree();
126};
127```