Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the restrictions on what can and cannot be done in a a service worker?

Further to Can I http poll or use socket.io from a Service Worker on Safari iOS? what is the list of what can and cannot be done in a service worker? The answer referenced above says "You cannot ... have an open connection of any sort to your server" which makes sense, but where is that fact documented and how is the restriction enforced?

For example, are certain browser APIs unavailable to Service Workers? or is there an execution quota which prevents a long running process?

Eg. if my service worker has ... setInterval (()=>{console.log('foo'), 1000}) ... will it throw an exception?, will it run and then fail? is the behaviour browser dependent?

like image 711
pinoyyid Avatar asked May 04 '20 22:05

pinoyyid


People also ask

What service worker can do?

Service workers are specialized JavaScript assets that act as proxies between web browsers and web servers. They aim to improve reliability by providing offline access, as well as boost page performance.

Can service worker access cookies?

In addition, the reliance on document means that cookies cannot be accessed by service workers which cannot access the document object. The Cookie Store API provides an updated method of managing cookies.

Do service workers run when browser is closed?

Service workers have a total control over every request made from a website. Since they run in the background, they don't need the webpage to be open. That gives them ability to listen and respond to events such as push messages sent from the server to the browser.

What are the use cases of service workers?

Service workers have mostly been associated with offline support for web applications and for push notifications for re-engaging with users. There are pretty good drop-in wrapper libraries like sw-precache that you use to get such functionality without directly writing any Service Worker code yourself!


1 Answers

Service Workers are only supposed to process attached events. And those are to be registered by some script from the outside. Even delaying the execution is not supported in some cases on Safari - Event.waitUntil(promise).

Once your event queue is empty, your user agent is supposed to decide wether it kills of the service. There is no guarantee that anything from then on is going to get executed.

like image 154
muzzletov Avatar answered Sep 23 '22 02:09

muzzletov