create-nexa-app 1.0.19 → 1.0.22
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/package.json
CHANGED
package/template/public/sw.js
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
// public/sw.js
|
|
2
|
+
|
|
3
|
+
const CACHE_NAME = "nexa-cache-v2";
|
|
2
4
|
|
|
3
5
|
const ASSETS_TO_CACHE = ["/", "/manifest.json", "/nexa.svg"];
|
|
4
6
|
|
|
7
|
+
// =======================
|
|
8
|
+
// INSTALL
|
|
9
|
+
// =======================
|
|
5
10
|
self.addEventListener("install", (event) => {
|
|
6
11
|
event.waitUntil(
|
|
7
12
|
caches.open(CACHE_NAME).then((cache) => {
|
|
8
13
|
return cache.addAll(ASSETS_TO_CACHE);
|
|
9
14
|
}),
|
|
10
15
|
);
|
|
16
|
+
|
|
11
17
|
self.skipWaiting();
|
|
12
18
|
});
|
|
13
19
|
|
|
20
|
+
// =======================
|
|
21
|
+
// ACTIVATE (cleanup old caches)
|
|
22
|
+
// =======================
|
|
14
23
|
self.addEventListener("activate", (event) => {
|
|
15
24
|
event.waitUntil(
|
|
16
25
|
caches.keys().then((keys) =>
|
|
@@ -23,31 +32,88 @@ self.addEventListener("activate", (event) => {
|
|
|
23
32
|
),
|
|
24
33
|
),
|
|
25
34
|
);
|
|
35
|
+
|
|
26
36
|
self.clients.claim();
|
|
27
37
|
});
|
|
28
38
|
|
|
39
|
+
// =======================
|
|
40
|
+
// FETCH (smart caching)
|
|
41
|
+
// =======================
|
|
29
42
|
self.addEventListener("fetch", (event) => {
|
|
30
|
-
|
|
43
|
+
const request = event.request;
|
|
44
|
+
const url = new URL(request.url);
|
|
31
45
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
// 🚫 Only handle GET requests
|
|
47
|
+
if (request.method !== "GET") return;
|
|
48
|
+
|
|
49
|
+
// 🚫 NEVER cache API calls
|
|
50
|
+
// if (url.hostname.includes("execute-api")) {
|
|
51
|
+
// event.respondWith(fetch(request));
|
|
52
|
+
// return;
|
|
53
|
+
// }
|
|
37
54
|
|
|
38
|
-
|
|
55
|
+
// 🚫 NEVER cache API calls (anything not same origin: like "execute-api")
|
|
56
|
+
/**
|
|
57
|
+
* 🧠 Why this matters
|
|
58
|
+
* execute-api → AWS only ❌
|
|
59
|
+
origin check → works for:
|
|
60
|
+
- AWS
|
|
61
|
+
- Firebase
|
|
62
|
+
- Supabase
|
|
63
|
+
- custom backend
|
|
64
|
+
- localhost dev
|
|
65
|
+
|
|
66
|
+
🎯 Final CLI-ready rule set
|
|
67
|
+
|
|
68
|
+
✔ Same-origin files → cache intelligently
|
|
69
|
+
✔ Cross-origin (APIs/CDNs) → never cache
|
|
70
|
+
✔ App shell → network-first
|
|
71
|
+
✔ Assets → cache-first
|
|
72
|
+
✔ versioned cache
|
|
73
|
+
*/
|
|
74
|
+
if (url.origin !== self.location.origin) {
|
|
75
|
+
event.respondWith(fetch(request));
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 🔥 APP SHELL → NETWORK FIRST (fixes stale UI issue)
|
|
80
|
+
if (
|
|
81
|
+
request.mode === "navigate" ||
|
|
82
|
+
url.pathname.endsWith(".js") ||
|
|
83
|
+
url.pathname.endsWith(".css")
|
|
84
|
+
) {
|
|
85
|
+
event.respondWith(
|
|
86
|
+
fetch(request)
|
|
39
87
|
.then((networkResponse) => {
|
|
40
|
-
const
|
|
88
|
+
const clone = networkResponse.clone();
|
|
89
|
+
|
|
90
|
+
caches.open(CACHE_NAME).then((cache) => {
|
|
91
|
+
cache.put(request, clone);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
return networkResponse;
|
|
95
|
+
})
|
|
96
|
+
.catch(() => caches.match(request)),
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 🧱 STATIC ASSETS → CACHE FIRST
|
|
103
|
+
event.respondWith(
|
|
104
|
+
caches.match(request).then((cachedResponse) => {
|
|
105
|
+
return (
|
|
106
|
+
cachedResponse ||
|
|
107
|
+
fetch(request).then((networkResponse) => {
|
|
108
|
+
const clone = networkResponse.clone();
|
|
41
109
|
|
|
42
110
|
caches.open(CACHE_NAME).then((cache) => {
|
|
43
|
-
cache.put(
|
|
111
|
+
cache.put(request, clone);
|
|
44
112
|
});
|
|
45
113
|
|
|
46
114
|
return networkResponse;
|
|
47
115
|
})
|
|
48
|
-
|
|
49
|
-
return caches.match("/");
|
|
50
|
-
});
|
|
116
|
+
);
|
|
51
117
|
}),
|
|
52
118
|
);
|
|
53
119
|
});
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
const CACHE_NAME = "nexa-cache-v1";
|
|
2
|
+
|
|
3
|
+
const ASSETS_TO_CACHE = ["/", "/manifest.json", "/nexa.svg"];
|
|
4
|
+
|
|
5
|
+
self.addEventListener("install", (event) => {
|
|
6
|
+
event.waitUntil(
|
|
7
|
+
caches.open(CACHE_NAME).then((cache) => {
|
|
8
|
+
return cache.addAll(ASSETS_TO_CACHE);
|
|
9
|
+
}),
|
|
10
|
+
);
|
|
11
|
+
self.skipWaiting();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
self.addEventListener("activate", (event) => {
|
|
15
|
+
event.waitUntil(
|
|
16
|
+
caches.keys().then((keys) =>
|
|
17
|
+
Promise.all(
|
|
18
|
+
keys.map((key) => {
|
|
19
|
+
if (key !== CACHE_NAME) {
|
|
20
|
+
return caches.delete(key);
|
|
21
|
+
}
|
|
22
|
+
}),
|
|
23
|
+
),
|
|
24
|
+
),
|
|
25
|
+
);
|
|
26
|
+
self.clients.claim();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
self.addEventListener("fetch", (event) => {
|
|
30
|
+
if (event.request.method !== "GET") return;
|
|
31
|
+
|
|
32
|
+
event.respondWith(
|
|
33
|
+
caches.match(event.request).then((cachedResponse) => {
|
|
34
|
+
if (cachedResponse) {
|
|
35
|
+
return cachedResponse;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return fetch(event.request)
|
|
39
|
+
.then((networkResponse) => {
|
|
40
|
+
const responseClone = networkResponse.clone();
|
|
41
|
+
|
|
42
|
+
caches.open(CACHE_NAME).then((cache) => {
|
|
43
|
+
cache.put(event.request, responseClone);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return networkResponse;
|
|
47
|
+
})
|
|
48
|
+
.catch(() => {
|
|
49
|
+
return caches.match("/");
|
|
50
|
+
});
|
|
51
|
+
}),
|
|
52
|
+
);
|
|
53
|
+
});
|