-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice-worker.js
More file actions
168 lines (151 loc) · 5.38 KB
/
service-worker.js
File metadata and controls
168 lines (151 loc) · 5.38 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
158
159
160
161
162
163
164
165
166
167
168
// Service Worker for حاسبة الطالب PWA
const CACHE_NAME = 'student-calculator-v1.0.0';
const RUNTIME_CACHE = 'runtime-cache-v1';
// الملفات التي يجب تخزينها في الكاش
const STATIC_CACHE_URLS = [
'./',
'./index.html',
'./styles.css',
'./script.js',
'./manifest.json',
'./absence-guide.html',
'https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700;900&display=swap',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css'
];
// الموارد الخارجية التي يمكن تخزينها (اختياري)
const EXTERNAL_RESOURCES = [
'https://cdn.jsdelivr.net/particles.js/2.0.0/particles.min.js',
'https://cdn.jsdelivr.net/npm/chart.js'
];
// استراتيجية التخزين: network-first للموارد الخارجية
const NETWORK_FIRST_PATTERNS = [
/^https:\/\/fonts\.googleapis\.com/,
/^https:\/\/cdnjs\.cloudflare\.com/,
/^https:\/\/cdn\.jsdelivr\.net/
];
// تثبيت Service Worker
self.addEventListener('install', (event) => {
console.log('[Service Worker] Installing...');
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
console.log('[Service Worker] Caching static files');
return cache.addAll(STATIC_CACHE_URLS);
})
.then(() => {
// محاولة تخزين الموارد الخارجية (قد تفشل بسبب CORS)
return caches.open(RUNTIME_CACHE).then((cache) => {
return Promise.allSettled(
EXTERNAL_RESOURCES.map(url =>
fetch(url).then(response => {
if (response.ok) {
return cache.put(url, response);
}
}).catch(() => {
// تجاهل الأخطاء للموارد الخارجية
console.log(`[Service Worker] Failed to cache: ${url}`);
})
)
);
});
})
.then(() => self.skipWaiting())
);
});
// تفعيل Service Worker
self.addEventListener('activate', (event) => {
console.log('[Service Worker] Activating...');
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME && cacheName !== RUNTIME_CACHE) {
console.log('[Service Worker] Deleting old cache:', cacheName);
return caches.delete(cacheName);
}
})
);
}).then(() => self.clients.claim())
);
});
// اعتراض الطلبات
self.addEventListener('fetch', (event) => {
const { request } = event;
const url = new URL(request.url);
// تجاهل الطلبات غير GET
if (request.method !== 'GET') {
return;
}
// استراتيجية Network First للموارد الخارجية
const isExternalResource = NETWORK_FIRST_PATTERNS.some(pattern => pattern.test(request.url));
if (isExternalResource) {
event.respondWith(
fetch(request)
.then((response) => {
if (response.ok) {
const responseToCache = response.clone();
caches.open(RUNTIME_CACHE).then((cache) => {
cache.put(request, responseToCache);
});
}
return response;
})
.catch(() => {
return caches.match(request);
})
);
return;
}
// استراتيجية Cache First للموارد المحلية
event.respondWith(
caches.match(request)
.then((cachedResponse) => {
// إذا كان موجود في الكاش، أرجعها
if (cachedResponse) {
return cachedResponse;
}
// إذا لم تكن موجودة، اجلبها من الشبكة
return fetch(request)
.then((response) => {
// تحقق من أن الاستجابة صالحة
if (!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// استنساخ الاستجابة
const responseToCache = response.clone();
// تخزين في الكاش
const cacheName = url.origin === location.origin ? CACHE_NAME : RUNTIME_CACHE;
caches.open(cacheName)
.then((cache) => {
cache.put(request, responseToCache);
});
return response;
})
.catch(() => {
// إذا فشل الاتصال، حاول إرجاع صفحة offline
if (request.mode === 'navigate') {
return caches.match('./index.html');
}
// للموارد الأخرى، حاول إرجاع أي شيء من الكاش
return caches.match(request);
});
})
);
});
// معالجة الرسائل من الصفحة الرئيسية
self.addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
self.skipWaiting();
}
if (event.data && event.data.type === 'CACHE_URLS') {
event.waitUntil(
caches.open(RUNTIME_CACHE).then((cache) => {
return cache.addAll(event.data.urls);
})
);
}
});
// إشعار عند تحديث جديد
self.addEventListener('updatefound', () => {
console.log('[Service Worker] Update found');
});