-
Notifications
You must be signed in to change notification settings - Fork 0
/
pwa-bootstrap.js
119 lines (100 loc) · 3.39 KB
/
pwa-bootstrap.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'use strict';
(async (currentScript) => {
if(location.protocol === 'http:') {
console.info("[INFO] The application is served over HTTP. PWA feature is not applicable.")
return
}
//
// Register installation related event listeners
//
window.addEventListener("beforeinstallprompt", (event) => {
console.info("[INFO] Calling beforeinstallprompt(event.platforms: %o) ...", event.platforms)
if(typeof InstallPrompt !== 'undefined') {
InstallPrompt.onBeforePrompt(event)
} else { // Perform the default interactions
event.userChoice.then((choice) => {
// either "accepted" or "dismissed"
console.info("[INFO] Default install prompt user choice: %s", choice.outcome)
})
}
})
if('onappinstalled' in window) {
window.addEventListener('appinstalled', () => {
console.info("[INFO] Current app (url: %s) is being installed.", HREF_BASE + '/manifest.json')
if(typeof State !== 'undefined') {
State.set({
'installationTime': Date.now()
})
}
if(typeof InstallPrompt !== 'undefined') {
InstallPrompt.onAfterPrompted()
}
})
} else {
console.info("[INFO] Event 'appinstalled' is not supported. Listening to the synthetic 'applaunched' event.")
window.addEventListener('applaunched', () => {
console.info("[INFO] Current app (url: %s) is launched for the first time after installation.", HREF_BASE + '/manifest.json')
if(typeof State !== 'undefined') {
State.set({
'appInitialLaunchTime': Date.now()
})
}
})
}
//
// Bootstrap logic
//
window.KNOWN_INTALLED = await isKnownInstalled()
if(KNOWN_INTALLED) {
console.info("[INFO] The application is known installed.")
return
}
if(shouldPolyfillIntallPrompt()) {
loadResources(HREF_BASE + '/../install/polyfill.js?' + versionOf(currentScript))
}
//
// Utility functions
//
async function isKnownInstalled() {
const appUrl = HREF_BASE + '/manifest.json'
if(navigator.getInstalledRelatedApps) {
// console.debug("[DEBUG] Checking installed related apps ...")
const apps = await navigator.getInstalledRelatedApps()
if(apps.some(app => app.url === appUrl)) {
// console.debug("[DEBUG] Current app (url: %s) is installed.", appUrl)
return true
}
} else {
console.info("[INFO] Checking installed related apps is not supported.")
}
if(typeof State !== 'undefined') {
await State.load()
const installationTime = State.get('installationTime')
if(installationTime) {
// console.debug("[DEBUG] Current app (url: %s) was installed at %o.", appUrl, new Date(installationTime))
return true
}
const appInitialLaunchTime = State.get('appInitialLaunchTime')
if(appInitialLaunchTime) {
// console.debug("[DEBUG] Current app (url: %s) was initially launched at %o after installation.", appUrl, new Date(appInitialLaunchTime))
return true
}
}
return false
}
function shouldPolyfillIntallPrompt() {
if('onbeforeinstallprompt' in window) return false
if(navigator.userAgent.match(/Windows/)) return false
return true
}
//
// Register service worker
//
if(navigator.serviceWorker) {
const servieWorkerUrl = `${APP_BASE}/service-worker.js?locale=${LOCALE}`
// console.debug("[DEBUG] Going to regiester the service worker at %s ...", servieWorkerUrl)
navigator.serviceWorker.register(servieWorkerUrl)
} else {
console.info("[INFO] navigator.serviceWorker is not supported.")
}
})(document.currentScript)