-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsw.js
More file actions
114 lines (106 loc) · 3.86 KB
/
sw.js
File metadata and controls
114 lines (106 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Version will be injected at build time from package.json
const APP_VERSION = "0.6.54"; // Placeholder replaced by build process
const CACHE_NAME = `black-trigram-v${APP_VERSION}`;
// Minimal caching - essential assets for reliable offline support
// All other resources use network-first strategy
const urlsToCache = [
"/",
"/index.html",
"/manifest.json",
];
// Install event - cache minimal resources and skip waiting for immediate activation
self.addEventListener("install", (event) => {
console.log(`[SW v${APP_VERSION}] Installing...`);
event.waitUntil(
caches
.open(CACHE_NAME)
.then((cache) => {
console.log(`[SW v${APP_VERSION}] Caching essential assets`);
return cache.addAll(urlsToCache);
})
.then(() => {
console.log(`[SW v${APP_VERSION}] Installation complete`);
})
);
// Force immediate activation - don't wait for old service worker to finish
self.skipWaiting();
});
// Activate event - aggressively clean up old caches and take control immediately
self.addEventListener("activate", (event) => {
console.log(`[SW v${APP_VERSION}] Activating...`);
event.waitUntil(
caches
.keys()
.then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
// Delete all caches that don't match current version
if (cacheName.startsWith("black-trigram-") && cacheName !== CACHE_NAME) {
console.log(`[SW v${APP_VERSION}] Deleting old cache: ${cacheName}`);
return caches.delete(cacheName);
}
})
);
})
.then(() => {
console.log(`[SW v${APP_VERSION}] Activation complete - now controlling all pages`);
})
);
// Take control of all pages immediately, even if they were loaded with old SW
self.clients.claim();
});
// Fetch event - NETWORK FIRST for all resources to ensure fresh content
self.addEventListener("fetch", (event) => {
const url = new URL(event.request.url);
// Skip caching for development servers and WebSocket connections
if (
url.hostname.includes(".app.github.dev") ||
url.hostname.includes("gitpod.io") ||
url.protocol === "ws:" ||
url.protocol === "wss:"
) {
return; // Let browser handle it normally
}
// Skip intercepting Google Fonts requests to comply with CSP connect-src
if (
url.hostname === "fonts.googleapis.com" ||
url.hostname === "fonts.gstatic.com"
) {
return; // Let browser handle font requests natively
}
// Network-first strategy for ALL resources - always get fresh content
// This ensures users always get the latest version when online
event.respondWith(
fetch(event.request)
.then((response) => {
// Only cache successful, non-opaque responses
if (response.ok && response.type !== "opaque") {
// Clone response for caching (responses can only be used once)
const responseClone = response.clone();
// Cache for offline fallback only
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseClone);
});
}
return response;
})
.catch(() => {
// Only use cache as fallback when network fails (offline)
return caches.match(event.request).then((cachedResponse) => {
if (cachedResponse) {
console.log(`[SW v${APP_VERSION}] Serving from cache (offline): ${url.pathname}`);
return cachedResponse;
}
// If requesting document and no cache, serve cached index.html
if (event.request.destination === "document") {
return caches.match("/index.html");
}
// Otherwise, fail gracefully
return new Response("Offline and no cached content available", {
status: 503,
statusText: "Service Unavailable"
});
});
})
);
});