ritz078
ritz078

Reputation: 2337

How to use inline service worker

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 ?

Upvotes: 13

Views: 4906

Answers (1)

smnbbrv
smnbbrv

Reputation: 24581

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.

Upvotes: 9

Related Questions