Usually in javascript you can find the hostname using window.location.hostname
, but it seems in service workers you cannot find the hostname using this method.
Is there any other elegant way to get the hostname dynamically?
Usually in javascript you can find the hostname using window.location.hostname
, but it seems in service workers you cannot find the hostname using this method.
Is there any other elegant way to get the hostname dynamically?
Share Improve this question asked Jul 25, 2016 at 13:22 ShashankShashank 8741 gold badge9 silver badges13 bronze badges1 Answer
Reset to default 23Code running in the context of a service worker can get the relevant hostname via self.location.hostname
.
If you'd like to explore more of what's available inside of a service worker, the relevant documentation is for the ServiceWorkerGlobalScope
.
Specifically, you'll see that the ServiceWorkerGlobalScope
inherits from WorkerGlobalScope
, and that, in turn, exposes a read-only location
property.