-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlux-sw.js
93 lines (72 loc) · 2.28 KB
/
lux-sw.js
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import xin from 'xin';
import 'file-loader?name=./sw/[name].[ext]!./sw/service-worker.js';
import 'file-loader?name=./sw/sw-toolbox/[name].[ext]!sw-toolbox/sw-toolbox.js';
import 'file-loader?name=./sw/sw-toolbox/[name].[ext]!sw-toolbox/sw-toolbox.js.map';
function isServiceWorkerSupported () {
return 'serviceWorker' in navigator;
}
class LuxSw extends xin.Component {
get props () {
return Object.assign({}, super.props, {
href: {
type: String,
value: 'sw-import.js',
},
skipWaiting: {
type: Boolean,
value: false,
},
clientsClaim: {
type: Boolean,
value: false,
},
debug: {
type: Boolean,
value: false,
},
defaultCacheStrategy: {
type: String,
value: 'networkFirst',
},
});
}
async attached () {
super.attached();
if (!isServiceWorkerSupported()) {
console.error('Service worker is not supported!');
return;
}
try {
let params = {
version: '1.0',
skipWaiting: this.skipWaiting,
clientsClaim: this.clientsClaim,
debug: this.debug,
defaultCacheStrategy: this.defaultCacheStrategy,
};
let baseUrl = new window.URL(this.href, window.location.href);
let childParams = await Promise.all([].map.call(this.children, el => el._getParameters(baseUrl)));
childParams.forEach(param => {
Object.keys(param).forEach(key => {
params[key] = (params[key] || []).concat(param[key]);
});
});
let url = `${this.href}?${this._serializeUrlParams(params)}`;
let registration = window.sw = await navigator.serviceWorker.register(url);
console.info(`Service worker registration successful with scope: ${registration.scope}`);
} catch (err) {
console.error(`${err.name}: ${err.message}`);
console.warn(`
Maybe, a valid service worker script not found "${this.href}"
Create new file "${this.href}" and copy paste lines below,
\`\`\`javascript
importScripts('/sw/service-worker.js');
\`\`\``);
}
}
_serializeUrlParams (params) {
return Object.keys(params).sort().map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`).join('&');
}
}
xin.define('lux-sw', LuxSw);
export default LuxSw;