Skip to content

Commit 915c9f1

Browse files
Check if browser supports SW before using them (#298)
Check if browser supports SW before using them
2 parents 2436b9e + 8d1e42b commit 915c9f1

1 file changed

Lines changed: 12 additions & 10 deletions

File tree

src/pwa.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
let hasInteracted, shouldReload;
44

55
function sw() {
6-
navigator.serviceWorker.getRegistration().then(reg => {
7-
reg.onupdatefound = () => {
8-
const w = reg.installing;
9-
w.onstatechange = () => {
10-
if (w.state === 'installed' && navigator.serviceWorker.controller) {
11-
shouldReload = true;
12-
if (!hasInteracted) location.reload();
13-
}
6+
if ('serviceWorker' in navigator) {
7+
navigator.serviceWorker.getRegistration().then(reg => {
8+
reg.onupdatefound = () => {
9+
const w = reg.installing;
10+
w.onstatechange = () => {
11+
if (w.state === 'installed' && navigator.serviceWorker.controller) {
12+
shouldReload = true;
13+
if (!hasInteracted) location.reload();
14+
}
15+
};
1416
};
15-
};
16-
});
17+
});
18+
}
1719
}
1820

1921
if (!PRERENDER) {

0 commit comments

Comments
 (0)