-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
120 lines (110 loc) · 3.07 KB
/
Copy pathsw.js
File metadata and controls
120 lines (110 loc) · 3.07 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
// Service Worker for caching and offline support
const CACHE_VERSION = 'v9';
const CACHE_NAME = `acreetionos-${CACHE_VERSION}`;
const STATIC_ASSETS = [
'/',
'/404.html',
'/compare.html',
'/contact.html',
'/docs.html',
'/faq.html',
'/flash.html',
'/git-tracker.html',
'/governance.html',
'/hosting.html',
'/index.html',
'/newsletter.html',
'/requirements.html',
'/unofficial/',
'/unofficial/32bit.html',
'/unofficial/gnome.html',
'/unofficial/hyprland.html',
'/unofficial/i3.html',
'/unofficial/immutable.html',
'/unofficial/mate.html',
'/unofficial/openbox.html',
'/unofficial/plasma.html',
'/unofficial/sway.html',
'/unofficial/xfce.html',
'/contact.css',
'/acreetionoslogo.webp',
'/logo.webp',
];
// Install event - cache static assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
return cache.addAll(STATIC_ASSETS);
})
);
self.skipWaiting();
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames
.filter((name) => name !== CACHE_NAME)
.map((name) => caches.delete(name))
);
})
);
self.clients.claim();
});
// Fetch event
self.addEventListener('fetch', (event) => {
const url = new URL(event.request.url);
// Bypass service worker for Cloudflare edge endpoints
if (url.pathname.startsWith('/cdn-cgi/')) {
return;
}
// Forward all POST API requests directly without caching
if (event.request.method === 'POST' && url.pathname.startsWith('/api/')) {
event.respondWith(fetch(event.request.clone()));
return;
}
// Only cache GET requests
if (event.request.method !== 'GET') {
event.respondWith(fetch(event.request));
return;
}
// Network-first for HTML and JS: always fetch fresh, fall back to cache if offline
if (url.pathname.endsWith('.html') || url.pathname === '/' || url.pathname === '' || url.pathname.endsWith('.js')) {
event.respondWith(
fetch(event.request)
.then((response) => {
if (response.status === 200) {
const clone = response.clone();
caches.open(CACHE_NAME).then((cache) => cache.put(event.request, clone));
}
return response;
})
.catch(() => caches.match(event.request))
);
return;
}
// Cache-first for static assets (images, fonts, CSS)
event.respondWith(
caches.match(event.request).then((response) => {
if (response) {
return response;
}
return fetch(event.request).then((response) => {
if (response.status === 200) {
const responseClone = response.clone();
caches.open(CACHE_NAME).then((cache) => {
cache.put(event.request, responseClone);
});
}
return response;
});
})
);
});
// Listen for messages from pages
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
});