Skip to content

Commit 9afa6c0

Browse files
committed
feat: Enhance PWA capabilities by adding new icons, updating the manifest, integrating an install component, and refining service worker caching strategies.
1 parent 926fbe5 commit 9afa6c0

6 files changed

Lines changed: 77 additions & 35 deletions

File tree

‎index.html‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
<!doctype html>
22
<html lang="en">
3-
<head>
4-
<meta charset="UTF-8" />
5-
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
6-
<link href="/img/64.png" rel="shortcut icon" />
7-
<link rel="manifest" href="./manifest.webmanifest" />
8-
<meta name="theme-color" content="#0052cc" />
9-
<meta name="mobile-web-app-capable" content="yes" />
10-
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
11-
<title>Document Editor</title>
12-
<script src="/web-apps/apps/api/documents/api.js"></script>
13-
</head>
143

15-
<body>
16-
<div id="app" class="w-full h-full">
17-
<div id="iframe"></div>
18-
</div>
19-
</body>
20-
<script type="module" src="./index.ts"></script>
21-
</html>
4+
<head>
5+
<meta charset="UTF-8" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
7+
<link href="/img/64.png" rel="shortcut icon" />
8+
<link rel="icon" type="image/png" href="/img/64.png" />
9+
<link rel="manifest" href="./manifest.webmanifest" />
10+
<meta name="theme-color" content="#0052cc" />
11+
<meta name="mobile-web-app-capable" content="yes" />
12+
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
13+
<title>Document Editor</title>
14+
<script src="/web-apps/apps/api/documents/api.js"></script>
15+
</head>
16+
17+
<body>
18+
<div id="app" class="w-full h-full">
19+
<div id="iframe"></div>
20+
</div>
21+
<script type="module"
22+
src="https://cdn.jsdelivr.net/npm/@khmyznikov/pwa-install@0.4.3/dist/pwa-install.bundle.js"></script>
23+
<pwa-install id="pwa-install" manifestpath="./manifest.webmanifest" iconpath="./img/pwa-512.png"></pwa-install>
24+
</body>
25+
<script type="module" src="./index.ts"></script>
26+
27+
</html>

‎public/img/64.png‎

4.93 KB
Loading

‎public/img/pwa-192.png‎

33.5 KB
Loading

‎public/img/pwa-512.png‎

357 KB
Loading

‎public/manifest.webmanifest‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
"src": "img/64.png",
1212
"sizes": "64x64",
1313
"type": "image/png"
14+
},
15+
{
16+
"src": "img/pwa-192.png",
17+
"sizes": "192x192",
18+
"type": "image/png"
19+
},
20+
{
21+
"src": "img/pwa-512.png",
22+
"sizes": "512x512",
23+
"type": "image/png"
1424
}
1525
]
1626
}

‎public/sw.js‎

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ self.addEventListener('activate', (event) => {
2828
self.clients.claim();
2929
});
3030

31-
// Fetch event: Stale-While-Revalidate strategy
31+
// Fetch event: Strategy-based resource handling
3232
self.addEventListener('fetch', (event) => {
3333
const url = new URL(event.request.url);
3434

@@ -42,13 +42,20 @@ self.addEventListener('fetch', (event) => {
4242
// These are typically documents being edited, which should always be fresh.
4343
if (url.searchParams.has('file') || url.searchParams.has('src')) return;
4444

45-
// 4. Stale-While-Revalidate Strategy
46-
event.respondWith(
47-
caches.match(event.request).then((cachedResponse) => {
48-
const fetchPromise = fetch(event.request)
45+
// 4. Determine Strategy
46+
const isHtml = event.request.mode === 'navigate' ||
47+
url.pathname.endsWith('.html') ||
48+
url.pathname === '/' ||
49+
url.pathname.endsWith('/');
50+
51+
if (isHtml) {
52+
// Strategy: Network-First for HTML/Navigation
53+
// Ensuring the user always gets the latest version if online,
54+
// but can still access the app when offline.
55+
event.respondWith(
56+
fetch(event.request)
4957
.then((networkResponse) => {
50-
// Only cache valid 200 responses
51-
if (networkResponse && networkResponse.status === 200 && networkResponse.type === 'basic') {
58+
if (networkResponse && networkResponse.status === 200) {
5259
const responseToCache = networkResponse.clone();
5360
caches.open(CACHE_NAME).then((cache) => {
5461
cache.put(event.request, responseToCache);
@@ -57,15 +64,34 @@ self.addEventListener('fetch', (event) => {
5764
return networkResponse;
5865
})
5966
.catch(() => {
60-
// If network fails, return the cached version if we have it
61-
return cachedResponse;
62-
});
67+
return caches.match(event.request);
68+
})
69+
);
70+
} else {
71+
// Strategy: Stale-While-Revalidate for other static assets (JS, CSS, Images)
72+
event.respondWith(
73+
caches.match(event.request).then((cachedResponse) => {
74+
const fetchPromise = fetch(event.request)
75+
.then((networkResponse) => {
76+
// Only cache valid 200 responses
77+
if (networkResponse && networkResponse.status === 200 && networkResponse.type === 'basic') {
78+
const responseToCache = networkResponse.clone();
79+
caches.open(CACHE_NAME).then((cache) => {
80+
cache.put(event.request, responseToCache);
81+
});
82+
}
83+
return networkResponse;
84+
})
85+
.catch(() => {
86+
return cachedResponse;
87+
});
6388

64-
// Use event.waitUntil to ensure the fetch/cache update continues in background
65-
if (!cachedResponse) {
66-
return fetchPromise;
67-
}
68-
return cachedResponse;
69-
}),
70-
);
89+
// Use event.waitUntil to ensure the fetch/cache update continues in background
90+
if (!cachedResponse) {
91+
return fetchPromise;
92+
}
93+
return cachedResponse;
94+
}),
95+
);
96+
}
7197
});

0 commit comments

Comments
 (0)