-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path54_service_workers.js
130 lines (120 loc) · 4.09 KB
/
54_service_workers.js
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
/**
* ========================================================
* Service Workers
* ========================================================
* Service Workers enable various powerful features like offline caching, background sync, and push notifications.
* They run in the background and act as a proxy between web applications and the network.
*/
/**
* ========================================================
* 1. Registering a Service Worker
* ========================================================
* To register a Service Worker, you use `navigator.serviceWorker.register`.
* This returns a promise that resolves with the ServiceWorkerRegistration object when the worker gets registered successfully.
*/
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/service-worker.js")
.then((registration) => {
console.log("Service Worker registered:", registration);
})
.catch((error) => {
console.log("Service Worker registration failed:", error);
});
}
/**
* ========================================================
* 2. The Install Event
* ========================================================
* The `install` event is fired when the Service Worker is being installed.
* This is an opportunity to cache assets for offline use using the Cache API.
*/
self.addEventListener("install", (event) => {
event.waitUntil(
caches.open("my-cache").then((cache) => {
return cache.addAll(["/", "/index.html", "/styles.css", "/script.js"]);
})
);
});
/**
* ========================================================
* 3. The Activate Event
* ========================================================
* The `activate` event fires when the Service Worker becomes active.
* This is a good place to manage old caches.
*/
self.addEventListener("activate", (event) => {
console.log("Service Worker activated");
});
/**
* ========================================================
* 4. The Fetch Event
* ========================================================
* The `fetch` event can be used to intercept network requests.
* You can respond to this event by fetching a resource from the cache or network.
*/
self.addEventListener("fetch", (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
return response || fetch(event.request);
})
);
});
/**
* ========================================================
* Nuances and Advanced Techniques
* ========================================================
*/
/**
* 1. Skip Waiting
* ---------------
* The `skipWaiting` method allows a service worker to skip the 'waiting' lifecycle phase and move to 'active'.
*/
self.skipWaiting();
/**
* 2. Clients Claim
* ----------------
* Using `clients.claim`, a newly activated Service Worker can immediately control all pages under its scope.
*/
self.clients.claim();
/**
* 3. Background Sync
* ------------------
* The 'sync' event enables deferred actions to be retried when the user has network connectivity.
*/
self.addEventListener("sync", (event) => {
if (event.tag === "myBackgroundSync") {
// Your background sync logic here
}
});
/**
* 4. Push Notifications
* ----------------------
* Service Workers can receive push messages and show local notifications to the user.
*/
self.addEventListener("push", (event) => {
const title = "New Notification";
const options = {
body: "This is a sample push notification!",
};
event.waitUntil(self.registration.showNotification(title, options));
});
/**
* 5. Cache Versioning
* -------------------
* Multiple versions of cached assets can be managed by maintaining a cache version identifier.
*/
const cacheName = "my-cache-v1";
self.addEventListener("activate", (event) => {
event.waitUntil(
caches.keys().then((keyList) => {
return Promise.all(
keyList.map((key) => {
if (key !== cacheName) {
return caches.delete(key);
}
})
);
})
);
});