Skip to content

Commit

Permalink
feat: support offline mode via service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
seleb committed Mar 6, 2021
1 parent eda59ec commit f65b598
Show file tree
Hide file tree
Showing 4 changed files with 2,220 additions and 7 deletions.
29 changes: 29 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,35 @@
}
}
</script>
<script>
// https://www.bha.ee/how-to-make-your-parcel-js-app-progressive/
// this is needed because parcel will not try to resolve this as a file
const sw = "service-worker.js";
navigator.serviceWorker
.register(sw)
.then(registration => {
registration.onupdatefound = () => {
const installingWorker = registration.installing;
if (installingWorker == null) {
return;
}
installingWorker.onstatechange = () => {
if (installingWorker.state === "installed") {
if (navigator.serviceWorker.controller) {
console.log(
"New content is available and will be used when all tabs for this page are closed."
);
} else {
console.log("Content is cached for offline use.");
}
}
};
};
})
.catch(error => {
console.error("Error during service worker registration:", error);
});
</script>
</head>

<body>
Expand Down
Loading

0 comments on commit f65b598

Please sign in to comment.