Eilon / MauiHybridWebView

MIT License
216 stars 49 forks source link

Service workers cannot be registered #28

Open Ceshion opened 1 year ago

Ceshion commented 1 year ago

When trying to register a service worker, the WebView's dev tools show an "unknown error" message during registration. I have been digging into the error some, it appears that service worker registration either never makes an actual request, or that request goes through a different channel than normal requests--in any case the request does not go through the WebResourceRequested handler. This is, however, possible with a plain WebView directed to the same page.

Our use case is migrating an application that previously used Capacitor into MAUI, where we intend to use this package to provide interop for migrated and unmigrated components. I'll keep looking, but hoping somebody with more familiarity with the platform internals could figure it out in less time :)

The exact error is: Uncaught (in promise) TypeError: Failed to register a ServiceWorker for scope ('https://0.0.0.0/') with script ('https://0.0.0.0/service-worker.js'): An unknown error occurred when fetching the script.

And attempting to run the call through the DevTools console results in the following error in the response in the Network tab: image

This can be reproduced by installing this package onto a new Blazor MAUI project, with the following changes:

Resources/Raw/hybrid_app/index.html

<html>
  <head>
    <script>
      if ('serviceWorker' in navigator) {
        navigator.serviceWorker.register('service-worker.js');
      }
    </script>
  </head>
  <body>
  </body>
</html>

Resources/Raw/hybrid_app/service-worker.js

self.addEventListener('install', event => {});

self.addEventListener('activate', event => {});

self.addEventListener('fetch', event => {});

MainPage.xaml

<ContentPage ...>
  <xlj:HybridWebView HybridAssetRoot="hybrid_app" MainFile="index.html" EnableWebDevTools="True" />
</ContentPage>
sibiren-spins commented 1 year ago

Could this be something to do with the same problem with external resources mentioned in other issues? Even though that service worker file is not externally hosted, it may be that the app cannot reach it, for the same reason that it won't load externally hosted pages. Does this still break if you try the same setup with this fork?

Eilon commented 1 year ago

I'll have to try this out. I've never used service workers but hopefully it's something easy to get working.