-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
139 lines (130 loc) · 4.35 KB
/
Copy pathsw.js
File metadata and controls
139 lines (130 loc) · 4.35 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
const CACHE_NAME = 'alayqona-v2';
const OFFLINE_URL = 'offline.html';
const PRECACHE_URLS = [
'/',
'/index.html',
'/men.html',
'/female.html',
'/offline.html',
'/moan.html',
'/icons/icon-192x192.png',
'/icons/icon-512x512.png',
'https://talalye1.github.io/Icon-perfumes/moant4.jpg',
'https://talalye1.github.io/Icon-perfumes/men.jpg',
'https://talalye1.github.io/Icon-perfumes/female.jpg',
'https://talalye1.github.io/Icon-perfumes/moan.jpg',
'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css',
'https://unpkg.com/aos@2.3.1/dist/aos.css'
];
// مرحلة التثبيت - تخزين الملفات الأساسية في الكاش
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME)
.then(cache => {
console.log('تم فتح الكاش');
return cache.addAll(PRECACHE_URLS);
})
.then(() => {
console.log('تم تخزين جميع الموارد في الكاش');
return self.skipWaiting();
})
);
});
// مرحلة التنشيط - تنظيف الكاش القديم
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(cacheNames => {
return Promise.all(
cacheNames.map(cache => {
if (cache !== CACHE_NAME) {
console.log('حذف الكاش القديم:', cache);
return caches.delete(cache);
}
})
);
})
.then(() => self.clients.claim())
);
});
// استراتيجية التخزين: Network First, Fallback to Cache
self.addEventListener('fetch', event => {
// تجاهل طلبات غير GET
if (event.request.method !== 'GET') return;
// معالجة طلبات الصفحات
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request)
.then(response => {
// تحديث الكاش مع الاستجابة الجديدة
const responseClone = response.clone();
caches.open(CACHE_NAME)
.then(cache => cache.put(event.request, responseClone));
return response;
})
.catch(() => {
// عرض الصفحة المخزنة عند عدم الاتصال بالإنترنت
return caches.match(OFFLINE_URL) ||
caches.match('/index.htm');
})
);
} else {
// معالجة الطلبات الأخرى (أصول الموقع)
event.respondWith(
caches.match(event.request)
.then(cachedResponse => {
// إرجاع الاستجابة المخزنة إذا وجدت مع التحقق من التحديثات
if (cachedResponse) {
fetch(event.request)
.then(response => {
// تحديث الكاش مع الاستجابة الجديدة
const responseClone = response.clone();
caches.open(CACHE_NAME)
.then(cache => cache.put(event.request, responseClone));
});
return cachedResponse;
}
// إذا لم تكن موجودة في الكاش، جلبها من الشبكة
return fetch(event.request)
.then(response => {
// تخزين الاستجابة الجديدة في الكاش
const responseClone = response.clone();
caches.open(CACHE_NAME)
.then(cache => cache.put(event.request, responseClone));
return response;
})
.catch(error => {
console.error('فشل الجلب:', error);
});
})
);
}
});
// معالجة رسائل التحديث
self.addEventListener('message', event => {
if (event.data.action === 'skipWaiting') {
self.skipWaiting();
}
});
// معالجة دفع الإشعارات
self.addEventListener('push', event => {
const data = event.data.json();
const options = {
body: data.body,
icon: '/icons/icon-192x192.png',
badge: '/icons/icon-192x192.png',
vibrate: [200, 100, 200],
data: {
url: data.url
}
};
event.waitUntil(
self.registration.showNotification(data.title, options)
);
});
// معالجة النقر على الإشعارات
self.addEventListener('notificationclick', event => {
event.notification.close();
event.waitUntil(
clients.openWindow(event.notification.data.url)
);
});