// Build a worker from an anonymous function body
var blobURL = URL.createObjectURL( new Blob([ '(',
//Long-running work here
worker = new Worker( blobURL );
// Won't be needing this anymore
URL.revokeObjectURL( blobURL );
// Post code from window to ServiceWorker...
[ '/my_workers/worker1.js', '(' + workerFunction1.toString() + ')()' ]
// Insert via ServiceWorker.onmessage. Or directly once window.caches is exposed
caches.open( 'myCache' ).then( function( cache )
Can you elaborate on this solution, how does it work? What is the worker1.js? Is it a seperate js file? I am trying to use this but unable to make it work. Specifically I am trying to make it work for a SharedWorker
If only you could wrap it up in a useful function!
Now there's an even more powerful way of solving this problem.
Again, store the worker code as a function, (rather than a static string) and convert using .toString(), then insert the code into CacheStorage under a static URL of your choice.
Personally I really like the toString methods, but @dan-man THAT regex!
Support is the intersection of these three tables:
The html5rocks solution of embedding the web worker code in HTML is fairly horrible.