Skip to content

Commit 84a2eb2

Browse files
committed
Updated UI 2.4
Signed-off-by: Someshdiwan <[email protected]>
1 parent 7ee0aa7 commit 84a2eb2

File tree

1 file changed

+142
-0
lines changed

1 file changed

+142
-0
lines changed

site/assets/sw.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/*
12
const CACHE_NAME = "java-evolution-cache-v3";
23
const urlsToCache = [
34
"/JavaEvolution-Learning-Growing-Mastering/",
@@ -66,3 +67,144 @@ self.addEventListener("fetch", (event) => {
6667
})
6768
);
6869
});
70+
*/
71+
72+
73+
const CACHE_NAME = "java-evolution-cache-v5"; // Updated version
74+
const STATIC_ASSETS = [
75+
"/JavaEvolution-Learning-Growing-Mastering/",
76+
"/JavaEvolution-Learning-Growing-Mastering/default.html", // Changed from index.html
77+
"/JavaEvolution-Learning-Growing-Mastering/assets/style.css",
78+
"/JavaEvolution-Learning-Growing-Mastering/assets/favicon-96x96.png",
79+
"/JavaEvolution-Learning-Growing-Mastering/assets/favicon.svg",
80+
"/JavaEvolution-Learning-Growing-Mastering/assets/favicon.ico",
81+
"/JavaEvolution-Learning-Growing-Mastering/assets/apple-touch-icon.png",
82+
"/JavaEvolution-Learning-Growing-Mastering/assets/site.webmanifest",
83+
"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" // Cache CDN resource
84+
];
85+
86+
// Simple offline fallback page
87+
const OFFLINE_PAGE = `
88+
<!DOCTYPE html>
89+
<html lang="en">
90+
<head>
91+
<meta charset="UTF-8">
92+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
93+
<title>Offline</title>
94+
<style>
95+
body { font-family: 'Segoe UI', sans-serif; text-align: center; padding: 2rem; background: #f9f9f9; color: #333; }
96+
h1 { font-size: 1.5rem; }
97+
p { font-size: 1rem; }
98+
</style>
99+
</head>
100+
<body>
101+
<h1>You're Offline</h1>
102+
<p>Please check your internet connection and try again.</p>
103+
</body>
104+
</html>
105+
`;
106+
107+
// Install event
108+
self.addEventListener("install", (event) => {
109+
console.log("[Service Worker] Installing and caching static assets");
110+
self.skipWaiting();
111+
event.waitUntil(
112+
caches.open(CACHE_NAME).then((cache) => {
113+
// Cache static assets
114+
return cache.addAll(STATIC_ASSETS).then(() => {
115+
// Cache offline page
116+
const offlineResponse = new Response(OFFLINE_PAGE, {
117+
headers: { 'Content-Type': 'text/html' }
118+
});
119+
return cache.put('/offline.html', offlineResponse);
120+
});
121+
})
122+
);
123+
});
124+
125+
// Activate event
126+
self.addEventListener("activate", (event) => {
127+
console.log("[Service Worker] Activated and cleaning old caches");
128+
event.waitUntil(
129+
caches.keys().then((cacheNames) =>
130+
Promise.all(
131+
cacheNames
132+
.filter((name) => name !== CACHE_NAME)
133+
.map((name) => caches.delete(name))
134+
)
135+
)
136+
);
137+
self.clients.claim();
138+
});
139+
140+
// Fetch event
141+
self.addEventListener("fetch", (event) => {
142+
const request = event.request;
143+
144+
// Handle navigation requests (HTML pages)
145+
if (request.mode === 'navigate') {
146+
event.respondWith(
147+
fetch(request)
148+
.then((networkResponse) => {
149+
// Cache successful response
150+
return caches.open(CACHE_NAME).then((cache) => {
151+
cache.put(request, networkResponse.clone());
152+
return networkResponse;
153+
});
154+
})
155+
.catch(() => {
156+
// Serve cached page or offline fallback
157+
return caches.match(request).then((cachedResponse) => {
158+
return cachedResponse || caches.match('/offline.html');
159+
});
160+
})
161+
);
162+
return;
163+
}
164+
165+
// Handle other requests (CSS, images, fonts, etc.)
166+
event.respondWith(
167+
caches.match(request).then((cachedResponse) => {
168+
// Return cached response if available
169+
if (cachedResponse) {
170+
// Refresh cache in background
171+
fetch(request).then((networkResponse) => {
172+
caches.open(CACHE_NAME).then((cache) => {
173+
cache.put(request, networkResponse);
174+
});
175+
}).catch(() => {}); // Silent fail
176+
return cachedResponse;
177+
}
178+
179+
// Fetch from network
180+
return fetch(request)
181+
.then((networkResponse) => {
182+
// Cache GET requests from same origin or Font Awesome CDN
183+
if (
184+
request.method === "GET" &&
185+
(request.url.startsWith(self.location.origin) ||
186+
request.url.startsWith('https://cdnjs.cloudflare.com'))
187+
) {
188+
return caches.open(CACHE_NAME).then((cache) => {
189+
cache.put(request, networkResponse.clone());
190+
// Limit cache size
191+
cache.keys().then((keys) => {
192+
if (keys.length > 50) {
193+
cache.delete(keys[0]);
194+
}
195+
});
196+
return networkResponse;
197+
});
198+
}
199+
return networkResponse;
200+
})
201+
.catch(() => {
202+
// Fallback for non-HTML resources
203+
if (request.url.includes('font-awesome')) {
204+
return caches.match('https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css');
205+
}
206+
return caches.match('/offline.html');
207+
});
208+
})
209+
);
210+
});

0 commit comments

Comments
 (0)