I am trying to install a service worker in React app. But when i try to register it, it fails with:
Uncaught (in promise) TypeError: Failed to register a ServiceWorker: ServiceWorker script evaluation failed
My code is in App.js and the code inside of it is:
import React, { Component } from 'react';
class App extends Component {
constructor(){
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('./sw.js')})}
}
render() {
return (
<div className="App">
<h1>PWA Go!</h1>
</div>
);
}
}
Also, all of my files are in the src folder. The files are:
index.js
App.js
offline.html
sw.js
My service worker(sw.js) has this code inside it:
this.addEventListner(`install`, (e) => {
e.waitUntil(
caches.open(`V1`)
.then((ref) => {
ref.add(`offline.html`)
})
.catch((err) => {
console.log(`Something went wrong here : ${err}`);
})
);
});
But I still keep on getting the above mentioned error. If anyone knows what I am doing wrong, please tell me. Thanks for helping me!