DEV Community

AK DevCraft
AK DevCraft Subscriber

Posted on • Edited on

Web component - learnings and limitations

Though the concept of Web Component 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 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

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:

Top comments (5)

Collapse
 
dannyengelman profile image
Danny Engelman

| A shadow DOM is inaccessible from outer DOM

So you haven't learned yet about:

  • Inheritable styles
  • CSS properties
  • ::part
Collapse
 
akdevcraft profile image
AK DevCraft • Edited

I guess I should have mentioned it, does Shadow DOM in closed mode still allows accessing its elements from outer DOM?

const header = document.createElement('header');
const shadowRoot = header.attachShadow({mode: 'closed'});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
dannyengelman profile image
Danny Engelman

it is "closed", not "close"

And all it does is prevent access to .shadowRoot, and that is all it does.

Nothing stops you (the WC Author) from exposing your component to the outside world.

Thread Thread
 
akdevcraft profile image
AK DevCraft

This is really awesome, I didn't find an example this like. My requirement was to expose certain HTML element inside the web component, that now I can easily expose as a simple function. Going back to your example, I can add a function like below

getButtonEle(){
              return this.root.children[1];
          }
Enter fullscreen mode Exit fullscreen mode

Thank you!

Collapse
 
akdevcraft profile image
AK DevCraft

Nope, but going check it out. Thank you!