Service Worker is not working in my angular 14 version

81 views Asked by At

I Integrate ,service worker in my angular version 14, it will not working

I got an console error

main.e5bc1c66576092c1.js:1 Service worker registration failed with: Type Error: Failed to register a Service Worker for scope ('http://127.0.0.1:8080/') with script ('http://127.0.0.1:8080/service-worker.js'): A bad HTTP response code (404) was received when fetching the script.

my service worker script is it won't work

*self.addEventListener("sync", (event) => { if(event.tag == 'postData'){ event.waitUntil(addData()); //this fetch method is reject then this method will try again after user will get connection alert('Event.waitUntil') } });*

and my component code for call the service worker is

postSync(){ //api call let user = { name : 'kumar', age : 12 } //during the internet it continue the server call else it call the service worker this.httpservice.post(${baseUrl}/checkBackgoundSync,user).subscribe({ next : (res) =>{ console.log("Response",res); },error : (err) =>{ console.log("Error",err); alert('hii') this.backgroundSync(); } }) }

backgroundSync(){ alert('Background sync') navigator.serviceWorker.ready .then( (swRegistration : any) =>{ swRegistration.sync.register('postData') console.log("Then backgounf"); alert("Then") }) .catch(()=>{ alert('catch') console.log("Wrong"); }) can any one please help me to sort out this problem

0

There are 0 answers