Comlink uses postMessage under the hood to communicate between the Web Worker & your page.

I believe that Comlink will motivate developers to rely more and more on Web Workers.

Fetch + Comlink

Comlink-fetch allows you to use Fetch in a web worker that is exposed through Comlink.

I recently published version 0.1.2 on npm. Here’s how it works:

Start by installing comlink-fetch from npm:

npm install comlink-fetch --save

install comlink-fetch

Then create a minimal index.html file that loads index.js:

I’m using JavaScript modules ( type="module" ) because I want to be able to import Comlink from within that index.js

Now create an index.js with the below code:

Here’s a breakdown of what’s going on in this index.js:

You import Comlink from comlink.es6.js

from You create a new Web Worker from comlink-fetch’s fetch.worker.js

You create a proxy for that worker using Comlink

And then you’re all set to use it! The rest of the code that I presented is a use case where you want to load some additional information in the background, for example posts that you’ll use later on.

Loading them in a web worker will make sure that they won’t block the main thread and won’t slow down your UI.

Network tab recording with Fast 3G network throttling

It’s worth noting that GET /posts/1 and GET /posts/2 are being executed at the same time, in the background.

How Comlink-fetch is built

Once you take a look at the source code of comlink-fetch’s fetch.worker.js, you’ll understand why Comlink is awesome.

I simply defined a class Fetch which is a wrapper on top of fetch that makes it possible to have nice features, such as setting a base url, default headers and default body.

You only have to import Comlink at the beginning and then expose the class you wrote so that it can be proxied from the the main thread later on.

Take a look at the signature of the class Fetch that I defined:

As you can see there’s no boilerplate or unnecessary code. Just a simple import at the beginning, and a Comlink.expose at the end.

Performance benefits

Will you directly see performance benefits if you use comlink-fetch? Most probably not. As outlined in this Stackoverflow thread by Konrad Dzwinel, a generic worker based fetch will not necessarily improve your performance.

However, if you have to do some heavy processing on fetched data, then it would make sense as you’re parsing results and processing them off the main thread.