Search code examples
htmlweb-workershadow-dom

Is it possible to use shadow DOM in a webworker?


I know Webworkers can't manipulate DOM (and that are not supposed to) but I would like to know if there is any way to manipulate a shadow DOM within a webworker (attaching it later in the UI thread). Even if I'm almost sure is not possible, I couldn't find any information/documentation on it.


Solution

  • You cannot post or transfer a Shadow DOM to a Web Worker for the same reasons you cannot do that with a DOM element.

    Instead, you should manipulate HTML as string in your worker, then insert it in a Shadow DOM shadowRoot via its innerHTML property.

    Depending of the architecture of your projet, you could also consider using WebSocket that can directly interact with the DOM.