-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
86 lines (80 loc) · 3.23 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>sw test</title>
<meta id="meta-viewport" name="viewport"
content="user-scalable=yes, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<script src="js/assert.js"></script>
<script src="js/sw-client.js"></script>
<script src="js/debug-console.js"></script>
<link rel="manifest" href="manifest.json">
<style>
#status-messages {
--status-color: black;
font-weight: bold;
color: white;
background-color: var(--status-color);
padding: 10px;
border: solid 1px var(--status-color);
width: fit-content;
border-radius: 10px;
}
#status-messages.controller-present {
--status-color: #57c100;
}
#status-messages.controller-absent {
--status-color: indianred;
}
#time-stamp {
font-weight: bold;
color: white;
background-color: cornflowerblue;
padding: 10px;
border: solid 1px cornflowerblue;
width: fit-content;
border-radius: 10px;
margin-top: 1rem;
}
</style>
</head>
<body>
<h1>Service Worker Test</h1>
<div id="status-messages"></div>
<ol>
<li>Open the dev tools console ASAP and check the console messages to see what's happening in the background.</li>
<li>Simulate an upate by adding/removing spaces from the end of the <i>sw.js</i> file.</li>
<li>Press the 'Check for updates' button</li>
<li>Play with accepting and rejecting updates at different times.</li>
<li>Note what happens if an update is found when the page is NOT controlled by the service worker.</li>
<li>Go offline and then press 'check for updates'. </li>
</ol>
<p>
Note that changing any file other than the sw.js file <a href="https://developers.google.com/web/fundamentals/primers/service-workers/lifecycle#updates">does NOT count as an update</a>.
</p>
<button id="check-for-updates">Check for updates...</button>
<div id="time-stamp"></div>
<script type="module">
const debug = new DebugConsole(true, 'Application', 'cornflowerblue');
debug.heading('Application Starting up...');
// Status messages - so we can see what's going on.
let $messages = document.getElementById('status-messages');
let underControl = navigator.serviceWorker.controller;
let className = (underControl ? 'controller-present' : 'controller-absent');
let message = (underControl ? 'This page IS controlled by a Service Worker' : 'This page is NOT controlled by a Service Worker');
$messages.className = className;
$messages.innerHTML = message;
const swc = new ServiceWorkerClient('sw.js', debug, new SimpleUI(debug));
swc.register();
// The update button
const updateButton = document.getElementById('check-for-updates');
updateButton.addEventListener('click', function () {
swc.update(true);
});
// The load-time date stamp
const dateStamp = document.getElementById('time-stamp');
const loadDate = new Date();
dateStamp.innerHTML = `Last reload: ${loadDate.toDateString()} @ ${loadDate.toLocaleTimeString()}` ;
</script>
</body>
</html>