question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Connection refused when call ngsw-worker.js file and Service worker is not registering

See original GitHub issue

I can’t register service worker in capacitor webview I used angular and capacitor 1.0.0-beta.24

I think that URL schema need to be https which service worker required, How to make this https ? or what is the suitable solution to register SW My package.json is :

image

and the problem is : (This is from Android Emulator)

Failed to load resource: net::ERR_CONNECTION_REFUSED
Uncaught Error: Uncaught (in promise): TypeError: Failed to register a ServiceWorker: An unknown error occurred when fetching the script.
TypeError: Failed to register a ServiceWorker: An unknown error occurred when fetching the script.

image

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
jcesarmobilecommented, Jan 28, 2020

So, the problem is the web worker requests don’t go through the regular shouldInterceptRequest.

You can use this workaround https://stackoverflow.com/questions/55894716/how-to-package-a-hosted-web-app-with-ionic-capacitor

You don’t need a plugin, you can use a similar code in the MainActivity.java like this:

if(Build.VERSION.SDK_INT >= 24 ){
      ServiceWorkerController swController = ServiceWorkerController.getInstance();

      swController.setServiceWorkerClient(new ServiceWorkerClient() {
        @Override
        public WebResourceResponse shouldInterceptRequest(WebResourceRequest request) {
          return bridge.getLocalServer().shouldInterceptRequest(request);
        }
      });
    }

But notice that it requires SDK 24 and Capacitor supports SDK 21, so it won’t work on 21-23

2reactions
dimitrios1988commented, Oct 14, 2019

Is there any news about the matter? I am having the same issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Service Worker Registration Failed - Stack Overflow
Solved: First thing is service worker only works in secure mode either in https or localhost. It doesnot work in local resources like ......
Read more >
Angular service worker introduction
For service workers to be registered, the application must be accessed over HTTPS, not HTTP. Browsers ignore service workers on pages that are...
Read more >
Angular 5 Service Worker - Medium
Important to note is that fact that the service worker registration is only done by calling ServiceWorkerModule.register('/ngsw-worker.js') only if we're in ...
Read more >
Capacitor: Service Worker - Ionic Forum
Connection refused when call ngsw -worker. js file and Service worker is not registering. The issue is not with SW registration that is...
Read more >
CSP: worker-src - HTTP - MDN Web Docs
The HTTP Content-Security-Policy (CSP) worker-src directive specifies ... serviceWorker.register("https://not-example.com/sw.js"); </script>
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found