This site is under construction

You probably already noticed that this page looks very bare. I'm working on a new design and will gradually make adjustments to make this site a little more appealing and accessible. If you notice something that is totally broken, feel free to contact me.

Skip to content

Declarative Shadow DOM

14 Oct 2020

There is a new way using the Shadow DOM API for Self-Containing Web Components: the declarative Shadow DOM for Server Side Rendering

V1 - The imperative way

Shadow DOM is one of the three Web Components standards, rounded out by HTML Templates and Custom Elements. Shadow DOM provides a way to scope CSS styles to a specific DOM subtree and isolate that subtree from the rest of the document. The <slot> element gives us a way to control where the children of a Custom Element should be inserted within its Shadow Tree. These features combined enable a system for building self-contained, reusable components that integrate seamlessly into existing applications just like a built-in HTML element. - (1)

The Shadow DOM v1 can only be enabled as an imperative API with JavaScript and is perfect for Client Side Rendering. But what if your Web Component doesn't need JavaScript to work as expected? What if some Crawlers ignore JavaScript and therefore can't visit and see your website? One main idea of the declarative Shadow DOM is to use the API with the advantages of Server Side Rendering without the need of JavaScript. For example rendering content as fast as possible with less priority on interactivity. Or even take of care of users who run the Browser or System with JavaScript disabled. With v1 of the Shadow DOM this is not possible.

How to use the declarative way

The proposed Solution is to use the Shadow DOM with the HTML Template and an additional attribute shadowroot.

  <template shadowroot="open">
  <h2>Light content</h2>

The HTML Parser will detect the Template tag with the shadowroot attribute and append its content as the shadow root of its parent element, in this example the Custom Element host-element. The HTML Structure above will result in the following DOM tree:

  #shadow-root (open)
    <h2>Light content</h2>

At the time of writing this post, the declarative Shadow DOM is only available as an experimental feature from Chrome 85 (chrome://flags/#enable-experimental-web-platform-features) and "The Chrome team is tentatively looking at un-flagging Declarative Shadow DOM in Chrome 88".

Read more about the new declarative way of using the Shadow DOM here:


More articles and notes