Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
145 lines (130 loc) · 4.87 KB
/
Copy pathsw.js
File metadata and controls
145 lines (130 loc) · 4.87 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// LSP D-Planner — Service Worker
// Network-first for HTML (always fresh), cache-first for static assets.
// Bump CACHE_VERSION on every deploy to force old SW replacement.
const CACHE_VERSION = 'lsp-dplanner-v2.40.02';
// These are never cached — always fetched live or passed through
const NEVER_CACHE = [
'.apk',
'.aab',
'raw.githubusercontent.com'
];
function shouldNeverCache(url) {
return NEVER_CACHE.some(pattern => url.href.includes(pattern));
}
function isHTMLRequest(request) {
const accept = request.headers.get('Accept') || '';
return accept.includes('text/html');
}
function getAppBasePath() {
const p = self.location.pathname || '/';
if (p.includes('/d-planner')) return '/d-planner/';
if (p.includes('/LSP_D-planner')) return '/LSP_D-planner/';
const swDir = p.replace(/[^/]*$/, '');
return swDir || '/LSP_D-planner/';
}
const APP_BASE = getAppBasePath();
const OFFLINE_INDEX = APP_BASE + 'index.html';
// Required for offline/PWA startup (Tier-3 ZHL + self-hosted fonts/icons)
const PRECACHE_ASSETS = [
OFFLINE_INDEX,
APP_BASE + 'capacitor-bridge.js',
APP_BASE + 'zhl-engine-bundle.js',
APP_BASE + 'zhl-worker-bridge.js',
APP_BASE + 'zhl-schedule-worker.js',
APP_BASE + 'vendor/jspdf.umd.min.js',
APP_BASE + 'vendor/fonts/fonts.css',
APP_BASE + 'vendor/fonts/DejaVuSans.ttf',
APP_BASE + 'vendor/fonts/DejaVuSans-Bold.ttf',
APP_BASE + 'vendor/fonts/JTUSjIg69CK48gW7PXoo9Wdhyzbi.woff2',
APP_BASE + 'vendor/fonts/JTUSjIg69CK48gW7PXoo9Wlhyw.woff2',
APP_BASE + 'vendor/fonts/QGYvz_MVcBeNP4NJtEtq.woff2',
APP_BASE + 'vendor/fonts/QGYvz_MVcBeNP4NJuktqQ4E.woff2',
APP_BASE + 'vendor/fonts/tDbV2o-flEEny0FZhsfKu5WU4xD0OwG_TA.woff2',
APP_BASE + 'vendor/fonts/tDbV2o-flEEny0FZhsfKu5WU4xD1OwG_TA.woff2',
APP_BASE + 'vendor/fonts/tDbV2o-flEEny0FZhsfKu5WU4xD2OwG_TA.woff2',
APP_BASE + 'vendor/fonts/tDbV2o-flEEny0FZhsfKu5WU4xD4OwG_TA.woff2',
APP_BASE + 'vendor/fonts/tDbV2o-flEEny0FZhsfKu5WU4xD7OwE.woff2',
APP_BASE + 'vendor/fonts/tDbV2o-flEEny0FZhsfKu5WU4xD_OwG_TA.woff2',
APP_BASE + 'vendor/icons/giw-icon-192.png',
APP_BASE + 'manifest.json',
APP_BASE + 'icon-192.png',
APP_BASE + 'icon-512.png',
];
// Install — skip waiting immediately so new SW takes over fast
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_VERSION)
.then(cache => cache.addAll(PRECACHE_ASSETS))
.then(() => self.skipWaiting())
);
});
// Activate — delete ALL old caches, claim clients immediately
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys()
.then(keys => Promise.all(
keys
.filter(key => key !== CACHE_VERSION)
.map(key => {
console.log('[SW] Deleting old cache:', key);
return caches.delete(key);
})
))
.then(() => self.clients.claim())
);
});
self.addEventListener('message', event => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});
// Fetch strategy:
// - APK / external URLs: pass through, never intercept
// - HTML (index.html, navigation): network-first, cache fallback
// - Everything else (JS, CSS, fonts, etc.): cache-first, network fallback
self.addEventListener('fetch', event => {
if (event.request.method !== 'GET') return;
const url = new URL(event.request.url);
// Never intercept cross-origin or APK requests
if (url.origin !== self.location.origin) return;
if (shouldNeverCache(url)) return;
// Network-first for HTML — ensures index.html is always up to date
if (isHTMLRequest(event.request) || url.pathname.endsWith('.html') || url.pathname.endsWith('/')) {
event.respondWith(
fetch(event.request)
.then(response => {
if (response.ok) {
// Update cache with fresh version
const clone = response.clone();
caches.open(CACHE_VERSION).then(cache => {
const key = new Request(url.origin + url.pathname);
cache.put(key, clone);
});
}
return response;
})
.catch(() => {
// Network failed — serve cached version as offline fallback
return caches.match(event.request, { ignoreSearch: true })
.then(match => match || caches.match(OFFLINE_INDEX, { ignoreSearch: true }));
})
);
return;
}
// Cache-first for all other static assets
event.respondWith(
caches.match(event.request, { ignoreSearch: true })
.then(cached => {
if (cached) return cached;
return fetch(event.request)
.then(response => {
if (response.ok && url.pathname.startsWith(APP_BASE)) {
const clone = response.clone();
const cacheKey = new Request(url.origin + url.pathname);
caches.open(CACHE_VERSION).then(cache => cache.put(cacheKey, clone));
}
return response;
});
})
);
});