-
Notifications
You must be signed in to change notification settings - Fork 15
/
service-worker.js
168 lines (136 loc) · 4.54 KB
/
service-worker.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
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
import { precacheAndRoute, createHandlerBoundToURL } from "workbox-precaching";
import { NavigationRoute, registerRoute } from "workbox-routing";
import { setCacheNameDetails, clientsClaim } from "workbox-core";
import {
CacheFirst,
NetworkFirst,
StaleWhileRevalidate,
} from "workbox-strategies";
import { ExpirationPlugin } from "workbox-expiration";
import { CacheableResponsePlugin } from "workbox-cacheable-response";
import { googleFontsCache, imageCache } from "workbox-recipes";
import { BackgroundSyncPlugin } from "workbox-background-sync";
import { BroadcastUpdatePlugin } from "workbox-broadcast-update";
// SETTINGS
// Claiming control to start runtime caching asap
clientsClaim();
// Use to update the app after user triggered refresh
//self.skipWaiting();
// Setting custom cache names
setCacheNameDetails({ precache: "wb6-precache", runtime: "wb6-runtime" });
// PRECACHING
// Precache and serve resources from __WB_MANIFEST array
precacheAndRoute(self.__WB_MANIFEST);
// NAVIGATION ROUTING
// This assumes /index.html has been precached.
const navHandler = createHandlerBoundToURL("/index.html");
const navigationRoute = new NavigationRoute(navHandler, {
denylist: [new RegExp("/out-of-spa/")], // Also might be specified explicitly via allowlist
});
registerRoute(navigationRoute);
// RUNTIME CACHING
// Load details immediately and check for update right after
registerRoute(
new RegExp("https://progwebnews-app.azurewebsites.net.*content/posts/slug.*"),
new StaleWhileRevalidate({
cacheName: "wb6-post",
plugins: [
new ExpirationPlugin({
// Only cache requests for a week
maxAgeSeconds: 7 * 24 * 60 * 60,
}),
new BroadcastUpdatePlugin(),
],
})
);
// Keeping lists always fresh
registerRoute(
new RegExp("https://progwebnews-app.azurewebsites.net.*content/posts.*"),
new NetworkFirst()
);
// Avatars can live in cache
registerRoute(
({ url }) => url.hostname.includes("gravatar.com"),
new CacheFirst({
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200],
}),
],
})
);
// STATIC RESOURCES
googleFontsCache({ cachePrefix: "wb6-gfonts" });
// CONTENT
imageCache({ cacheName: "wb6-content-images", maxEntries: 10 });
// APP SHELL UPDATE FLOW
addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});
// BACKGROUND SYNC
// Instantiating and configuring plugin
const bgSyncPlugin = new BackgroundSyncPlugin("feedbackQueue", {
maxRetentionTime: 24 * 60, // Retry for max of 24 Hours (specified in minutes)
});
// Registering a route for retries
registerRoute(
// Alternative notation: ({url}) => url.pathname.startsWith('/post-tweet'),
/(http[s]?:\/\/)?([^\/\s]+\/)post-tweet/,
new NetworkFirst({
plugins: [bgSyncPlugin],
}),
"POST"
);
// ALL OTHER EVENTS
// Receive push and show a notification
self.addEventListener("push", function (event) {
console.log("[Service Worker]: Received push event", event);
var notificationData = {};
if (event.data.json()) {
notificationData = event.data.json().notification;
} else {
notificationData = {
title: "Something Has Happened",
message: "Something you might want to check out",
icon: "/assets/images/logo.png",
};
}
self.registration.showNotification(notificationData.title, notificationData);
});
// Custom notification actions
self.addEventListener("notificationclick", function (event) {
console.log("[Service Worker]: Received notificationclick event");
event.notification.close();
if (event.action == "opentweet") {
console.log("[Service Worker]: Performing action opentweet");
event.waitUntil(
clients.openWindow(event.notification.data).then(function (windowClient) {
// do something with the windowClient.
})
);
} else {
console.log("[Service Worker]: Performing default click action");
// This looks to see if the current is already open and
// focuses if it is
event.waitUntil(
clients
.matchAll({
includeUncontrolled: true,
type: "window",
})
.then(function (clientList) {
for (var i = 0; i < clientList.length; i++) {
var client = clientList[i];
if (client.url == "/" && "focus" in client) return client.focus();
}
if (clients.openWindow) return clients.openWindow("/");
})
);
}
});
// Closing notification action
self.addEventListener("notificationclose", function (event) {
log("[Service Worker]: Received notificationclose event");
});