Register deleted service worker

286 views Asked by At

Hey I am really struggeling with polymer 2.x after polymer init my sw did not work so I decided to unregister it from

chrome://serviceworker-internals/

following to this answer after that all I get is this:

sw

How can I enable the service worker again?

1

There are 1 answers

2
Ofisora On

You need to add the following code in your application, after first render or whenever you want, to register the service worker:

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('/service-worker.js');
}

If supported this code will register the service worker.

If you want to add in index.html:

<script>
      const baseUrl = document.querySelector('base').href;

      if ('serviceWorker' in navigator) {
        window.addEventListener('load', function() {
          navigator.serviceWorker.register(baseUrl + 'service-worker.js');
        });
      }
</script>