Reputation: 529
I have implemented web push notifications and now I'm looking into the possibility to use the service worker to update the DOM if the website is opened in the browser. Similar to how social media sites update if you receive a message (or SO for that matter).
Is this something that is possible, or am I barking up the wrong tree?
I have tried simply telling the service worker to look for a certain element and update its contents, but that didn't work.
Upvotes: 3
Views: 6343
Reputation: 2010
Service worker (web worker in general) doesn't have access to window or document element. What that means, web workers can`t directly manipulate DOM.
However, web workers can send messages to main thread (document javascript), by using Client.postMessage(). So what you need to do, is send message from service worker to document javascript with list of id/class/tagname/whatever of DOM elements that you want to manipulate, and let main thread do the job.
Upvotes: 9