Can't load service worker from disk

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Can't load service worker from disk

Postby Astaroth » Sat May 28, 2022 9:30 am

Hi, I'm trying to load a service worker that's actually a file on disk (not on a website) and it's not working.

My web page code looks like:

navigator.serviceWorker.register('/my-service-worker.js'

I've overridden CefResourceRequestHandler::GetResourceHandler() and it's not being called for "my-service-worker.js" at all.
That works for everything else, why not this one file?

My overridden CefResourceRequestHandler::OnResourceRedirect() also isn't involved with this file.

I also tried the advice here https://www.magpcss.org/ceforum/viewtop ... 20&p=49477
and my CefRequestContextHandler::GetResourceRequestHandler() isn't being called at all!

I think it's going to the real website instead of using my local file because the DevTools console shows this error:
"Service worker registration failed: DOMException: Failed to register a ServiceWorker for scope ('http://www.foo.com/') with script ('http://www.foo.com/my-service-worker.js'): The script resource is behind a redirect, which is disallowed."

How can I get it to load the file from disk?
Astaroth
Techie
 
Posts: 35
Joined: Tue Apr 23, 2019 6:23 pm

Re: Can't load service worker from disk

Postby magreenblatt » Sat May 28, 2022 9:53 am

Service workers are associated with a request context, not a browser. You need create a CefRequestContext, implement CefRequestContextHandler::GetResourceRequestHandler, and pass that request context when creating the browser.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Can't load service worker from disk

Postby Astaroth » Sat May 28, 2022 10:01 am

Yeah, I tried that already. My CMyCefRequestContextHandler is being constructed, but its overridden GetResourceRequestHandler() never gets called (at least in the main process):

CefRequestContextSettings settings;
CefRefPtr<CefRequestContextHandler> pRequestContextHandler = new CMyCefRequestContextHandler();
CefRefPtr<CefRequestContext> pRequestContext = CefRequestContext::CreateContext(settings, pRequestContextHandler);

// Create the BrowserView.
CefRefPtr<CefBrowserView> pBrowserView = CefBrowserView::CreateBrowserView
(
pClient,
CefString(url.data(), url.size(), true),
browserSettings,
m_MyConstants.ToDictionary(),
pRequestContext,
new CMyCefBrowserViewDelegate()
);
Astaroth
Techie
 
Posts: 35
Joined: Tue Apr 23, 2019 6:23 pm

Re: Can't load service worker from disk

Postby magreenblatt » Sat May 28, 2022 10:43 am

Service workers also require HTTPS. Your error message suggests that you’re using HTTP currently.
magreenblatt
Site Admin
 
Posts: 12382
Joined: Fri May 29, 2009 6:57 pm

Re: Can't load service worker from disk

Postby Astaroth » Sun May 29, 2022 10:09 am

Yes, I've tried both http and https and actually I found it necessary to use --unsafely-treat-insecure-origin-as-secure in order to get navigator.serviceWorker to be defined.
Anyway, I still have no idea why I can't intercept the file or why CefRequestContextHandler::GetResourceRequestHandler() isn't being called on the main process.
Astaroth
Techie
 
Posts: 35
Joined: Tue Apr 23, 2019 6:23 pm

Re: Can't load service worker from disk

Postby Astaroth » Wed Jun 01, 2022 10:59 am

I think I misunderstood this post: viewtopic.php?f=6&t=18520&p=49496&hilit=service+worker#p49496

This person was able to intercept file requests from within the service worker, but not the loading of the serviceworker.js itself

Maybe this is just a limitation in CEF. I'm using CEF 98.1.17 which is a few months old.
Astaroth
Techie
 
Posts: 35
Joined: Tue Apr 23, 2019 6:23 pm


Return to Support Forum

Who is online

Users browsing this forum: Google [Bot] and 31 guests