generated from pwa-builder/pwa-starter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpwabuilder-sw.js
199 lines (168 loc) · 5.15 KB
/
pwabuilder-sw.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
importScripts(
"https://storage.googleapis.com/workbox-cdn/releases/5.1.1/workbox-sw.js"
);
importScripts(
"https://cdn.jsdelivr.net/npm/idb-keyval@3/dist/idb-keyval-iife.min.js"
);
const syncContent = async () => {
if (navigator.connection.effectiveType === "4g") {
const token = await idbKeyval.get("graphToken");
if (token) {
const headers = new Headers();
const bearer = "Bearer " + token;
headers.append("Authorization", bearer);
const options = {
method: "GET",
headers: headers,
};
const graphEndpoint = `https://graph.microsoft.com/beta/me/messages`;
const response = await fetch(graphEndpoint, options);
const cache = await caches.open("offline-mail");
if (cache) {
const cacheResp = await cache.matchAll(graphEndpoint);
cacheResp.forEach(async (element, index, array) => {
await cache.delete(element);
});
await navigator.setAppBadge();
cache.put(graphEndpoint, response);
if (Notification.permission === "granted") {
const options = {
body: "New email was received in the background",
icon: "/assets/icons/icon_48.png",
vibrate: [100],
data: {
dateOfArrival: Date.now(),
},
actions: [
{ action: "open", title: "Open" },
{ action: "close", title: "Close" },
],
};
await registration.showNotification("New mail available", options);
}
}
}
}
};
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});
workbox.routing.registerRoute(
({ url }) =>
url.href.includes("https://graph.microsoft.com/beta/me/messages"),
new workbox.strategies.StaleWhileRevalidate({
cacheName: "offline-mail",
plugins: [
new workbox.expiration.ExpirationPlugin({
maxEntries: 50,
maxAgeSeconds: 19 * 60, // 5 minutes
}),
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
],
})
);
const bgSyncPlugin = new workbox.backgroundSync.BackgroundSyncPlugin(
"sentEmail",
{
maxRetentionTime: 24 * 60, // Retry for max of 24 Hours (specified in minutes)
}
);
const attachBgSyncPlugin = new workbox.backgroundSync.BackgroundSyncPlugin(
"attemptedAttach",
{
maxRetentionTime: 24 * 60, // Retry for max of 24 Hours (specified in minutes)
}
);
const unsubBgSyncPlugin = new workbox.backgroundSync.BackgroundSyncPlugin(
"unsubAttempts",
{
maxRetentionTime: 24 * 60, // Retry for max of 24 Hours (specified in minutes)
}
);
workbox.routing.registerRoute(
({ url }) => url.href.includes("me/sendEmail"),
new workbox.strategies.NetworkOnly({
plugins: [bgSyncPlugin],
}),
"POST"
);
workbox.routing.registerRoute(
({ url }) => url.href.includes("/$value"),
new workbox.strategies.NetworkOnly({
plugins: [attachBgSyncPlugin],
})
);
workbox.routing.registerRoute(
({ url }) => url.href.includes("/unsubscribe"),
new workbox.strategies.NetworkOnly({
plugins: [unsubBgSyncPlugin],
}),
"POST"
);
self.addEventListener("notificationclick", (event) => {
const notification = event.notification;
const action = event.action;
if (action === "close") {
notification.close();
} else {
clients.openWindow(
notification.body.substring(notification.body.indexOf("https"))
);
notification.close();
}
});
self.addEventListener("periodicsync", (event) => {
if (event.tag === "mail-sync") {
event.waitUntil(syncContent());
}
});
async function shareTargetHandler({ event }) {
// event.respondWith(Response.redirect("/newEmail"));
const formData = await event.request.formData();
const mediaFiles = formData.getAll("file");
const cache = await caches.open("shareTarget");
for (const mediaFile of mediaFiles) {
await cache.put(
// TODO: Handle scenarios in which mediaFile.name isn't set,
// or doesn't include a proper extension.
mediaFile.name,
new Response(mediaFile, {
headers: {
"content-length": mediaFile.size,
"content-type": mediaFile.type,
},
})
);
}
return Response.redirect(`/newEmail?name=${mediaFiles[0].name}`, 303);
}
workbox.routing.registerRoute("/attach/file/", shareTargetHandler, "POST");
workbox.routing.registerRoute(
({ url }) => url.href.includes("comlink"),
new workbox.strategies.CacheFirst()
);
workbox.routing.registerRoute(
({ url }) => url.href.includes("fast-components"),
new workbox.strategies.CacheFirst()
);
workbox.routing.registerRoute(
({ url }) => url.href.includes("ionic"),
new workbox.strategies.CacheFirst()
);
workbox.routing.registerRoute(
({ url }) => url.href.includes("@pwabuilder"),
new workbox.strategies.CacheFirst()
);
workbox.routing.registerRoute(
({ url }) => url.href.includes("/me/drive/recent/"),
new workbox.strategies.StaleWhileRevalidate()
);
workbox.routing.registerRoute(
({ url }) => url.href.includes("/mailFolders"),
new workbox.strategies.StaleWhileRevalidate()
);
workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);