-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
76 lines (67 loc) · 2.52 KB
/
index.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
// based on https://developers.google.com/web/tools/workbox/guides/advanced-recipes
module.exports = function initWorkboxRefresh(
registration,
{
render = defaultRenderUI,
textContent = "Updates loaded. Please click to apply.",
className = "cra-workbox-refresh"
} = {}
) {
// When the user asks to refresh the UI, we'll need to reload the window
var preventDevToolsReloadLoop;
navigator.serviceWorker.addEventListener("controllerchange", function() {
// Ensure refresh is only called once.
// This works around a bug in "force update on reload".
if (preventDevToolsReloadLoop) return;
preventDevToolsReloadLoop = true;
window.location.reload();
});
onNewServiceWorker(registration, function() {
const refresh = () =>
registration.waiting &&
registration.waiting.postMessage("skipWaiting");
render(registration, { textContent, className, refresh });
});
};
function defaultRenderUI(registration, { textContent, className } = {}) {
// Creates and injects a button to refresh the page.
var button = document.createElement("button");
button.style.position = "absolute";
button.style.bottom = "24px";
button.style.left = "24px";
button.className = className;
button.textContent = textContent;
button.addEventListener("click", function() {
if (!registration.waiting) {
// Just to ensure registration.waiting is available before
// calling postMessage()
return;
}
button.disabled = true;
registration.waiting.postMessage("skipWaiting");
});
document.body.appendChild(button);
}
function onNewServiceWorker(registration, callback) {
if (registration.waiting) {
// SW is waiting to activate. Can occur if multiple clients open and
// one of the clients is refreshed.
return callback();
}
function listenInstalledStateChange() {
registration.installing.addEventListener("statechange", function(
event
) {
if (event.target.state === "installed") {
// A new service worker is available, inform the user
callback();
}
});
}
if (registration.installing) {
return listenInstalledStateChange();
}
// We are currently controlled so a new SW may be found...
// Add a listener in case a new SW is found,
registration.addEventListener("updatefound", listenInstalledStateChange);
}