To implement this service worker in your project, you must copy the service-worker.js
file to the root directory of the webapp/web page. You must then amend your index.html
to contain the following script
tag:
<script>
// This makes sure that Service Workers are supported before registering.
if ('serviceWorker' in navigator) {
navigator.serviceWorker
.register('/service-worker.js')
.then((registration) => {
console.log(`Service Worker Registered at scope: ${registration.scope}`);
})
.catch((error) => {
console.error(`Service Worker failed registration: ${error}`);
})
}
</script>
You can check to see if your service worker is registering by opening the webapp/web page in Chrome, opening the Dev Tools and navigating to the 'Application' tab and selecting the 'Service Worker' heading.