Unable to deploy service worker on Chrome browser

I am currently attempting to implement service workers in Chrome on localhost:3000. My application is built using AngularJS 1.5.

The service worker does not seem to be activating properly. It transitions from the installing state to redundant.

if ('serviceWorker' in navigator) {
  navigator.serviceWorker
    .register('/service-worker.js')
    .then(function(registration) {
        var serviceWorker;
        if (registration.installing) {
            serviceWorker = registration.installing;
        } else if (registration.waiting) {
            serviceWorker = registration.waiting;
        } else if (registration.active) {
            serviceWorker = registration.active;
        }

        if (serviceWorker) {
            console.log("ServiceWorker phase:", serviceWorker.state);

            serviceWorker.addEventListener('statechange', function (e) {
                console.log("ServiceWorker phase:", e.target.state);
            });
        }
    })
    .catch(function(err) {
        console.log('Service Worker Error', err);
    });
}

Output from the above script:

ServiceWorker phase: installing

ServiceWorker phase: redundant

You can find the service-worker.js file at http://localhost:3000/service-worker.js. I have not encountered any error messages. Can anyone help identify what might be causing this issue?

Answer №1

Encountered a similar issue and tackled it successfully by following these steps:

  • Firstly, navigate to the page where the service worker is active
  • Access
    chrome://inspect/#service-workers
    on your Chrome browser
  • Locate the redundant service worker and terminate it by clicking on it

https://i.sstatic.net/uCNSB.png

  • Next, refresh the page with the service worker

  • Open the developer console, go to the application tab, and then check the service worker section. Your service worker should now display a green icon.

  • Include the following function in your Service Worker code:

    self.addEventListener("activate", function(event) {
      event.waitUntil(self.clients.claim());
    });

https://i.sstatic.net/zN7EQ.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Calling a class like a function in Node.js Express is not allowed

Greetings! I've been experimenting with ES6 syntax to develop a middleware for my node.js application. Here is the code in index.js: export default class Middleware { constructor() { //do nothing } fun1 = (req, res, next) => { cons ...

Ways to divide the value of an input text box using Angular

Given the value "<HR>*10/100", how can I split it into three parts: "HR", "*", and "10/100"? HTML <input type='text' ng-model='value' ng-change="GetAngValue(value)"> {{Result}} Angular $scope.GetAngValue = function (val ...

Functionality of jQuery Mobile Page

$(document).on("pageshow", "#mappage", function (event) { $("#doldur").append("<li> <label onclick='getBina(" + featuressokak[f].attributes.SOKAKID + ");'>" ...

Methods for transferring data to controller in Angular Modal service

After implementing the angular modal service discussed in this article: app.service('modalService', ['$modal', function ($modal) { var modalDefaults = { backdrop: true, keyboard: true, m ...

PHP redirect malfunctioning, yet still functioning?

After making some changes to the structure of my website, I seem to have broken the script somehow. When a user fills out a form correctly, they should be redirected to the appropriate page. However, the page just hangs. Strangely, the form works fine when ...

Vuejs tutorial: Toggle a collapsible menu based on the active tab status

I am using two methods called forceOpenSettings() and forceCloseSettings() to control the opening and closing of a collapsible section. These methods function properly when tested individually. However, I need them to be triggered based on a specific condi ...

The forward button is malfunctioning after using history.pushState

I have managed to resolve issues with the back button, however, I am still unable to fix the forward button. Despite the URL changing, the page does not reload. Here is the code snippet that I am currently using: $('.anchor .wrapper').css({ &a ...

Including files in node package without specifying the name of the dist directory

In my library directory structure for seamless import by node js projects, it looks like this: my-lib/ ├─ dist/ │ ├─ index.js │ ├─ foo.js ├─ src/ │ ├─ index.ts │ ├─ foo.ts ├─ package.json Take a look at my package.j ...

Using JavaScript to plot JSON information on a map

Currently, I have a JSON dataset that needs to be reformatted into a different structure. This is the original JSON data: { "info": { "file1": { "book1": { "lines": { "102:0": [ "102:0" ], "10 ...

Issues with the display property

After setting the display of a div tag as none in the style property, I am trying to show it based on an on-click function. However, the issue I am facing is that when I click on the function, the div displays for a brief moment then disappears again. Ca ...

Postman is displaying [object object] as the return value, but the actual value is not

Currently, I am working on automating tasks using Postman One interesting aspect is the presence of a vehicles array in the body { "Vehicles":[ { "car":"{{car}}", "bike":"{{bike}}&quo ...

Component nesting is not supported; cannot render a component inside a

I've set up a simple Vue application within Rails, but I am encountering an issue: hello_vue.js: import Vue from 'vue/dist/vue.esm' import TurbolinksAdapter from 'vue-turbolinks' Vue.use(TurbolinksAdapter) import CollectionSet f ...

Incorporating TypeScript basics into the if statement post compiling

As I delve into the Angular2 Quickstart, I stumbled upon a peculiar issue within app.component.js after compiling app.component.ts using tsc (version 1.8.2): if (d = decorators[i]) I am unable to pinpoint where I may have gone wrong in configuring the qu ...

Combining multiple dictionaries into one single dictionary array using JavaScript

In my JavaScript code, I am working with an array that looks like this: arr = [{"class":"a"},{"sub_class":"b"},{"category":"c"},{"sub_category":"d"}] My goal is to transform t ...

Implementing auto-suggest functionality in a React-Bootstrap Select component

I am seeking to create a specialized react select component that can search through a constantly updating list pulled from a server api in JSON format. I am currently exploring other options aside from React-select because it is not compatible with my proj ...

jQuery Validation for Radio Buttons and Optional Fields

I've been pondering over this issue for a couple of hours now. Currently, my focus is on validating a form using a jQuery plugin. Here's the link to the documentation: http://docs.jquery.com/Plugins/Validation Here's the code I have so fa ...

Provide a response containing JSON data extracted from an HTML file

I am looking for a way to create a simple REST API using a hosting site that only allows hosting of HTML files along with JavaScript files. I want to be able to send POST/GET requests and receive JSON data in response. For instance, when making a GET requ ...

Displaying a loader in AngularJS on every page

I'm currently working on creating a loader that will appear on every page and fade out once all the content is loaded. Here's what I have so far: <div id="loader-wrapper" data-loader-drct data-ng-hide="hidden"> <div id="loader"& ...

When using the updateMany function, only update the field if it already exists, without creating a new key

My goal within the $set operation is to update a field only if it already exists. While $cond seems to be on the right track, it has the drawback of always creating the field, which is not what I want. Filtering to retrieve only existing fields seems like ...

Error: JSON parsing error - Unexpected token U found at the beginning of JSON string while making a POST request

When I make a post request to an API using the submit() function attached to an ng-click directive, sending data in JSON format, it returns an error. The request works fine on postman, so the error seems to be on the client side only. Additionally, the e ...