How to get browser language in ServiceWorker?
navigator.language || navigator.userLanguage; //not work
This should work now according to the spec.
If your users are using an older version of Chrome, you have to rely on the language read from the global (regular) window.navigator.language
and pass it to the Service Worker via navigator.serviceWorker.controller.postMessage
. Here is the old Chrome tracking bug.
Alternatively, you can try reading Accept-Language
header from your Requests in fetch
event handler - event.request.headers.get('Accept-Language')
- although it didn't work for me for some reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With