Skip to content

Commit 4506082

Browse files
finfinyangshun
andauthored
contents: update browser support on Shared Workers (#8)
Co-authored-by: Yangshun Tay <[email protected]>
1 parent 793c99a commit 4506082

File tree

1 file changed

+14
-14
lines changed
  • questions/what-are-workers-in-javascript-used-for

1 file changed

+14
-14
lines changed

questions/what-are-workers-in-javascript-used-for/en-US.mdx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ In this example:
9696
- After processing the message, the worker posts a message back to the main script using `postMessage()`.
9797
- The main script listens for messages from the worker using `onmessage` on the `Worker` instance.
9898

99-
## Service workers
99+
### Service workers
100100

101101
- Act as a network proxy between web app, browser, and network.
102102
- Can intercept and handle network requests, cache resources.
@@ -148,18 +148,18 @@ self.addEventListener('fetch', function (event) {
148148

149149
In this example:
150150

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)`
155155

156-
## Shared workers
156+
### Bonus: Shared workers
157157

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)
161161

162-
### Use cases for shared workers:
162+
#### Use cases for shared workers:
163163

164164
- State sharing across multiple windows.
165165

@@ -171,10 +171,10 @@ You are not expected to know about worklets, so it won't be covered in great det
171171

172172
## Considerations and limitations
173173

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
178178

179179
## Further reading
180180

0 commit comments

Comments
 (0)