-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
157 lines (146 loc) · 5.57 KB
/
sw.js
File metadata and controls
157 lines (146 loc) · 5.57 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
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Tezos Systems — Service Worker
* Cache-first for shell assets, network-first for API data
*/
const CACHE_NAME = 'tezos-systems-v62';
// Shell assets to precache
const SHELL_ASSETS = [
'/',
'/index.html',
'/css/styles.css',
'/css/styles.min.css',
'/js/core/app.js',
'/js/core/api.js',
'/js/core/config.js',
'/js/core/utils.js',
'/js/core/storage.js',
'/js/ui/animations.js',
'/js/ui/gauge.js',
'/js/ui/tabs.js',
'/js/ui/theme.js',
'/js/ui/title.js',
'/js/ui/share.js',
'/js/features/whales.js',
'/js/features/moments.js',
'/js/features/my-baker.js',
'/js/features/sleeping-giants.js',
'/js/features/governance.js',
'/js/features/history.js',
'/js/features/price.js',
'/js/features/changelog.js',
'/js/features/comparison.js',
'/js/features/streak.js',
'/js/features/calculator.js',
'/js/effects/arcade-effects.js',
'/js/effects/audio.js',
'/js/effects/matrix-effects.js',
'/js/effects/bg-effects.js',
'/js/effects/vibes.js',
'/js/features/baker-report-card.js',
'/js/features/chamber.js',
'/js/features/compare-page.js',
'/js/features/cycle-pulse.js',
'/js/features/daily-briefing.js',
'/js/features/hen-mode.js',
'/js/features/leaderboard.js',
'/js/features/my-tezos.js',
'/js/features/price-intelligence.js',
'/js/features/rewards-tracker.js',
'/js/features/state-of-tezos.js',
'/js/features/tooltip-tour.js',
'/js/features/upgrade-effect.js',
'/js/landing/live-data.js',
'/data/protocol-data.json',
'/data/tweets.json',
'/favicon.svg',
'/favicon-48.png',
'/site.webmanifest'
];
// API domains — network-first with cache fallback
const API_HOSTS = ['api.tzkt.io', 'eu.rpc.tez.capital', 'api.coingecko.com', 'iijpfczftroespicmufb.supabase.co', 'data.objkt.com'];
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
// Don't fail install if some assets are missing
return Promise.allSettled(
SHELL_ASSETS.map((url) => cache.add(url).catch(() => console.warn('SW: skip', url)))
);
}).then(() => self.skipWaiting())
);
});
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => {
return Promise.all(
keys.filter((k) => k !== CACHE_NAME).map((k) => caches.delete(k))
);
}).then(() => self.clients.claim())
);
});
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Skip non-GET
if (event.request.method !== 'GET') return;
// Version metadata must stay fresh for the footer sanity check.
if (url.origin === self.location.origin && url.pathname === '/version.json') {
event.respondWith(
fetch(event.request, { cache: 'no-store' })
.then((response) => {
if (response.ok) {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put('/version.json', clone));
}
return response;
})
.catch(() => caches.match('/version.json').then((r) => r || new Response('Version unavailable', { status: 503, statusText: 'Service Unavailable' })))
);
return;
}
// API requests: network-first, cache fallback
if (API_HOSTS.some((h) => url.hostname === h)) {
event.respondWith(
fetch(event.request)
.then((response) => {
if (response.ok) {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
}
return response;
})
.catch(() => caches.match(event.request).then((r) => r || new Response('Offline', { status: 503, statusText: 'Service Unavailable' })))
);
return;
}
// CDN resources (Chart.js, fonts): cache-first
if (url.hostname === 'cdn.jsdelivr.net' || url.hostname === 'fonts.googleapis.com' || url.hostname === 'fonts.gstatic.com' || url.hostname === 'unpkg.com') {
event.respondWith(
caches.match(event.request).then((cached) => {
return cached || fetch(event.request).then((response) => {
if (response.ok) {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
}
return response;
}).catch(() => new Response('', { status: 504 }));
})
);
return;
}
// Shell assets: network-first with cache fallback so version sanity checks
// and front-page JS do not lag behind deployed code.
if (url.origin === self.location.origin) {
event.respondWith(
fetch(event.request, { cache: 'no-cache' })
.then((response) => {
if (response.ok) {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
}
return response;
})
.catch(() => caches.match(event.request)
.then((r) => r || caches.match(url.pathname))
.then((r) => r || new Response('Offline', { status: 503, statusText: 'Service Unavailable' })))
);
}
});