-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-sw-workbox.js
318 lines (256 loc) · 14.1 KB
/
install-sw-workbox.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { Workbox } from 'workbox-window/Workbox.mjs';
const minimumRequirementsCheck = () => {
if (! ('serviceWorker' in navigator)) {
throw new Error('No Service Worker support!')
}
// if (! ('Notification' in window)) {
// throw new Error('No Notification Support!')
// }
// if (! window.Notification || ! Notification.requestPermission) {
// console.error('This browser does not support desktop notification 😔')
// return
// }
}
window.addEventListener('online', (event) => {
// Resync data with server.
console.info('You are online');
}, false);
window.addEventListener('offline', function (e) {
// Queue up events for server.
console.warn('You are offline');
}, false);
minimumRequirementsCheck()
try {
if ('serviceWorker' in navigator &&
'caches' in window &&
'indexedDB' in window) {
function urlBase64ToUint8Array(base64String) {
let padding = '='.repeat((4 - (base64String.length % 4)) % 4);
let base64 = (base64String + padding)
.replace(/\-/g, '+')
.replace(/_/g, '/');
let rawData = window.atob(base64);
let outputArray = new Uint8Array(rawData.length);
for (let i = 0; i < rawData.length; ++i) {
outputArray[i] = rawData.charCodeAt(i);
}
return outputArray;
}
const VAPID_PUBLIC_KEY = process.env.MIX_VAPID_PUBLIC_KEY;
async function enableNotifications(pushManager) {
const swalActivatedzNotificationSuccessfully = () => {
Swal.mixin({customClass: {popup: 'popup-request-notification'}})
.fire({
icon: 'success',
position: 'top-end',
text: 'Agora você receberá notificações sempre que um novo post for criado 🎉',
showCloseButton: true,
showConfirmButton: false,
});
}
try {
const permission = await Notification.requestPermission()
if (permission !== 'granted') {
console.warn('User blocked notifications 😟');
throw new Error('We weren\'t granted permission.');
}
if (permission === 'granted' && (! window.PushManager)) {
swalActivatedzNotificationSuccessfully()
return;
}
// https://web.dev/push-notifications-in-all-modern-browsers
// https://caniuse.com/push-api
// https://developer.mozilla.org/en-US/docs/Web/API/PushEvent/PushEvent#browser_compatibility
if (! ('PushManager' in window)) {
Swal.mixin({customClass: {popup: 'popup-request-notification'}})
.fire({
icon: 'info',
position: 'top-end',
html: '<b>Seu navegador não suporta o envio de notificações push na web.</b>',
footer: '<a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/PushEvent/PushEvent#browser_compatibility">Confira aqui a lista de navegadores compatíveis 👍</a>',
showCloseButton: true,
showConfirmButton: false,
});
throw new Error('Browser does not support Push Messages!')
}
let subscription = null
const subscribed = await pushManager.getSubscription()
if (subscribed) { // User is already subscribed
subscription = subscribed
} else {
const serverKey = urlBase64ToUint8Array(VAPID_PUBLIC_KEY);
subscription = await pushManager.subscribe({userVisibleOnly: true, applicationServerKey: serverKey});
}
const key = subscription.getKey('p256dh')
const token = subscription.getKey('auth')
const contentEncoding = (PushManager.supportedContentEncodings || ['aesgcm'])[0]
const data = {
endpoint: subscription.endpoint,
public_key: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null,
auth_token: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null,
encoding: contentEncoding,
};
let res = await axios.post('/notifications/subscribe', data, {
'Accept': 'application/json',
'Content-Type': 'application/json',
});
swalActivatedzNotificationSuccessfully()
} catch (error) {
console.error(error);
}
}
async function addUserToUptimeNotifications(pushManager) {
const subscription = await pushManager.getSubscription()
const key = subscription.getKey('p256dh')
const token = subscription.getKey('auth')
const contentEncoding = (PushManager.supportedContentEncodings || ['aesgcm'])[0]
const data = {
endpoint: subscription.endpoint,
public_key: key ? btoa(String.fromCharCode.apply(null, new Uint8Array(key))) : null,
auth_token: token ? btoa(String.fromCharCode.apply(null, new Uint8Array(token))) : null,
encoding: contentEncoding,
};
axios.post('/notifications/add-user-to-uptime-monitor', data, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
}
}).then(function (response) {
Swal.fire({
text: 'Você foi adicionado com sucesso para receber notificações do Health Check dos monitores do UPTIME',
icon: "success"
});
})
.catch(function (error) {
console.error(error.response);
Swal.fire({
text: JSON.stringify(error.response.data),
icon: "error"
});
});
}
const pushNotificationAccepted = () => window.localStorage.getItem('acceptPushNotifications');
(async () => {
const wb = new Workbox('/service-worker.min.js');
let registration;
// Assuming the user accepted the update, set up a listener
// that will reload the page as soon as the previously waiting
// service worker has taken control.
wb.addEventListener('controlling', (event) => {
// At this point, reloading will ensure that the current
// tab is loaded under the control of the new service worker.
// Depending on your web app, you may want to auto-save or
// persist transient state before triggering the reload.
window.location.reload();
});
wb.addEventListener('installed', async (event) => {
// If this is not the very first service worker install (event.isUpdate === true),
// it means a newer version of the service worker has been found and
// installed (that is, a different version from the one currently controlling the page).
if (! event.isUpdate) {
// First-installed code goes here...
console.info('Service worker [INSTALLED] for the first time!');
} else {
console.info('Service worker [INSTALLED]!');
}
});
wb.addEventListener('activated', (event) => {
// `event.isUpdate` will be true if another version of the service
// worker was controlling the page when this version was registered.
if (! event.isUpdate) {
console.info('Service worker [ACTIVATED] for the first time!');
// If your service worker is configured to precache assets, those
// assets should all be available now.
} else {
console.info('Service worker [ACTIVATED]!');
}
});
wb.addEventListener('waiting', (event) => {
console.info(`A new service worker has installed, but it can't activate until all tabs running the current version have fully unloaded.`);
// Send a message to the waiting service worker, instructing it to activate.
// same as => wb.messageSW({type: 'SKIP_WAITING'});
wb.messageSkipWaiting();
});
wb.addEventListener('message', async event => {
if (! event.data) {
return;
}
switch (event.data.type) {
case 'RELOAD_WINDOW':
window.location.reload();
break;
default:
// NOOP
break;
}
if (event.data.type === 'CACHE_UPDATED' && event.data.meta === 'workbox-broadcast-update') {
const {cacheName, updatedURL} = event.data.payload;
const cache = await caches.open(cacheName);
const updatedResponse = await cache.match(updatedURL);
const updatedText = await updatedResponse.text();
const doc = (new DOMParser()).parseFromString(updatedText, 'text/html');
const title = doc.getElementById('post-title').textContent.trim();
registration.showNotification('Conteúdo do post foi atualizado ✍️', {
body: `Clique para ver o novo conteúdo de: ${title}`,
icon: '/images/favicons/favicon-192x192.png',
tag: 'post-update-notification',
data: {
link: updatedURL.replace("/index.content.html?content=true", "").trim(),
title: title,
},
vibrate: true,
requireInteraction: false,
});
}
if (event.data.type === 'HI_SERVICE_WORKER') {
console.log('Hi service worker');
}
});
// Register the service worker after event listeners have been added.
registration = await wb.register();
const swVersion = await wb.messageSW({type: 'GET_VERSION'});
console.info('Service Worker Version: ', swVersion);
})();
// At this point, a Service Worker is controlling the current page
navigator.serviceWorker.ready.then((registration) => {
let serviceWorker = registration.active;
console.log(`A service worker is [${serviceWorker.state}] 🚀`);
// If the key does not exist, `null` is returned
if (serviceWorker?.state === 'activated' && pushNotificationAccepted() === null && window.location.pathname.match(/^\/blog\/.*/)?.length) {
const swalCustomButtons = Swal.mixin({
customClass: {
popup: 'popup-request-notification',
confirmButton: 'btn confirm-button without-style icon solid fa-bell primary',
cancelButton: 'btn without-style light small'
},
buttonsStyling: false
});
swalCustomButtons.fire({
position: 'top-end',
html: '<b>Ative as notificações</b> para receber as <i>últimas atualizações</i> do blog.',
confirmButtonText: 'Ativar',
showCancelButton: true,
cancelButtonText: 'Agora não',
reverseButtons: true,
}).then((result) => {
if (result.isConfirmed) {
window.localStorage.setItem('acceptPushNotifications', true);
enableNotifications(registration.pushManager);
} else {
window.localStorage.setItem('acceptPushNotifications', false);
}
});
}
document.querySelector('.enable-notifications')
.addEventListener('click', () => enableNotifications(registration.pushManager));
let btnUserToUptimeNotifications = document.querySelector('.add-user-to-uptime-notifications');
if (btnUserToUptimeNotifications) {
btnUserToUptimeNotifications.addEventListener('click', () => addUserToUptimeNotifications(registration.pushManager));
}
});
} else {
console.warn("Service Worker is not supported in this browser 😔");
}
} catch (err) {
console.err(err);
}