0

I have a PWA. I have wrapped the application as cordova application. But I see that Service worker is not getting registered. I get error "ServiceWorker registration failed: DOMException: Failed to register a ServiceWorker: The URL protocol of the current origin ('file://') is not supported."

I can see there are a few plugins for IOS that helps in registering the Service Worker. Is there a way to register the service worker in Android platform?

1 Answers1

1

Based from this thread, service worker only works in secure mode either in https or localhost. It doesn't work in local resources like file:// or http.

The error message indicates that your browser is refusing to use a resource because the origin is null - which happens often for local (file://) resources. service-worker.js is local - I'll bet this is why you're getting the cross origin resource issue

abielita
  • 13,147
  • 2
  • 17
  • 59
  • 1
    Your are totally correct! I used [cordova-plugin-ionic-webview](https://github.com/ionic-team/cordova-plugin-ionic-webview) to set the scheme to **https** and I was able to register a service worker! – KenCorbettJr May 30 '19 at 22:02
  • 1
    @KenCorbettJr error Unhandled Promise rejection: Failed to register a ServiceWorker for scope ('https://localhost/') with script ('https://localhost/sw.js'): An unknown error occurred when fetching the script. ; Zone: ; Task: Promise.then ; Value: TypeError: Failed to register a ServiceWorker for scope ('https://localhost/') with script ('https://localhost/sw.js'): An unknown error occurred when fetching the script. TypeError: Failed to register a ServiceWorker for scope ('https://localhost/') with script ('https://localhost/sw.js'): An unknown error occurred when fetching the script. – Shubham Takode Dec 15 '19 at 09:27
  • @KenCorbettJr do you have any idea ? – Shubham Takode Dec 15 '19 at 09:27