@cocreate/pwa 1.9.2 → 1.10.1

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,25 @@
1
+ ## [1.10.1](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.10.0...v1.10.1) (2023-09-18)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Add path and pathname ([1eaa09c](https://github.com/CoCreate-app/CoCreate-pwa/commit/1eaa09c1bb7dab55868e02ac4b994b1464a4f38e))
7
+ * bug in cache ([35229bc](https://github.com/CoCreate-app/CoCreate-pwa/commit/35229bc521e867ece5bc8140d9cdfcde0587220b))
8
+ * set cache name ([35fcedd](https://github.com/CoCreate-app/CoCreate-pwa/commit/35fceddf099cb74a028a8ae2448e6d6ee4cb9ee4))
9
+ * Update CoCreate depndencies to latest versions ([42fca0a](https://github.com/CoCreate-app/CoCreate-pwa/commit/42fca0a8852974cf5aec912e00c1b2d8dc7ff0f6))
10
+
11
+ # [1.10.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.9.2...v1.10.0) (2023-09-17)
12
+
13
+
14
+ ### Bug Fixes
15
+
16
+ * object.$filter ([1e73390](https://github.com/CoCreate-app/CoCreate-pwa/commit/1e73390bc97f840c54fb2043624bbeda622b63d9))
17
+
18
+
19
+ ### Features
20
+
21
+ * cache updates dynaically using crud and sockert listener ([17e1646](https://github.com/CoCreate-app/CoCreate-pwa/commit/17e1646949ec49efc487e8e3cbcd565e5aa0c6b9))
22
+
1
23
  ## [1.9.2](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.9.1...v1.9.2) (2023-09-07)
2
24
 
3
25
 
@@ -1,25 +1,24 @@
1
- module.exports = {
2
- "organization_id": "",
3
- "key": "",
4
- "host": "",
5
- "sources": [
6
- {
7
- "array": "files",
8
- "object": {
9
- "_id": "637ca5ac50234ef1671ce327",
10
- "name": "index.html",
11
- "path": "/docs/pwa/index.html",
12
- "src": "{{./docs/index.html}}",
13
- "host": [
14
- "cocreate.app",
15
- "general.cocreate.app"
16
- ],
17
- "directory": "/docs/pwa",
18
- "parentDirectory": "{{parentDirectory}}",
19
- "content-type": "{{content-type}}",
20
- "public": "true",
21
- "website_id": "614298c2829b690010a5c031"
22
- }
23
- }
24
- ]
25
- }
1
+ module.exports = {
2
+ "organization_id": "",
3
+ "key": "",
4
+ "host": "",
5
+ "sources": [
6
+ {
7
+ "array": "files",
8
+ "object": {
9
+ "_id": "637ca5ac50234ef1671ce327",
10
+ "name": "index.html",
11
+ "path": "/docs/pwa",
12
+ "pathname": "/docs/pwa/index.html",
13
+ "src": "{{./docs/index.html}}",
14
+ "host": [
15
+ "cocreate.app",
16
+ "general.cocreate.app"
17
+ ],
18
+ "directory": "pwa",
19
+ "content-type": "{{content-type}}",
20
+ "public": "true"
21
+ }
22
+ }
23
+ ]
24
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/pwa",
3
- "version": "1.9.2",
3
+ "version": "1.10.1",
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.0"
62
+ "@cocreate/local-storage": "^1.10.1"
63
63
  }
64
64
  }
package/src/cache.js CHANGED
@@ -1,28 +1,37 @@
1
+ import socket from "@cocreate/socket-client"
2
+
3
+ // const cacheName = "dynamic-v2"
4
+
1
5
  const cacheBtn = document.getElementById('cacheBtn');
2
6
 
3
- function deleteCache(cache) {
7
+ function putFile(cacheName, data) {
8
+ if (!data.name || !data.path || !data.src || !data['content-type'])
9
+ return
10
+ // Open the cache and update it with the new file data
11
+ caches.open(cacheName).then((cache) => {
12
+ // Construct the URL for the file based on the fileObject's path and src
13
+ const fileUrl = new URL(data.name, window.location.origin + data.path).toString();
14
+
15
+ // Create a Response object with the file data
16
+ const fileResponse = new Response(data.src, {
17
+ headers: {
18
+ 'Content-Type': data['content-type'],
19
+ },
20
+ });
21
+
22
+ // Update the cache with the new version (or add it if not in the cache)
23
+ cache.put(fileUrl, fileResponse).then(() => {
24
+ console.log(`Updated cache for ${fileUrl}`);
25
+ });
26
+ });
27
+ }
28
+
29
+ function deleteCache(cacheName) {
4
30
  if ('serviceWorker' in navigator) {
5
- return caches.delete(key);
6
-
7
- // caches.keys().then(function(cacheNames) {
8
- // cacheNames.forEach(function(cacheName) {
9
- // if (cacheName == cache)
10
- // caches.delete(cacheName);
11
- // });
12
- // });
31
+ return caches.delete(cacheName);
13
32
  }
14
33
  }
15
34
 
16
- // function cleanCache(cacheName, numeroItems) {
17
- // caches.open(cacheName).then((cache) => {
18
- // return cache.keys().then((keys) => {
19
- // if (keys.length > numeroItems) {
20
- // cache.delete(keys[0]).then(cleanCache(cacheName, numeroItems));
21
- // }
22
- // });
23
- // });
24
- // }
25
-
26
35
  function deleteFile(cacheName, fileName) {
27
36
  if ('serviceWorker' in navigator) {
28
37
  caches.open(cacheName).then(function (cache) {
@@ -34,11 +43,23 @@ function deleteFile(cacheName, fileName) {
34
43
  }
35
44
  }
36
45
 
37
-
38
46
  if (cacheBtn) {
39
47
  cacheBtn.addEventListener('click', function () {
40
48
  deleteFile('dynamic-v2', '/CoCreate-components/CoCreate-pwa/src/index.js');
41
49
  });
42
50
  }
43
51
 
44
- export { deleteCache, deleteFile }
52
+ function fileChange(data) {
53
+ if (!data.array === 'files' || !data.object)
54
+ return
55
+
56
+ for (let i = 0; i < data.object.length; i++)
57
+ putFile('dynamic-v2', data.object[i])
58
+ }
59
+
60
+ socket.listen('create.object', (data) => fileChange(data));
61
+ socket.listen('read.object', (data) => fileChange(data));
62
+ socket.listen('update.object', (data) => fileChange(data));
63
+ socket.listen('delete.object', (data) => fileChange(data));
64
+
65
+ export { putFile, deleteFile, deleteCache }
@@ -15,10 +15,12 @@
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
 
23
25
  const cacheName = "dynamic-v2";
24
26
  let organization_id = ""
@@ -40,46 +42,11 @@ self.addEventListener("activate", async (e) => {
40
42
  self.addEventListener("fetch", async (e) => {
41
43
  if (!(e.request.url.indexOf('http') === 0) || e.request.method === 'POST') return;
42
44
 
43
- if (!storage) {
44
- console.log('storage:', storage);
45
-
46
- const url = new URL(e.request.url);
47
- const hostname = url.host;
48
- const pathname = url.pathname;
49
-
50
- let file = await readFile({
51
- database: '5ff747727005da1c272740ab',
52
- array: 'files',
53
- filter: {
54
- path: hostname,
55
- host: '*'
56
- }
57
- });
58
-
59
- if (file) {
60
- console.log('file:', file);
61
- console.log('Pathname:', pathname);
62
- console.log('file', file)
63
- }
64
-
65
- if (file && file.object && file.object[0]) {
66
- file = file.object[0]
67
-
68
- const modifiedRequest = new Request(e.request, {
69
- headers: new Headers({
70
- 'File-Content': file.src,
71
- 'Content-Type': file['content-type']
72
- })
73
- });
74
- e.request = modifiedRequest
75
- }
76
- }
77
-
78
45
  e.respondWith(
79
46
  caches
80
47
  .match(e.request)
81
48
  .then(async (cacheResponse) => {
82
- if (!navigator.onLine && !!cacheResponse && cacheType !== 'false') {
49
+ if (!navigator.onLine || !!cacheResponse && cacheType !== 'false') {
83
50
  return cacheResponse;
84
51
  } else {
85
52
  const networkResponse = await fetch(e.request);
@@ -91,10 +58,6 @@ self.addEventListener("fetch", async (e) => {
91
58
  if (storageHeader)
92
59
  storage = storageHeader
93
60
 
94
- if (!cacheResponse || cacheType === 'false' || cacheType === 'offline') {
95
- return networkResponse.clone();
96
- }
97
-
98
61
  if (cacheType && cacheType !== 'false') {
99
62
  console.log('caching')
100
63
 
@@ -119,6 +82,10 @@ self.addEventListener("fetch", async (e) => {
119
82
  });
120
83
  }
121
84
 
85
+ if (!cacheResponse || cacheType === 'false' || cacheType === 'offline') {
86
+ return networkResponse.clone();
87
+ }
88
+
122
89
  }
123
90
 
124
91
  if (!!cacheResponse && cacheType !== 'false' && cacheType !== 'offline') {
@@ -132,56 +99,6 @@ self.addEventListener("fetch", async (e) => {
132
99
  );
133
100
  });
134
101
 
135
-
136
- function readFile(data) {
137
- return new Promise((resolve) => {
138
- const request = indexedDB.open(data.database);
139
-
140
- request.onsuccess = function () {
141
- const db = request.result
142
- console.log('readFile', data)
143
- try {
144
- const transaction = db.transaction(data.array, "readonly");
145
- const objectStore = transaction.objectStore(data.array);
146
- const cursorRequest = objectStore.openCursor();
147
-
148
- cursorRequest.onsuccess = function () {
149
- const cursor = cursorRequest.result;
150
- if (cursor) {
151
- const file = cursor.value;
152
- if (!file || !file.src || !file.path || !file.host)
153
- cursor.continue();
154
- else if (file.path !== data.filter.path || !file.host.includes(data.filter.host))
155
- cursor.continue();
156
- else {
157
- db.close()
158
- return resolve(file);
159
- }
160
- } else {
161
- // Resolve the Promise when cursor is finished
162
- db.close()
163
- resolve();
164
- }
165
- };
166
-
167
- cursorRequest.onerror = function () {
168
- console.error("Cursor error:", cursorRequest.error);
169
- db.close()
170
- resolve();
171
- };
172
- } catch (error) {
173
-
174
- }
175
-
176
- };
177
-
178
- request.onerror = function (event) {
179
- reject(event.target.error);
180
- };
181
-
182
- })
183
- }
184
-
185
102
  self.addEventListener('message', function (event) {
186
103
  if (event.data === 'getOrganization')
187
104
  event.source.postMessage(organization_id);