Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

PreloadableWorker

There's no way to <link rel=preload> a Web Worker. This fixes that.

npm i -S gist:developit/567dde2346d785b2628224fddbf6783c

<!-- workers are now just a normal script preload: -->
<link rel=preload href=/path/to/worker.js as=script crossorigin>
import PreloadableWorker from 'preloadable-worker';

// use PreloadableWorker instead of Worker
const worker = new PreloadableWorker('/path/to/worker.js', { name: 'my-worker' });
worker.postMessage('hello');
{
"version": "0.1.0",
"name": "preloadable-worker",
"main": "preloadable-worker.cjs.js",
"module": "preloadable-worker.js"
}
var a;
module.exports = function PreloadableWorker(src, opts) {
(a || (a = document.createElement('a'))).href = src;
return new Worker('data:,importScripts("'+a.href+'")', opts);
}
var a;
export default function PreloadableWorker(src, opts) {
(a || (a = document.createElement('a'))).href = src;
return new Worker('data:,importScripts("'+a.href+'")', opts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment