Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use inline service worker

Tags:

javascript

I know that you need to create a separate file for service worker and register it. How can i write the code of service worker in the same file and use it ?

I know that I can use Blob in web workers . Is it same for service Workers ?

like image 503
ritz078 Avatar asked Jul 20 '15 11:07

ritz078


Video Answer


1 Answers

According to MDN you must specify the URL path. The only way I can imagine how to do that inline is using data URIs, but when you try the following in Chrome:

navigator.serviceWorker.register("data:application/javascript,alert('123');");

it responds with

Uncaught (in promise) DOMException: Failed to register a ServiceWorker: The origin of the provided scriptURL ('null') does not match the current origin ('my origin URL').

So I would rather say no way to do that.

Concerning Blob check the following which says the same you've told

Blob is available in Worker scopes.

It does not name the workers type. If you check the official w3c draft they don't have a single word about blob in there.

The only way you can know it - try in Chrome yourself. But still even if it will work out for you in Chrome now it does not mean it will work later. It is still an experimental technology.

like image 89
smnbbrv Avatar answered Sep 22 '22 13:09

smnbbrv