1 | ---
|
2 | layout: doc_page
|
3 | title: Configuration
|
4 | order: 3
|
5 | sections:
|
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 |
|
18 | The service worker in the browser is updated when the `sw.js` file has changed.
|
19 | The 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
|
21 | force 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
|
25 | var EmberApp = require('ember-cli/lib/broccoli/ember-app');
|
26 |
|
27 | module.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 |
|
38 | In the example the strategy is set to `every-build`, this is a very safe
|
39 | strategy, because it will force a change to your `sw.js` file everytime you do a
|
40 | new build. This is also the strategy what Ember Service Worker defaults to.
|
41 |
|
42 | There are two other options to this: `project-revision` and `project-version`.
|
43 |
|
44 | The `project-revision` option will create a checksum based on the contents of
|
45 | your application folder using the [hash-for-dep](https://github.com/stefanpenner/hash-for-dep)
|
46 | package and use that to version the `sw.js` file.
|
47 |
|
48 | The `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 |
|
53 | There are various ways to inject the service worker registration script. By
|
54 | default the registration file `sw-registration.js` is loaded using a simple
|
55 | script 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
|
59 | var EmberApp = require('ember-cli/lib/broccoli/ember-app');
|
60 |
|
61 | module.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 |
|
72 | In the example the strategy is set to `inline`, this will write the contents of
|
73 | the registration script into the `index.html` file instead. If your registration
|
74 | script is small, this saves you an extra http request.
|
75 |
|
76 | In addition to `inline` strategy there is also the `async` option. This will add
|
77 | the async property to the injected script tag, which will make the loading
|
78 | behavior of the registration script async.
|
79 |
|
80 | ### Disabling the Service Worker
|
81 |
|
82 | If you like to run or build your app without the Service Worker functionality
|
83 | you can set the `enabled` property to false in your `ember-cli-build.js` file or
|
84 | using the `SW_DISABLED` environment variable when executing the `ember`
|
85 | command.
|
86 |
|
87 | To disable the Service Worker in your `ember-cli-build.js` file:
|
88 |
|
89 | ```js
|
90 | var EmberApp = require('ember-cli/lib/broccoli/ember-app');
|
91 |
|
92 | module.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 |
|
103 | To disable the Service Worker on the command line:
|
104 |
|
105 | ```sh
|
106 | SW_DISABLED=true ember serve
|
107 | ```
|
108 |
|
109 | ### Customizing the sw.js Filename
|
110 |
|
111 | The 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 |
|
113 | To change the filename used from the `sw.js` default, update your `ember-cli-build.js` file:
|
114 |
|
115 | ```js
|
116 | var EmberApp = require('ember-cli/lib/broccoli/ember-app');
|
117 |
|
118 | module.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 | ```
|