Versioning
The service worker in the browser is updated when the sw.js
file has changed.
The problem is that not every update to your app will result in a change to the
sw.js
file. Therefore ember-service-worker has a few configuration options to
force a change to the sw.js
file when building. This is done by setting the
versionStrategy
property in your ember-cli-build.js
file. An example:
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
'ember-service-worker': {
versionStrategy: 'every-build'
}
});
return app.toTree();
};
In this example, the strategy is set to every-build
. This is a very safe
strategy as it will force a change to your sw.js
file every time you do a
new build. This is the default strategy for Ember Service Worker.
There are two other options:
-
project-revision
– This will create a checksum based on the contents of your application folder using the hash-for-dep package and use that to version thesw.js
file. -
project-version
– This will use the version number specified in yourpackage.json
file to version thesw.js
file.
Registration
There are various ways to inject the service worker registration script. By
default the registration file sw-registration.js
is loaded using a simple
script tag in the bottom of the body element. You can change this by setting the
registrationStrategy
property in your ember-cli-build.js
file:
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
'ember-service-worker': {
registrationStrategy: 'inline'
}
});
return app.toTree();
};
In the example the strategy is set to inline
. This will write the contents of
the registration script into the index.html
file instead. If your registration
script is small, this saves you an extra http request.
In addition to inline
strategy there is also the async
option. This will add
the async property to the injected script tag, which will make the loading
behavior of the registration script asynchronous.
Disabling the Service Worker
If you like to run or build your app without the Service Worker functionality
you can set the enabled
property to false
in your ember-cli-build.js
file or
use the SW_DISABLED
environment variable when executing the ember
command.
To disable the Service Worker in your ember-cli-build.js
file:
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
'ember-service-worker': {
enabled: false
}
});
return app.toTree();
};
To disable the Service Worker on the command line:
SW_DISABLED=true ember serve
Customizing the sw.js Filename
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.
To change the filename used from the default sw.js
, update your ember-cli-build.js
file:
var EmberApp = require('ember-cli/lib/broccoli/ember-app');
module.exports = function(defaults) {
var app = new EmberApp(defaults, {
'ember-service-worker': {
serviceWorkerFilename: 'customfilename.js'
}
});
return app.toTree();
};