23

I created a new project with create-react-app then ran npm start then this showed up. https://i.stack.imgur.com/GzXmh.jpg

Using FF 69.0.3

Anurag Srivastava
  • 14,077
  • 4
  • 33
  • 43
Dwyte
  • 451
  • 1
  • 3
  • 15

6 Answers6

17

This problem seems to only happen in Firefox. Edge worked fine.

After creating the project, in index.js change:

serviceWorker.register();
//serviceWorker.unregister();

Still trying to figure whose fault this is, but this should get you going.

david.pfx
  • 10,520
  • 3
  • 30
  • 63
  • this registers the serviceworker, which comes with a bunch of caveats https://create-react-app.dev/docs/making-a-progressive-web-app/#why-opt-in – tobbe Nov 21 '19 at 09:28
  • 1
    @tobbe: Indeed, but do you have an answer for why it happens and a better way to fix it? – david.pfx Nov 22 '19 at 12:55
7

Another solution found in the link in the answer by Niraj Niroula:

If you want to keep the Delete cookies and site data when Firefox is closed option enabled, click on the Manage Exceptions button and add localhost (with the protocol and port) to the exceptions list:

cookie and site data deletion exceptions

g-otn
  • 293
  • 4
  • 9
5

Seems that the issue exists only in Firefox. Anyone still bugged by this bug can try disabling Delete cookies and site data when Firefox is closed option in Privacy and Security settings. enter image description here Or try wrapping the navigator.serviceWorker usages inside a try/catch. In my case

  if ("serviceWorker" in navigator) {
    navigator.serviceWorker.ready
      .then(registration => {
        registration.unregister();
      })
      .catch(error => console.log("ServiceWorker Warning: ", error));
  }

For related discussion and more info

Niraj Niroula
  • 2,376
  • 1
  • 17
  • 36
  • Adding the `.catch(error => console.log("ServiceWorker Warning: ", error));` line fixed it for me. Thank you! – Des Aug 04 '22 at 14:48
4

A lot of answers here do actually solve the issue but the simplest way I have found is to add npm package serve to your dependencies.

yarn add serve or npm i serve

and then replace your start script with the following:

"scripts": {
    "start": "serve -s build",
}

This is actually straight out of the create-react-app docs

Leo Policastro
  • 1,062
  • 1
  • 8
  • 16
3

I had this error occur as well. Was able to clear it up by deleting the create-react-app serviceWorker.js file

Kory
  • 31
  • 1
-1

For me, it was a malformed URL in the call to the route() function in PreactJS.

I basically added a slash too much when integrating the process.env.PUBLIC_PATH variable into my links like so:

export function Example(props: {}) {
  const callback = useCallback((e: JSX.TargetedEvent<HTMLButtonElement>) => {
    route(`${process.env.PUBLIC_PATH}/my/path`)
//                                   ^
//                                   That slash is too much
  }, [])
  return <button onClick={callback}>Click me</button>
}

After clicking the button, the URL looked something like this in my Chrome location bar:

https://localhost/my-app//my/path
                        ^^
                        Here is the forbidden double slash

Interestingly, only Firefox reported this to me, while Chrome happily rendered the page and ignored the double slash.

The solution was simply to remove the unnecessary slash, since env.process.PUBLIC_PATH always needs to end with a slash:

route(`${process.env.PUBLIC_PATH}my/path`)
//                              ^^
//                              No extra slash fixed it!
Roberto
  • 713
  • 8
  • 14