Web component - learnings and limitations
Tags: webcomponents, javascript, webdev, tutorial
Published: 2022-05-08 18:31:04

Though the concept of [Web Component](https://kinsta.com/blog/web-components/) was first introduced more than a decade ago by Alex Russell; browsers took time to provide support for web components and initial implementation didn’t come until 2016 from Chrome, 2018 from Firefox, and 2020 from Edge.

I got introduce to the web component some time back while working on a new project. Since then I’m learning, trying to explore out best practices where native web component way is available, and hacking when can’t find native implementation.

Here are some pain areas when you work with the web component.

## 1. Inadequate Browser Support

Even though the web component is now supported on all major browsers on their latest version, that doesn’t really help when you have got a global product or global customer base. And definitely, there will be customers in various age ranges and going to use a wide variety of browsers and versions.

**What does it means when I say no support?** It means that the UI element doesn’t render on screen. You won’t see anything on its placement, e.g. if you have a footer coming from a web component, you won’t see the footer at all.

**Any workaround?** [Polyfill](https://www.webcomponents.org/polyfills) is promising and could extend support on the older version of browsers.

#### Major browser support version without Polyfill

| Browser | Version Support |
|---|---|
| Chrome | 68 and above |
| Firefox | 62 and above |
| Safari | 12 and above |

## 2. Shadow DOM

Sometimes strength goes against you, something on similar lines happens with the web component. Shadow DOM in closed mode provides the powerful property of encapsulation (limits the access to Shadow DOM and its child elements), however, if you’re developing a component to replace an existing legacy component then you’re most likely going to break the automation test. A shadow DOM in closed mode is inaccessible (unless you expose an interface to access it) from light DOM, but again you can use a testing library that provides support for Shadow DOM, like Cypress to write an automation test as it provides an interface to access shadow DOM.

## 3. Session Recording 

There are different software products available that records the user interaction with web pages so that further technical assistance could be provided. However, with the web component, there is a high chance that session recording software may not be able to capture the web component's HTML elements or in other words, the UI part of web component. This could happen as web component will load asynchronously and session recording may not wait for web component to completely load.

## 4. Challenge in loading external font

I have already written a comprehensive post about it, so I’m not going to copy and paste it here (I’m a bit lazy😀), here is the link [Load external font with web component](https://dev.to/akdevcraft/use-font-in-web-component-51a4)

## Conclusion 
**So, should we not use a web component?** There is no straightforward answer to it, nothing is perfect and everything in software engineering is a trade-off. But we always have to weigh the pros and cons, and ask the question, does web component solve that problem considering its limitations?

__If you have reached here, then I did a satisfactory effort to keep you reading. Please be kind to leave any comments or ask for any corrections. **Happy Learning!**__

#### Other Blogs:

- [How to setup pre-hook for an NPM project](https://dev.to/akdevcraft/git-pre-hook-pre-commit-hook-with-npm-project-44p2)
- [GitHub Repository Readme Template](https://dev.to/akdevcraft/github-repository-readme-template-6h2)