Reputation: 4394
Is it possible to pass data, or use a global defined variable in the main thread, to a service worker?
For example, I need to initialize a firebase messaging lib and I want to pass a messagingSenderId parameter (without making additional requests)
I couldn't find a way to do this as I see that the register method does not have useful options:
https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register
and the no object defined in the "window" is not accessible in service worker "self" (the accepted answer here does not work: Global variable in Web worker - maybe I am not accessing the variables correctly)
Upvotes: 1
Views: 2073
Reputation: 31
The service worker can see its own URL (without fragment) via the location
object so you can pass some query string values and have it use them in its initialization. And you can use new URLSearchParams(location.search)
to parse query string values in a standard way instead of custom string parsing of location.href
.
Upvotes: 2
Reputation: 51
You can try something like Service Worker postMessage! I hope this helps
Upvotes: 1