5

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!

10
  • Which browser (and its version) are you using? Commented Aug 24, 2018 at 3:53
  • Chrome Version 68.0.3440.106 (Official Build) (64-bit) Commented Aug 24, 2018 at 3:55
  • I've tried to register the same service worker with a different javascript file in different project(Non-React project) and it worked fine! But when i am using it with React it screws up!! Commented Aug 24, 2018 at 3:57
  • Might be to do with "addEventListner" and not "addEventListener" ? Commented Aug 24, 2018 at 4:00
  • Nope...Tried it right now....problem still exists! Commented Aug 24, 2018 at 4:02

1 Answer 1

2

That error means you're having exceptions in your service worker file sw.js so service worker can not evaluate the related code. It can either be a syntax error or a runtime exception. In this case, this.addEventListner must be this.addEventListener instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.