-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsw.js
More file actions
54 lines (45 loc) · 1.47 KB
/
sw.js
File metadata and controls
54 lines (45 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { debug } from '@ember/debug';
import { get } from '@ember/object';
function getWithDefault(obj, path, def) {
const value = get(obj, path);
return typeof value !== 'undefined' ? value : def;
}
export function getConfig(appInstance) {
const config = appInstance.resolveRegistration('config:environment');
const isProdBuild = config.environment === 'production';
return {
isEnabled: getWithDefault(config, 'ember-cli-workbox.enabled', isProdBuild),
debugAddon: getWithDefault(config, 'ember-cli-workbox.debug', !isProdBuild),
swDestFile: getWithDefault(config, 'ember-cli-workbox.swDest', 'sw.js'),
autoRegister: getWithDefault(
config,
'ember-cli-workbox.autoRegister',
true
),
};
}
export function initialize(appInstance) {
const swService = appInstance.lookup('service:service-worker');
const { isEnabled, debugAddon, swDestFile } = getConfig(appInstance);
swService.set('debug', debugAddon);
// first checks whether the browser supports service workers
if (swService.isSupported) {
// Load and register pre-caching Service Worker
if (isEnabled) {
swService.register(swDestFile);
} else {
swService.unregisterAll();
}
} else {
debug('Service workers are not supported in this browser.');
}
}
export default {
name: 'ember-cli-workbox',
initialize(appInstance) {
const { autoRegister } = getConfig(appInstance);
if (autoRegister) {
initialize(appInstance);
}
},
};