mouse-vm 1.0.2

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/static/sw.js ADDED
@@ -0,0 +1,85 @@
1
+ /* -------------------------------------------------------------
2
+ * VIRTUAL MOUSE & KEYBOARD - SERVICE WORKER
3
+ * Provides offline caching, fast load times, and PWA capabilities.
4
+ * ------------------------------------------------------------- */
5
+
6
+ const CACHE_NAME = "mouse-vm-v1.0.2";
7
+ const ASSETS_TO_CACHE = [
8
+ "./",
9
+ "./index.html",
10
+ "./style.css?v=2.5",
11
+ "./app.js?v=2.5",
12
+ "./manifest.json?v=2.5",
13
+ "./icons/icon.svg",
14
+ "./icons/icon-192.png",
15
+ "./icons/icon-512.png"
16
+ ];
17
+
18
+ // Install Event: Cache essential shell assets
19
+ self.addEventListener("install", (event) => {
20
+ event.waitUntil(
21
+ caches.open(CACHE_NAME).then((cache) => {
22
+ return cache.addAll(ASSETS_TO_CACHE).catch((err) => {
23
+ console.warn("PWA: Some assets failed to pre-cache:", err);
24
+ });
25
+ }).then(() => self.skipWaiting())
26
+ );
27
+ });
28
+
29
+ // Activate Event: Clean up outdated caches
30
+ self.addEventListener("activate", (event) => {
31
+ event.waitUntil(
32
+ caches.keys().then((keys) => {
33
+ return Promise.all(
34
+ keys.map((key) => {
35
+ if (key !== CACHE_NAME) {
36
+ return caches.delete(key);
37
+ }
38
+ })
39
+ );
40
+ })
41
+ );
42
+ self.clients.claim();
43
+ });
44
+
45
+ // Fetch Event: Network first with Cache fallback for dynamic requests
46
+ self.addEventListener("fetch", (event) => {
47
+ // Ignore non-GET and WebSocket requests
48
+ if (event.request.method !== "GET" || event.request.url.startsWith("ws:") || event.request.url.startsWith("wss:")) {
49
+ return;
50
+ }
51
+
52
+ event.respondWith(
53
+ caches.match(event.request).then((cachedResponse) => {
54
+ if (cachedResponse) {
55
+ // Fetch in background to revalidate cache (Stale-While-Revalidate)
56
+ fetch(event.request).then((networkResponse) => {
57
+ if (networkResponse && networkResponse.status === 200) {
58
+ caches.open(CACHE_NAME).then((cache) => {
59
+ cache.put(event.request, networkResponse.clone());
60
+ });
61
+ }
62
+ }).catch(() => {});
63
+ return cachedResponse;
64
+ }
65
+
66
+ return fetch(event.request).then((networkResponse) => {
67
+ if (!networkResponse || networkResponse.status !== 200 || networkResponse.type !== "basic") {
68
+ return networkResponse;
69
+ }
70
+
71
+ const responseToCache = networkResponse.clone();
72
+ caches.open(CACHE_NAME).then((cache) => {
73
+ cache.put(event.request, responseToCache);
74
+ });
75
+
76
+ return networkResponse;
77
+ }).catch(() => {
78
+ // Return index.html for navigation requests when offline
79
+ if (event.request.mode === "navigate") {
80
+ return caches.match("./index.html");
81
+ }
82
+ });
83
+ })
84
+ );
85
+ });