I like service workers, but I don't like the lack of control I have.
For example, I want Messenger to send me notifications if I have the tab open, but I don't want the service worker to maintain a persistent connection and send me notifications with the tab closed.
Unfortunately, I am not able to block service workers. My only option is to block notifications entirely, which doesn't even stop the service worker.
The service worker can't maintain a persistent connection. Without an active tab it is shut down after 30 seconds or so. It can receive events via the Push API, which is what you're seeing. But if you refuse notification permissions the site cannot use the Push API. I'm actually not sure how it works when you block notifications after having given permission - it's a requirement that you show a notification in response to a push, so if you're blocked, I'm not sure how the browser registers it.
More broadly, I agree that it is strange you can opt into the Push API without a permission prompt. I wish it weren't the case, but I do think it would be confusing to users to request separate push and notification permissions - most consider them to be synonymous, even if they aren't.
Great write up. I’ve been exploring SWs recently to help with downloading and caching data in the background for faster UI.
Question, I’d like to avoid doing large downloads or possibly cap the transfer rate to avoid any potential jank. Is there a best practice or is it possible to monitor the current browser interaction to determine how strategically best to pull data with a SW?
Depending on what a user chooses to cache, it’s JSON data (1-10MB) going to a mix of localstorage and indexedDB.
"Imagine being able to respond to push notifications, intercept resource requests, and schedule background syncronization, all without the need for a user to actively have your website open in front of them."
".. by authoring special-purpose JavaScript which you register with the browser."
Why does this seem like something useful for malware and generally annoying me.
Push requests only exists on Chrome and is behind a permission, intercepting resource requests within the domain of a website could cause some annoyances, can’t see the malware effect immediately. Scheduling a download for a stable connection, again, could be an annoyance, but what’s the malware implication?
They bite. The generic idea may even be reasonable (offline working web app), but every single time I encounter service workers they make things worse.
If you load a React frontend, it'll, by default, deploy a service worker. Good luck trying to load anything non-react from that resource/server.
I never asked for a web app to work in the background once I closed a site. Nobody should be trained to expect websites work in the background - that is not how they are. If there is no tab open, there should be no notification, no nothing. That is how the web works.
My most delightful experience with a service worker was SVGOMG, https://jakearchibald.github.io/svgomg/. One time I was without an internet connection, and I thought to myself, “if only it had a service worker so it’d work offline!” and then I opened a tab to it, to remind myself to optimise the SVG I was working with later, and— huh. It loaded. On reflection, I decided that given who Jake Archibald is, it made sense that he’d be an early service worker adopter. So that day I was really pleased with the Just Works™ value of that service worker.
And SVGOMG is able to load faster thanks to service workers, too.
At FastMail, we’ll be getting round to adding service workers some time soon, and it’ll actually give us a quite fascinating performance boost: even without supporting full offlineness, simply caching the basic data you need can get you to the point where you need literally no requests before you can load the interface, showing meaningful data to the user, and then fetch updated data in the background. I’m confident we’ll be able to get startup time to under two seconds with service workers for code and data loading/caching, and hopeful that we’ll get it down to under a second.
Service workers can be badly implemented, but they also offer the possibility of some quite delightful improvements in websites and web apps.
> If you load a React frontend, it'll, by default, deploy a service worker.
I think you mean that create-react-app is doing this by default, and that it might be a surprise and cause of problems for the developer. Which is also the reason why they're removing that by default from create-react-app v2.
> That is how the web works.
That's not really an argument, in that it also could've been used as an argument against the introduction of inline images, CSS, or anything added to the web after its first publication. It doesn't really matter how the web works today; what's relevant is how we want the web to work.
good luck trying to convince google of that. they are shaping the web just the way they want it. push notifications? nobody ever except the marketing apartment thought that was a good idea to add to anything. if we try making everything on the web like mobile apps we're gonna get garbage features like this and false expectations on what the web should be like.
> every single time I encounter service workers they make things worse
I'd wager you encounter more service workers than you think, but you don't notice them when they work correctly. They are designed to be invisible in the majority of cases, so of course you only notice them when something goes wrong.
> If you load a React frontend, it'll, by default, deploy a service worker.
What?
> That is how the web works.
The web 'experience' has stopped being about fetching static documents in a request/response fashion long ago. Consumers demand rich apps and interactivity, and it appears they're having it their way.
Maybe I'm out of touch, but for messaging apps wouldn't it be desirable to have an actual service from a native app that you can enable/disable in your OS?
[+] [-] dannyw|7 years ago|reply
For example, I want Messenger to send me notifications if I have the tab open, but I don't want the service worker to maintain a persistent connection and send me notifications with the tab closed.
Unfortunately, I am not able to block service workers. My only option is to block notifications entirely, which doesn't even stop the service worker.
[+] [-] untog|7 years ago|reply
More broadly, I agree that it is strange you can opt into the Push API without a permission prompt. I wish it weren't the case, but I do think it would be confusing to users to request separate push and notification permissions - most consider them to be synonymous, even if they aren't.
[+] [-] ahsanejaz|7 years ago|reply
[+] [-] nerdponx|7 years ago|reply
[+] [-] amelius|7 years ago|reply
[+] [-] sbr464|7 years ago|reply
Question, I’d like to avoid doing large downloads or possibly cap the transfer rate to avoid any potential jank. Is there a best practice or is it possible to monitor the current browser interaction to determine how strategically best to pull data with a SW?
Depending on what a user chooses to cache, it’s JSON data (1-10MB) going to a mix of localstorage and indexedDB.
[+] [-] slow_donkey|7 years ago|reply
That should allow you to sum up download sizes.
[+] [-] pmlnr|7 years ago|reply
[+] [-] michaelmrose|7 years ago|reply
".. by authoring special-purpose JavaScript which you register with the browser."
Why does this seem like something useful for malware and generally annoying me.
[+] [-] Brakenshire|7 years ago|reply
[+] [-] Khaine|7 years ago|reply
I would we rather leave http to be a document model, and create something new and better to support"web-based" apps.
[+] [-] fungi|7 years ago|reply
Kinda sucks that something with such promise is predominantly used as just another weapon for tracking.
[+] [-] pmlnr|7 years ago|reply
They bite. The generic idea may even be reasonable (offline working web app), but every single time I encounter service workers they make things worse.
If you load a React frontend, it'll, by default, deploy a service worker. Good luck trying to load anything non-react from that resource/server.
I never asked for a web app to work in the background once I closed a site. Nobody should be trained to expect websites work in the background - that is not how they are. If there is no tab open, there should be no notification, no nothing. That is how the web works.
[+] [-] chrismorgan|7 years ago|reply
And SVGOMG is able to load faster thanks to service workers, too.
At FastMail, we’ll be getting round to adding service workers some time soon, and it’ll actually give us a quite fascinating performance boost: even without supporting full offlineness, simply caching the basic data you need can get you to the point where you need literally no requests before you can load the interface, showing meaningful data to the user, and then fetch updated data in the background. I’m confident we’ll be able to get startup time to under two seconds with service workers for code and data loading/caching, and hopeful that we’ll get it down to under a second.
Service workers can be badly implemented, but they also offer the possibility of some quite delightful improvements in websites and web apps.
[+] [-] Vinnl|7 years ago|reply
I think you mean that create-react-app is doing this by default, and that it might be a surprise and cause of problems for the developer. Which is also the reason why they're removing that by default from create-react-app v2.
> That is how the web works.
That's not really an argument, in that it also could've been used as an argument against the introduction of inline images, CSS, or anything added to the web after its first publication. It doesn't really matter how the web works today; what's relevant is how we want the web to work.
[+] [-] htor|7 years ago|reply
good luck trying to convince google of that. they are shaping the web just the way they want it. push notifications? nobody ever except the marketing apartment thought that was a good idea to add to anything. if we try making everything on the web like mobile apps we're gonna get garbage features like this and false expectations on what the web should be like.
[+] [-] untog|7 years ago|reply
I'd wager you encounter more service workers than you think, but you don't notice them when they work correctly. They are designed to be invisible in the majority of cases, so of course you only notice them when something goes wrong.
[+] [-] wakeywakeywakey|7 years ago|reply
What?
> That is how the web works.
The web 'experience' has stopped being about fetching static documents in a request/response fashion long ago. Consumers demand rich apps and interactivity, and it appears they're having it their way.
[+] [-] unknown|7 years ago|reply
[deleted]
[+] [-] c0brac0bra|7 years ago|reply
Yes, wouldn't it be wonderful if we could do this in all major browsers today? sigh
[+] [-] eletious|7 years ago|reply
[+] [-] Ajedi32|7 years ago|reply
[+] [-] werdnapk|7 years ago|reply
[+] [-] bluntfang|7 years ago|reply
https://www.merriam-webster.com/dictionary/irregardless
[+] [-] baybal2|7 years ago|reply
[+] [-] mellett68|7 years ago|reply