@cocreate/pwa 1.14.0 → 1.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/package.json +2 -2
- package/src/index.js +92 -79
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
# [1.15.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.14.0...v1.15.0) (2023-11-12)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* bump dependencies for latest features ([f786f81](https://github.com/CoCreate-app/CoCreate-pwa/commit/f786f812e921d612b932ddaa7b8d3088ac98a410))
|
|
7
|
+
* relocated persit function out of init ([7725508](https://github.com/CoCreate-app/CoCreate-pwa/commit/7725508a023ece5b231387556849893b845ece73))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* observe actions=install ([d1f7aea](https://github.com/CoCreate-app/CoCreate-pwa/commit/d1f7aeae233cc8c3d531f66f982ef47e56280d92))
|
|
13
|
+
|
|
1
14
|
# [1.14.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.13.7...v1.14.0) (2023-11-09)
|
|
2
15
|
|
|
3
16
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/pwa",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.0",
|
|
4
4
|
"description": "A simple pwa component in vanilla javascript. Easily configured using HTML5 data-attributes and/or JavaScript API.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pwa",
|
|
@@ -59,6 +59,6 @@
|
|
|
59
59
|
"webpack-log": "^3.0.1"
|
|
60
60
|
},
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"@cocreate/local-storage": "^1.10.
|
|
62
|
+
"@cocreate/local-storage": "^1.10.10"
|
|
63
63
|
}
|
|
64
64
|
}
|
package/src/index.js
CHANGED
|
@@ -15,14 +15,16 @@
|
|
|
15
15
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
16
16
|
********************************************************************************/
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Commercial Licensing Information:
|
|
20
|
+
* For commercial use of this software without the copyleft provisions of the AGPLv3,
|
|
21
|
+
* you must obtain a commercial license from CoCreate LLC.
|
|
22
|
+
* For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
|
|
23
|
+
*/
|
|
22
24
|
|
|
25
|
+
import observer from '@cocreate/observer'
|
|
23
26
|
import localStorage from '@cocreate/local-storage'
|
|
24
27
|
|
|
25
|
-
const button = document.querySelector('[actions*="install"]');
|
|
26
28
|
|
|
27
29
|
/**
|
|
28
30
|
* Warn the page must be served over HTTPS
|
|
@@ -37,41 +39,87 @@ const button = document.querySelector('[actions*="install"]');
|
|
|
37
39
|
// requireHTTPS.classList.remove('hidden');
|
|
38
40
|
// }
|
|
39
41
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
// Stash the event so it can be triggered later.
|
|
43
|
-
window.deferredPrompt = event;
|
|
44
|
-
// Remove the 'hidden' class from the install button container
|
|
45
|
-
if (button)
|
|
46
|
-
button.removeAttribute('hidden');
|
|
47
|
-
});
|
|
42
|
+
function init() {
|
|
43
|
+
const button = document.querySelector('[actions*="install"]');
|
|
48
44
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
45
|
+
window.addEventListener('beforeinstallprompt', (event) => {
|
|
46
|
+
window.deferredPrompt = event;
|
|
47
|
+
if (button)
|
|
48
|
+
button.removeAttribute('hidden');
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
if (button) {
|
|
52
|
+
button.addEventListener('click', async () => {
|
|
53
|
+
// console.log('👍', 'installBtn-clicked');
|
|
54
|
+
const promptEvent = window.deferredPrompt;
|
|
55
|
+
if (!promptEvent) {
|
|
56
|
+
// The deferred prompt isn't available.
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
// Show the install prompt.
|
|
60
|
+
promptEvent.prompt();
|
|
61
|
+
// Log the result
|
|
62
|
+
const result = await promptEvent.userChoice;
|
|
63
|
+
console.log('👍', 'userChoice', result);
|
|
64
|
+
// Reset the deferred prompt variable, since
|
|
65
|
+
// prompt() can only be called once.
|
|
66
|
+
window.deferredPrompt = null;
|
|
67
|
+
// Hide the install button.
|
|
68
|
+
button.setAttribute('hidden', '');
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
window.addEventListener('appinstalled', (event) => {
|
|
73
|
+
// console.log('👍', 'appinstalled', event);
|
|
74
|
+
// Clear the deferredPrompt so it can be garbage collected
|
|
64
75
|
window.deferredPrompt = null;
|
|
65
|
-
// Hide the install button.
|
|
66
|
-
button.addAttribute('hidden', '');
|
|
67
76
|
});
|
|
68
|
-
}
|
|
69
77
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
78
|
+
if ('serviceWorker' in navigator) {
|
|
79
|
+
let workerPath
|
|
80
|
+
if (window.CoCreateConfig && window.CoCreateConfig.serviceWorker)
|
|
81
|
+
workerPath = window.CoCreateConfig.serviceWorker
|
|
82
|
+
else
|
|
83
|
+
workerPath = localStorage.getItem("serviceWorker") || '/service-worker.js'
|
|
84
|
+
|
|
85
|
+
localStorage.setItem("serviceWorker", workerPath);
|
|
86
|
+
|
|
87
|
+
let cache
|
|
88
|
+
if (window.CoCreateConfig && window.CoCreateConfig.cache)
|
|
89
|
+
cache = window.CoCreateConfig.cache
|
|
90
|
+
else
|
|
91
|
+
cache = localStorage.getItem("cache") || 'true'
|
|
92
|
+
|
|
93
|
+
localStorage.setItem("cache", cache);
|
|
94
|
+
workerPath += `?cache=${cache}`
|
|
95
|
+
|
|
96
|
+
let isPwa = true
|
|
97
|
+
if (workerPath) {
|
|
98
|
+
navigator.serviceWorker.getRegistrations().then(registrations => {
|
|
99
|
+
if (registrations.length === 0 || isPwa == true)
|
|
100
|
+
window.addEventListener("load", function () {
|
|
101
|
+
navigator.serviceWorker.getRegistration(workerPath).then(registration => {
|
|
102
|
+
if (registration && registration.active && registration.active.scriptURL.includes(workerPath)) {
|
|
103
|
+
console.log('Service Worker Active')
|
|
104
|
+
} else {
|
|
105
|
+
navigator.serviceWorker.register(workerPath)
|
|
106
|
+
.then(reg => {
|
|
107
|
+
reg.onupdatefound = () => {
|
|
108
|
+
const installingWorker = reg.installing;
|
|
109
|
+
installingWorker.onstatechange = () => {
|
|
110
|
+
// console.log('Service Worker', installingWorker.state);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
})
|
|
114
|
+
.catch(err => console.log('SW ERROR', err));
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
75
123
|
|
|
76
124
|
async function persistData() {
|
|
77
125
|
if (navigator.storage && navigator.storage.persist) {
|
|
@@ -80,49 +128,14 @@ async function persistData() {
|
|
|
80
128
|
}
|
|
81
129
|
}
|
|
82
130
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
localStorage.setItem("serviceWorker", workerPath);
|
|
91
|
-
|
|
92
|
-
let cache
|
|
93
|
-
if (window.CoCreateConfig && window.CoCreateConfig.cache)
|
|
94
|
-
cache = window.CoCreateConfig.cache
|
|
95
|
-
else
|
|
96
|
-
cache = localStorage.getItem("cache") || 'true'
|
|
97
|
-
|
|
98
|
-
localStorage.setItem("cache", cache);
|
|
99
|
-
workerPath += `?cache=${cache}`
|
|
100
|
-
|
|
101
|
-
let isPwa = true
|
|
102
|
-
if (workerPath) {
|
|
103
|
-
navigator.serviceWorker.getRegistrations().then(registrations => {
|
|
104
|
-
if (registrations.length === 0 || isPwa == true)
|
|
105
|
-
window.addEventListener("load", function () {
|
|
106
|
-
navigator.serviceWorker.getRegistration(workerPath).then(registration => {
|
|
107
|
-
if (registration && registration.active && registration.active.scriptURL.includes(workerPath)) {
|
|
108
|
-
console.log('Service Worker Active')
|
|
109
|
-
} else {
|
|
110
|
-
navigator.serviceWorker.register(workerPath)
|
|
111
|
-
.then(reg => {
|
|
112
|
-
reg.onupdatefound = () => {
|
|
113
|
-
const installingWorker = reg.installing;
|
|
114
|
-
installingWorker.onstatechange = () => {
|
|
115
|
-
// console.log('Service Worker', installingWorker.state);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
})
|
|
119
|
-
.catch(err => console.log('SW ERROR', err));
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
|
|
131
|
+
observer.init({
|
|
132
|
+
name: 'initPwa',
|
|
133
|
+
observe: ['addedNodes'],
|
|
134
|
+
target: '[actions="install"]',
|
|
135
|
+
callback: (mutation) => {
|
|
136
|
+
init()
|
|
125
137
|
}
|
|
126
|
-
}
|
|
138
|
+
});
|
|
139
|
+
|
|
127
140
|
|
|
128
141
|
export default { persistData }
|