@cocreate/pwa 1.25.0 → 1.27.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/CONTRIBUTING.md CHANGED
@@ -13,7 +13,7 @@ Fork the [project on Github](https://github.com/CoCreate-app/CoCreate-pwa) and c
13
13
  ```
14
14
  git pwa https://github.com/contributor/CoCreate-pwa.git
15
15
  cd CoCreate-pwa
16
- git remote add upstream https://github.com/CoCreate-app/CoCreate-pwa.git
16
+ git remote add upstream https://gitlab.com/cocreatejs/CoCreate-pwa.git
17
17
  ```
18
18
 
19
19
  ## Create a Topic Branch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/pwa",
3
- "version": "1.25.0",
3
+ "version": "1.27.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",
@@ -23,7 +23,7 @@
23
23
  },
24
24
  "repository": {
25
25
  "type": "git",
26
- "url": "git+https://github.com/CoCreate-app/CoCreate-pwa.git"
26
+ "url": "git+https://gitlab.com/cocreatejs/CoCreate-pwa.git"
27
27
  },
28
28
  "author": "CoCreate LLC",
29
29
  "license": "AGPL-3.0",
@@ -37,10 +37,10 @@
37
37
  },
38
38
  "main": "./src/index.js",
39
39
  "dependencies": {
40
- "@cocreate/local-storage": "^1.19.0"
40
+ "@cocreate/local-storage": "^1.20.0"
41
41
  },
42
42
  "devDependencies": {
43
- "@cocreate/webpack": "^1.8.0"
43
+ "@cocreate/webpack": "^1.9.0"
44
44
  },
45
45
  "allowScripts": {
46
46
  "@cocreate/actions@1.21.4": true,
@@ -32,6 +32,51 @@ const queryString = self.location.search;
32
32
  const queryParams = new URLSearchParams(queryString);
33
33
  let cacheType = queryParams.get('cache');
34
34
 
35
+ // Private responses use the browser HTTP cache, not the worker's shared asset cache.
36
+ function canUseAssetCache(response) {
37
+ if (!response) return false;
38
+ const policy = response.headers.get("Cache-Control") || "";
39
+ if (!/(?:^|,)\s*public\s*(?:,|$)/i.test(policy) ||
40
+ /(?:^|,)\s*(?:private|no-cache|no-store|must-revalidate|proxy-revalidate)\b/i.test(policy)) return false;
41
+ const age = policy.match(/(?:^|,)\s*max-age=(\d+)\s*(?:,|$)/i);
42
+ const date = Date.parse(response.headers.get("Date"));
43
+ const elapsed = (Date.now() - date) / 1000 + Number(response.headers.get("Age") || 0);
44
+ return age && Number.isFinite(elapsed) && elapsed < Number(age[1]);
45
+ }
46
+
47
+ // Ask the requesting page for its current Config token; do not persist a second copy.
48
+ async function applySessionToken(event, request = event.request) {
49
+ if (new URL(request.url).origin !== self.location.origin) return request;
50
+ if (request.headers.has("Authorization")) return request;
51
+ const client = event.clientId && await self.clients.get(event.clientId);
52
+ if (!client) return request;
53
+
54
+ const token = await new Promise((resolve) => {
55
+ const channel = new MessageChannel();
56
+ const finish = (value) => {
57
+ clearTimeout(timeout);
58
+ channel.port1.close();
59
+ channel.port2.close();
60
+ resolve(value);
61
+ };
62
+ const timeout = setTimeout(() => finish(null), 1000);
63
+ channel.port1.onmessage = (message) => finish(message.data?.token);
64
+ try {
65
+ client.postMessage({ action: "getSessionToken" }, [channel.port2]);
66
+ } catch {
67
+ finish(null);
68
+ }
69
+ });
70
+ if (typeof token !== "string" || !token) return request;
71
+ const headers = new Headers(request.headers);
72
+ headers.set("Authorization", `Bearer ${token}`);
73
+ return new Request(request, {
74
+ headers,
75
+ // Native image requests use no-cors, which strips Authorization.
76
+ ...(request.mode === "no-cors" ? { mode: "same-origin" } : {})
77
+ });
78
+ }
79
+
35
80
  self.addEventListener("install", (e) => {
36
81
  console.log('Service Worker Installing')
37
82
  self.skipWaiting();
@@ -52,12 +97,19 @@ self.addEventListener('activate', event => {
52
97
  });
53
98
 
54
99
  self.addEventListener("fetch", async (e) => {
55
- if (!(e.request.url.indexOf('http') === 0) || e.request.method === 'POST') return;
100
+ if (!(e.request.url.indexOf('http') === 0)) return;
101
+ if (e.request.method !== 'GET') {
102
+ e.respondWith(applySessionToken(e).then(request => fetch(request)));
103
+ return;
104
+ }
56
105
 
57
106
  e.respondWith(
58
107
  caches
59
108
  .match(e.request)
60
109
  .then(async (cacheResponse) => {
110
+ if (!canUseAssetCache(cacheResponse)) cacheResponse = null;
111
+ const request = await applySessionToken(e);
112
+ if (request.headers.has("Authorization")) return fetch(request);
61
113
  let fetchedOn
62
114
  if (cacheResponse && cacheType !== 'false') {
63
115
  fetchedOn = cacheResponse.headers.get('fetched-on')
@@ -66,7 +118,7 @@ self.addEventListener("fetch", async (e) => {
66
118
  }
67
119
 
68
120
 
69
- if (!navigator.onLine || !!cacheResponse && cacheType !== 'false' && (!fetchedOn || fetchedOn > updatedOn)) {
121
+ if ((!navigator.onLine && cacheResponse) || !!cacheResponse && cacheType !== 'false' && (!fetchedOn || fetchedOn > updatedOn)) {
70
122
  const organization = cacheResponse.headers.get('organization')
71
123
  console.log('servering cache fetchOn greater', fetchedOn > updatedOn, organization)
72
124
  const lastModified = cacheResponse.headers.get('last-modified')
@@ -76,6 +128,7 @@ self.addEventListener("fetch", async (e) => {
76
128
  console.log('fetching fetchOn less', fetchedOn > updatedOn)
77
129
 
78
130
  const networkResponse = await fetch(e.request);
131
+ if (!canUseAssetCache(networkResponse)) return networkResponse;
79
132
 
80
133
  if (!organization_id)
81
134
  organization_id = networkResponse.headers.get('organization')