Search code examples
javascriptweb-workerworker

How can I access the client URL in a Web Worker?


How can I access the client URL in a Web Worker? I can access the client and URL in a Service Worker with the Clients API, but I have not found a way to do that with Web Workers.

I need to know what page the user is viewing to conditionally change the data posted from a Web Worker. For separation of concerns, I can't handle anything on the client itself.


Solution

  • You can't. The Worker knows its own location, accessible through self.location, but it doesn't have access to the one of its creator.

    The only way is to have your main thread to pass that info to your Worker, e.g through postMessage.