Readit News logoReadit News
Posted by u/cushpush 2 years ago
Tell HN: Service Workers === Browser Background Tasks
The term "service worker" has confused me for a while, now I see it to be bad jargon selection. You can use "service workers" to send push notifications, load things from cache, and allow for offline behavior of your application (turn off your wifi and load twitter, for example). I think the term "browser background task" is more apt, I am sad another much more confusing and less relevant term was chosen years ago.
KRAKRISMOTT · 2 years ago
Service workers can function as proxies and man in the middle all requests, they are very different from traditional background tasks in the general sense of the word.
theGeatZhopa · 2 years ago
The same applies for the term "demon" in Linux, "services" in Windows. I'm pretty ok with the term "service worker" as I learned, it's a background service doing background things :)
notRobot · 2 years ago
Technically it's 'deamon', not 'demon' :)

Interesting read: https://man7.org/linux/man-pages/man7/daemon.7.html

KomoD · 2 years ago
You mean daemon, not deamon or demon
kalupa · 2 years ago
daemon? :)
bheadmaster · 2 years ago
Not an experienced JS dev here - if that is the case, then what is the difference between a "service worker" and a simple async function running in the background?
kevincox · 2 years ago
There are a lot of differences:

1. Service worker lifetime is not tied to a particular window. They can live across multiple windows either at the same time or across time. (Think of a tab closed then later reopened.) A background function will stop running when the tab is closed.

2. Service workers have some ability to intercept network requests. In a page you could patch APIs like fetch and XMLHttpRequest but you have no ability to intercept image loads or top-level navigations.

3. Other APIs such as receiving push notifications are also tied to service workers (likely because of the lifetime restrictions, otherwise the browser would need to decide which tab to deliver a notification to or open a new one if none are open).

fortunateregard · 2 years ago
If you want to intercept and modify a incoming json response for some specific url pattern, would a service worker be a good way to do so?

To illustrate, assume I frequently browse a blog and want to trick my browser into thinking that I have "favorited" every post. It's trivial to write a for loop that iterates over response.json and sets `is_favorite = true`. But it's not as clear to me where this script should ideally live in order to have the logic always executed before the response is made available to the site.

Your comment made me think about whether I can replace my overkill solution (https://requestly.io/) with something lightweight.

__ryan__ · 2 years ago
Simple async JavaScript is still single threaded with an event loop. In other words, your async code is just a task deferred for later and only one task runs at a time, only moving onto another task when complete or explicitly yielding via “await”.

Service workers are threads. They’re basically separate JavaScript processes you communicate with with IPC, with other special privileges and capabilities allotted to them.

houseatrielah · 2 years ago
Service worker is a whole pattern to give PWA's offline access, ie. intercepting network requests and caching. Async runs on the main thread. If you want to offload work to another thread you use a webworker.
dumpsterdiver · 2 years ago
To my understanding service workers are run in a separate thread using the Web Worker API, and thus are non-blocking unlike standard async functions.
smartplaya2001 · 2 years ago
promiscuous terminology :)
joshxyz · 2 years ago
we need to decriminalize, legalize, and regulate this industry.