How to use the built-in service worker

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

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

+6
source share
1 answer

According to MDN, you must specify the URL path . The only way to imagine how to do this is to use the data URI , but when you try to do the following in Chrome:

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

is responsible

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

So, I would rather not do this.

Regarding Blob check, which says the same thing you said

Blob is available in the desktop area.

He does not name the type of workers. If you check out the official w3c project , they don't have a word about blob.

The only way to find out is to try it on Chrome yourself. But even if this works for you in Chrome, it does not mean that it will work later. This is still experimental technology.

+4
source

All Articles