Web Performance Calendar

The speed geek's favorite time of year
2023 Edition
ABOUT THE AUTHOR
Photo of Theodor Bajusz

Theo is your friendly neighborhood web developer from Hamburg, Germany. He has roots in Serbia and Hungary. Fluent in multiple languages and passionate about computers, Theo pursues programming to contribute to the web's evolution, optimizing it one site and webperf metric at a time. Theo thrives on unraveling complex computer challenges. Beyond his digital expertise, Theo enjoys hands-on work like repairing cars, bikes and electronics.

tl;dr:

The Declarative Shadow DOM is a novelty in standardization which enables the server-side rendering of web components. This approach, in contrast to the traditional JS-based Shadow DOM, reduces client-side processing, improves load times, and offers SEO benefits by being part of the initial HTML.

Introduction

In the evolving landscape of web development, our quest for optimizing performance is to advance techniques and technologies: one such advancement is the Declarative Shadow DOM, a feature intersecting web components and server-side rendering which offers a significant boost in performance and encapsulation.

What is a Shadow DOM?

First of all, what is a Shadow DOM? Shadow DOM is a web standard that enables encapsulation in web components. It allows developers to bundle CSS, JavaScript, and HTML in isolated environments. This encapsulation ensures that a component’s styles and scripts do not leak into the global scope, preventing conflicts and preserving code integrity.

Traditional Shadow DOM vs. Declarative Shadow DOM

Traditionally, Shadow DOMs are created client-side using JavaScript. However, this approach has a downside: it relies heavily on client-side rendering as used in many SPAs. This can lead to performance bottlenecks, especially on devices with lower processing power.

Enter the Declarative Shadow DOM, a breakthrough that allows Shadow DOMs to be defined directly in HTML. This approach significantly enhances performance by enabling server-side rendering of the Shadow DOM structure.

Declarative Shadow DOM and Server-Side Rendering

The Declarative Shadow DOM can be rendered on the server using the <template shadowrootmode="open"> element. This technique allows the Shadow DOM to be shipped as part of the HTML from the server, fully formed and ready to be displayed. This method reduces the workload on the client-side, leading to faster rendering times and an improved user experience.

Benefits for Web Performance

  1. Reduced Client-Side Processing: By handling the complexity of Shadow DOM creation on the server-side, there’s less JavaScript to process on the client-side, leading to faster Time To Interactive (TTI), even more so on computationally weaker devices.
  2. Improved Load Times: As the Shadow DOM structure is already in place when the page loads, the browser can render content faster.
  3. SEO Advantages: Since content within a Declarative Shadow DOM is part of the initial HTML payload, it’s more readily accessible to search engines. Furthermore, it also improves Core Web Vitals, such as the FCP and LCP, as your browser can start rendering content sooner.

Conclusion

The Declarative Shadow DOM represents a significant step forward in web component technology. By enabling server-side rendering of isolated DOM trees, it paves the way for faster, more efficient, and robust web applications. Implementing Declarative Shadow DOM in your projects can show considerable improvements in loading times and overall application responsiveness in comparison to the traditional Shadow DOM approach, by leveraging the effect on server-side computation instead of having the client-side do the heavy lifting.

If you are interested in a deeper dive on this topic, I can warmly recommend the following article from Chrome for Developers.

Furthermore, as this topic is currently in the standardization process, the WHATWG pull request can be found here.

See here if you can use this feature in your favorite browser: https://caniuse.com/declarative-shadow-dom.