kiwix-js icon indicating copy to clipboard operation
kiwix-js copied to clipboard

Read the ZIM file directly from the ServiceWorker

Open mossroy opened this issue 3 years ago • 2 comments

So far, it was impossible for a ServiceWorker to read the ZIM file directly. So we setup a MessageChannel with the main javascript thread, transfer the "request" to the main thread, which reads the ZIM file, and transfers the content back to the ServiceWorker.

When discussing in https://github.com/kiwix/libkiwix/issues/740, I discovered that recent browsers can now transfer a https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream, and that we can get such a ReadableStream from our File object: https://developer.mozilla.org/en-US/docs/Web/API/Blob/stream

It means that we might be able to transfer the ZIM files (as ReadableStreams) to the ServiceWorker. So that the ServiceWorker would be able to directly make use of our "backend" to read the ZIM.

Unfortunately, this will only work with recent versions of browser engines. We'll have to keep the current MessageChannel logic for older ones (at least IE11, Firefox OS, Windows Mobile, maybe Ubuntu Touch).

But this might improve performance, and might also allow to make use of bigger parts of the C libraries, like considered in https://github.com/kiwix/libkiwix/issues/740

mossroy avatar Apr 08 '22 18:04 mossroy

Regarding the webassembly backend, it might be possible to use it from the ServiceWorker, too: https://medium.com/@kennethrohde/on-the-fly-webp-decoding-using-wasm-and-a-service-worker-33e519d8c21e (needs to be tested)

mossroy avatar Apr 08 '22 19:04 mossroy

In fact, the old browsers (IE etc) I was mentioning above don't support ServiceWorkers anyway (except maybe Ubuntu Touch).

But, if we manage to use this technique, the MessageChannel would need to be kept for some browser versions anyway:

  • Chrome 40 to 75
  • Edge 17 to 78
  • Firefox 44 to 68
  • and same for other browsers

Except if we cleanly fallback to jQuery mode for them (but it would be a more important feature loss)

mossroy avatar Apr 09 '22 13:04 mossroy