ServiceWorker in Webview apps

TLDR: App uses webview to load page from file. Serviceworkers only work via http. Searching for a solution to get serviceworkers running. Preferably not by compiling my own webserver into the app.

I am currently trying to get a serviceworker running in an app that utilizes WebView. (The hydrogen matrix chat client)

The problem is, that serviceworkers are not allowed when the page is served via the file: protocol.

An app for ubuntu touch solved this by spinning up a small http server which serves the file locally.

For an ecosystem however it does not sound nice, when every other app brings their own webserver just to solve this problem. Imagine there is a critical security issue with this server and every application that ships it to solve a common problem needs to be patched… That would result in more app nightmares that we already have with outdated apps.
The next question would be, if that is actually allowed in the store.

Does anyone have ideas to do that better? Either circumvent the local webserver entirely, or a solution provided by the platform.

Followup question: if I am shipping my own webserver, what do I need to look for? Its basically only serving once files at app startup, so I’d want something which is not eating resources afterwards.

A very simple http server is basically built into python, so you probably don’t need to build one yourself.

Also, doesn’t this apply to ServiceWorkers as well?

Yeah, we’ve discussed the communication between QML and the JS interpreter as well.

I think a very minimal web server is not actually an issue if only one request needs to be served. It could be launched and shut down immediately, or? And as @nephros suggests, you could use the python3

import http.server
import socketserver

classes. I think it’s 4 lines of code to serve a page… By default SimpleHTTPRequestHandler just serves index.html from the same directory. http.server — HTTP servers — Python 3.10.4 documentation

In that case, the system updates to python3 should have you covered where security is a concern, I would have a look to see if you can simply discard post/get input and if so, do that to stay safe?

EDIT: I do much more evil stuff with python in, for instance, Videoworks. I pass a filehandle to ffmpeg via python system calls and collect data dynamically (frames, meta data) … passing it back to QML.

1 Like

So, I’m building a demo on the side. It’s not quite as simple as starting the server :slight_smile: A background thread is required. I’ll post a repo link when it’s working properly.

EDIT! After measuring out the roof of the shed, moving some rocks about, staring at sagging foundations, it started to hail. soooo…

https://github.com/poetaster/harbour-wvex has been modified to launch a webserver and init the WebView from that.

There’s one catch, I haven’t figured how/if I can set the SimpleHTTPRequestHandler path properly. So I just shoved the index.html and framescript.js files into ~/

It works. The Server sits in a thread which seems to be cleaned up properly when the app exits.

1 Like

I think it always serves from $PWD. You can set a path relative to that though:

harbour-privoxy-httpd.py

Ah, I was being lazy and avoiding implementing the class. I couldn’t find a fast and dirty method to initialize the post 3.7 python Handler. So, the simple version would be something like:

class Handler(http.server.SimpleHTTPRequestHandler):
    directory = "~/.config/orgname/appname/"

? And then copy the index (etc) there on install? I modified the example to keep the javascript in the application qml, since that works without issue and seemed safer.

1 Like

It also works with a os.chdir in the pythonfile

Cool, I’ll rework the example to do an os.chdir to the application’s lib. Thanks!

But does it help with your ServiceWorker issue :wink: ??

I updated the wvex example to use chdir.

1 Like

It does, serviceworker seems to work now, but I messed it up somehow and can’t clean the caches apperantly…
Removing the applications .cache directory does not seem to help.

(currently trying to proof, that its not an issue of the app)

1 Like

Ah, do you mean: ‘.cache/orgname/appname/.mozilla/cache2’ Were you trying to clear the cache programmatically from within the webview?

No tried to clean it via FS but I think at this point, this wasn’t actually my issue…