@cocreate/pwa 1.14.0 → 1.16.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 CHANGED
@@ -1,3 +1,28 @@
1
+ # [1.16.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.15.0...v1.16.0) (2023-11-19)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * init() ([6b78a70](https://github.com/CoCreate-app/CoCreate-pwa/commit/6b78a70bb02bc4349886ae1ed29400b679639e6e))
7
+
8
+
9
+ ### Features
10
+
11
+ * update dependecies for th latest features and bug fixes ([7ab7547](https://github.com/CoCreate-app/CoCreate-pwa/commit/7ab754764df6bd0e3c3cee054587d27c71e7193b))
12
+
13
+ # [1.15.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.14.0...v1.15.0) (2023-11-12)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * bump dependencies for latest features ([f786f81](https://github.com/CoCreate-app/CoCreate-pwa/commit/f786f812e921d612b932ddaa7b8d3088ac98a410))
19
+ * relocated persit function out of init ([7725508](https://github.com/CoCreate-app/CoCreate-pwa/commit/7725508a023ece5b231387556849893b845ece73))
20
+
21
+
22
+ ### Features
23
+
24
+ * observe actions=install ([d1f7aea](https://github.com/CoCreate-app/CoCreate-pwa/commit/d1f7aeae233cc8c3d531f66f982ef47e56280d92))
25
+
1
26
  # [1.14.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.13.7...v1.14.0) (2023-11-09)
2
27
 
3
28
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/pwa",
3
- "version": "1.14.0",
3
+ "version": "1.16.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.8"
62
+ "@cocreate/local-storage": "^1.10.11"
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
- // Commercial Licensing Information:
19
- // For commercial use of this software without the copyleft provisions of the AGPLv3,
20
- // you must obtain a commercial license from CoCreate LLC.
21
- // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
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,89 @@ const button = document.querySelector('[actions*="install"]');
37
39
  // requireHTTPS.classList.remove('hidden');
38
40
  // }
39
41
 
40
- window.addEventListener('beforeinstallprompt', (event) => {
41
- // console.log('👍', 'beforeinstallprompt', event);
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
- if (button) {
50
- button.addEventListener('click', async () => {
51
- // console.log('👍', 'installBtn-clicked');
52
- const promptEvent = window.deferredPrompt;
53
- if (!promptEvent) {
54
- // The deferred prompt isn't available.
55
- return;
56
- }
57
- // Show the install prompt.
58
- promptEvent.prompt();
59
- // Log the result
60
- const result = await promptEvent.userChoice;
61
- console.log('👍', 'userChoice', result);
62
- // Reset the deferred prompt variable, since
63
- // prompt() can only be called once.
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
  });
77
+
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
+ }
68
122
  }
69
123
 
70
- window.addEventListener('appinstalled', (event) => {
71
- // console.log('👍', 'appinstalled', event);
72
- // Clear the deferredPrompt so it can be garbage collected
73
- window.deferredPrompt = null;
74
- });
124
+ init()
75
125
 
76
126
  async function persistData() {
77
127
  if (navigator.storage && navigator.storage.persist) {
@@ -80,49 +130,14 @@ async function persistData() {
80
130
  }
81
131
  }
82
132
 
83
- if ('serviceWorker' in navigator) {
84
- let workerPath
85
- if (window.CoCreateConfig && window.CoCreateConfig.serviceWorker)
86
- workerPath = window.CoCreateConfig.serviceWorker
87
- else
88
- workerPath = localStorage.getItem("serviceWorker") || '/service-worker.js'
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
-
133
+ observer.init({
134
+ name: 'initPwa',
135
+ observe: ['addedNodes'],
136
+ target: '[actions="install"]',
137
+ callback: (mutation) => {
138
+ init()
125
139
  }
126
- }
140
+ });
141
+
127
142
 
128
143
  export default { persistData }