@cocreate/pwa 1.12.0 → 1.13.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,20 @@
1
+ # [1.13.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.12.0...v1.13.0) (2023-10-09)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * check data.status and clientId to prevent duplicate request ([3d2cff7](https://github.com/CoCreate-app/CoCreate-pwa/commit/3d2cff7e5d2eb6a83b13102039f66b218d770b23))
7
+ * handle organization, and lastModified ([d4c819b](https://github.com/CoCreate-app/CoCreate-pwa/commit/d4c819b88f4334d68e95e2b7d2c7dc01562b5de5))
8
+ * remove cache.js has been relocated CoCreate-cache ([224532a](https://github.com/CoCreate-app/CoCreate-pwa/commit/224532a01824f41adf5da7ced3ca83365c2507db))
9
+ * removed deleteCache, deleteFile from export default ([7710ef5](https://github.com/CoCreate-app/CoCreate-pwa/commit/7710ef50a9fbcab6a5f17f0fddbce9251bb420e0))
10
+
11
+
12
+ ### Features
13
+
14
+ * checkCache - check cache responses for last-modified and send.crud() read.object to see if the object has been modified. If crud returns objects add to cache. ([8c0eba3](https://github.com/CoCreate-app/CoCreate-pwa/commit/8c0eba35b2364b55d6e0e8fe896658491fad8ac3))
15
+ * checkCache message event will dispatch client the returnedFromCache object containg all the cache responses during page load. {[url]: {organization, lastModified}} ([60f179c](https://github.com/CoCreate-app/CoCreate-pwa/commit/60f179c99a25402152da5951b2cd1ecd78a0b73b))
16
+ * update cache using pathname and host ([8fb604b](https://github.com/CoCreate-app/CoCreate-pwa/commit/8fb604b820aac3c4a15ca81139edabe09f0de10c))
17
+
1
18
  # [1.12.0](https://github.com/CoCreate-app/CoCreate-pwa/compare/v1.11.0...v1.12.0) (2023-09-19)
2
19
 
3
20
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/pwa",
3
- "version": "1.12.0",
3
+ "version": "1.13.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",
package/src/index.js CHANGED
@@ -20,7 +20,6 @@
20
20
  // you must obtain a commercial license from CoCreate LLC.
21
21
  // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
22
22
 
23
- import { deleteCache, deleteFile } from './cache.js'
24
23
  import localStorage from '@cocreate/local-storage'
25
24
 
26
25
  const button = document.querySelector('[actions*="install"]');
@@ -126,4 +125,4 @@ if ('serviceWorker' in navigator) {
126
125
  }
127
126
  }
128
127
 
129
- export default { deleteCache, deleteFile, persistData }
128
+ export default { persistData }
@@ -25,6 +25,7 @@
25
25
  const cacheName = "dynamic-v2";
26
26
  let organization_id = ""
27
27
  let storage = true
28
+ let returnedFromCache = {};
28
29
 
29
30
  const queryString = self.location.search;
30
31
  const queryParams = new URLSearchParams(queryString);
@@ -47,6 +48,10 @@ self.addEventListener("fetch", async (e) => {
47
48
  .match(e.request)
48
49
  .then(async (cacheResponse) => {
49
50
  if (!navigator.onLine || !!cacheResponse && cacheType !== 'false') {
51
+ const organization = cacheResponse.headers.get('organization')
52
+ const lastModified = cacheResponse.headers.get('last-modified')
53
+
54
+ returnedFromCache[e.request.url] = { organization, lastModified }
50
55
  return cacheResponse;
51
56
  } else {
52
57
  const networkResponse = await fetch(e.request);
@@ -63,9 +68,12 @@ self.addEventListener("fetch", async (e) => {
63
68
 
64
69
  caches.open(cacheName).then((cache) => {
65
70
  if (networkResponse.status !== 206 && networkResponse.status !== 502) {
71
+ const networkModified = networkResponse.headers.get('last-modified');
72
+ // if (!networkModified) {
73
+ // networkResponse.headers.set('Last-Modified', new Date().toISOString());
74
+ // }
66
75
  cache.put(e.request, networkResponse);
67
76
  if (cacheType === 'reload' || cacheType === 'prompt') {
68
- const networkModified = networkResponse.headers.get('last-modified');
69
77
  const cacheModified = cacheResponse.headers.get('last-modified');
70
78
  if (networkModified !== cacheModified) {
71
79
  self.clients.matchAll().then((clients) => {
@@ -100,8 +108,56 @@ self.addEventListener("fetch", async (e) => {
100
108
  });
101
109
 
102
110
  self.addEventListener('message', function (event) {
103
- if (event.data === 'getOrganization')
104
- event.source.postMessage(organization_id);
111
+ if (event.data.action === 'getOrganization')
112
+ event.source.postMessage({ action: 'getOrganization', organization_id });
113
+ else if (event.data.action === 'checkCache') {
114
+ event.source.postMessage({ action: 'checkCache', returnedFromCache: { ...returnedFromCache } });
115
+ returnedFromCache = {}
116
+ }
117
+ });
118
+
119
+
120
+ self.addEventListener('push', (event) => {
121
+ const pushData = event.data.json(); // Assuming the push payload is JSON data
122
+
123
+ // Process the push notification data as needed
124
+ const { socketUrl, _id } = pushData;
125
+
126
+ // Establish a WebSocket connection
127
+ const socket = new WebSocket(socketUrl);
128
+
129
+ // Handle WebSocket events
130
+ socket.addEventListener('open', () => {
131
+ // The WebSocket connection is open, send the message to the server
132
+ socket.send(JSON.stringify({ method: 'read.object', array: 'files', object: { _id }, uid: '' }));
133
+ });
134
+
135
+ socket.addEventListener('message', (event) => {
136
+ // Handle messages received from the server over the WebSocket
137
+ const serverData = JSON.parse(event.data);
138
+ if (serverData.uid === uid) {
139
+ // TODO: add file to cache and close socket
140
+ }
141
+ });
142
+
143
+ socket.addEventListener('close', () => {
144
+ // Handle the WebSocket connection being closed
145
+ console.log('WebSocket connection closed.');
146
+ });
147
+
148
+ socket.addEventListener('error', (error) => {
149
+ // Handle WebSocket errors
150
+ console.error('WebSocket error:', error);
151
+ });
152
+
153
+ // Ensure that the service worker stays active until the WebSocket connection is closed
154
+ event.waitUntil(
155
+ new Promise((resolve) => {
156
+ socket.addEventListener('close', () => {
157
+ resolve();
158
+ });
159
+ })
160
+ );
105
161
  });
106
162
 
107
163
  // self.addEventListener('backgroundfetchsuccess', (event) => {
package/src/cache.js DELETED
@@ -1,63 +0,0 @@
1
- import socket from "@cocreate/socket-client"
2
-
3
- // const cacheName = "dynamic-v2"
4
-
5
- const cacheBtn = document.getElementById('cacheBtn');
6
-
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
- // Create a Response object with the file data
13
- const fileResponse = new Response(data.src, {
14
- headers: {
15
- 'Content-Type': data['content-type'],
16
- },
17
- });
18
-
19
- // Update the cache with the new version (or add it if not in the cache)
20
- const fileUrl = window.location.origin + data.pathname;
21
- cache.put(fileUrl, fileResponse).then(() => {
22
- console.log(`Updated cache for ${fileUrl}`);
23
- });
24
- });
25
- }
26
-
27
- function deleteCache(cacheName) {
28
- if ('serviceWorker' in navigator) {
29
- return caches.delete(cacheName);
30
- }
31
- }
32
-
33
- function deleteFile(cacheName, fileName) {
34
- if ('serviceWorker' in navigator) {
35
- caches.open(cacheName).then(function (cache) {
36
- cache.delete(fileName).then(function (response) {
37
- return response
38
- // console.log(cacheName, fileName, response)
39
- });
40
- })
41
- }
42
- }
43
-
44
- if (cacheBtn) {
45
- cacheBtn.addEventListener('click', function () {
46
- deleteFile('dynamic-v2', '/CoCreate-components/CoCreate-pwa/src/index.js');
47
- });
48
- }
49
-
50
- function fileChange(data) {
51
- if (!data.array === 'files' || !data.object)
52
- return
53
-
54
- for (let i = 0; i < data.object.length; i++)
55
- putFile('dynamic-v2', data.object[i])
56
- }
57
-
58
- socket.listen('create.object', (data) => fileChange(data));
59
- socket.listen('read.object', (data) => fileChange(data));
60
- socket.listen('update.object', (data) => fileChange(data));
61
- socket.listen('delete.object', (data) => fileChange(data));
62
-
63
- export { putFile, deleteFile, deleteCache }