Search code examples
javascriptfirefoxweb-componentcustom-element

WebComponents: Firefox custom-element is not showing


I am using web components "custom elements" feature and need to support old browsers(Firefox v60), so instead of loading the polyfills via webcomponent-loader.js which loads all the polyfills . lazy loaded custom-elementpolyfill based on feature detection

(function() {
if(!window.customElements){
var ce = document.createElement('script');
ce.type = 'text/javascript';
ce.async = true;
ce.src = 'https://unpkg.com/@webcomponents/custom-elements@1.2.4/custom-elements.min.js';
/**

     * loading "customElement" polyfills wont't fire "WebComponentsReady" event, it will be called when we use
     * "webcomponent-loader.js" but it will load other polyfills("shadow-dom"), so loading the "customElements" polyfill alone
     * based on feature detection and firing "WebComponentsReady" event manually.
     */
  ce.onload = function() {
      document.dispatchEvent(
          new CustomEvent('WebComponentsReady', {bubbles: true}));
  };
  var st = document.getElementsByTagName('script')[0];
  st.parentNode.insertBefore(ce, st);
}
})()

and firedWebComponentsReady event manually when its loaded. Registered the element like below

let registerElement = () => {
 if(!window.customElements.get(“wc-button")){
   window.customElements.define(‘wc-button', WCButton);
 }
};

if(window.customElements){
  registerElement();
 } else {
  document.addEventListener('WebComponentsReady', registerElement);
}

WebComponentsReadygot fired and in the listener callback has been called to define/register the element, but the element isn't getting shown or loaded in the page in firefox60.6.1esr (64-bit)


Solution

  • webcomponents-loader.js does feature detection for you instead of waiting for WebComponentsReady event, you do

    <script src="https://unpkg.com/@webcomponents/webcomponentsjs/webcomponents-loader.js"></script>
    <script>
    window.WebComponents.waitFor(() => {
       // do stuff that needs the polyfill
    })
    </script>
    

    For more information: