You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: questions/what-are-workers-in-javascript-used-for/en-US.mdx
+14-14Lines changed: 14 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -96,7 +96,7 @@ In this example:
96
96
- After processing the message, the worker posts a message back to the main script using `postMessage()`.
97
97
- The main script listens for messages from the worker using `onmessage` on the `Worker` instance.
98
98
99
-
## Service workers
99
+
###Service workers
100
100
101
101
- Act as a network proxy between web app, browser, and network.
102
102
- Can intercept and handle network requests, cache resources.
@@ -148,18 +148,18 @@ self.addEventListener('fetch', function (event) {
148
148
149
149
In this example:
150
150
151
-
- The main script registers a service worker at `/service-worker.js`.
152
-
- The service worker listens for the `fetch()` event, which is fired whenever the browser makes a network request.
153
-
- The service worker first checks if the requested resource is cached using `caches.match(event.request)`.
154
-
- If it is, it returns the cached response. Otherwise, it fetches the resource from the network using `fetch(event.request)`.
151
+
- The main script registers a service worker at `/service-worker.js`
152
+
- The service worker listens for the `fetch()` event, which is fired whenever the browser makes a network request
153
+
- The service worker first checks if the requested resource is cached using `caches.match(event.request)`
154
+
- If it is, it returns the cached response. Otherwise, it fetches the resource from the network using `fetch(event.request)`
155
155
156
-
## Shared workers
156
+
### Bonus: Shared workers
157
157
158
-
- Can be accessed from multiple scripts in different windows/tabs/iframes.
159
-
- Allow data sharing between browser contexts via a messaging interface.
160
-
-Similar to dedicated web workers but with a broader scope.
158
+
-**Scope**: Can be accessed from multiple scripts in different windows/tabs/iframes
159
+
-**Sharing of data**: Allow data sharing between browser contexts via a messaging interface
160
+
-**Browser support**: Limited support, especially [not available on Android browsers](https://issues.chromium.org/issues/40290702)
161
161
162
-
### Use cases for shared workers:
162
+
####Use cases for shared workers:
163
163
164
164
- State sharing across multiple windows.
165
165
@@ -171,10 +171,10 @@ You are not expected to know about worklets, so it won't be covered in great det
171
171
172
172
## Considerations and limitations
173
173
174
-
-**Same-Origin policy**: Workers must comply with the same-origin policy, meaning the script that creates the worker and the worker script itself must be from the same origin.
175
-
-**No DOM access**: Workers do not have direct access to the DOM. They can communicate with the main thread through messages.
176
-
-**Performance**: Creating and managing workers incurs overhead. They should be used judiciously for tasks that truly benefit from parallel execution.
177
-
-**Error handling**: Proper error handling mechanisms should be in place to handle any issues within the worker scripts.
174
+
-**Same-Origin policy**: Workers must comply with the same-origin policy, meaning the script that creates the worker and the worker script itself must be from the same origin
175
+
-**No DOM access**: Workers do not have direct access to the DOM. They can communicate with the main thread through messages
176
+
-**Performance**: Creating and managing workers incurs overhead. They should be used judiciously for tasks that truly benefit from parallel execution
177
+
-**Error handling**: Proper error handling mechanisms should be in place to handle any issues within the worker scripts
0 commit comments