DEV Community

Discussion on: Async vs Defer in JavaScript: Which is Better?πŸ€”

 
dannyengelman profile image
Danny Engelman • Edited

@dannyengelman using web components you need to first introduce an unknown tag into the DOM just to tell the browser what it is afterwards.
If you analyse it, it's no different than creating and injecting an HTML tag into the DOM from a script, or adding some feature to a given selected HTMLElement.

You are wrong here; you can use customElements.define before DOM is parsed.

This can easily be checked with element instanceOf HTMLElement

Its just that 89 out of a 100 developers do not know/do this. They fall into the connectedCallback trap and never investigate WHY their code doesn't work, and whack on async or defer thinking they solved the problem.

So Web Components are totally different from (oldskool) create-DOM-after-DOM-is-parsed scripts.

  1. Define Web Components early (before DOM is parsed) for all GUI stuff.
  2. Define Web Components late for all stuff that can wait (like the oldskool way)

Note: 1. can be two stages, like with icons: define the GUI ASAP, add interactivity later

Thread Thread
 
efpage profile image
Eckehard

This can easily be checked with element instanceOf HTMLElement

This just means that the code is available in the DOM. I suppose it will be executed when HTML is parsed which is not much different from executing a script somewhere inmiddle of your HTML code.

Anyway, this could be an advantage as HTML expression is usually paused as long as scripts are executed, but I suppose the differences will be minor.

Thread Thread
 
dannyengelman profile image
Danny Engelman

Difference is major when GUI Web Components are created with render blocking and inlined <script> in the <head>; then get enhanced to full interactive GUI after DOMContentLoaded
Inline the GUI required CSS as well (I mean bare CSS, not Tailwind) and the first HTML page should be 10 to 15 KB at the most... Eat that Vue,React.

Thread Thread
 
efpage profile image
Eckehard

I suppose, Tailwind (or any CSS in JS solution) will give another twist to this story, but this possibly would fill a complete post. Anyway, an interesting story...

Thread Thread
 
dannyengelman profile image
Danny Engelman

I said not to use Tailwind. That's a 479 Kb Gzipped download.

Do everything (for your primary GUI) native and in one HTML file, then PWA

Thread Thread
 
efpage profile image
Eckehard

Sorry this was a misunderstanding. Even if you do not use a bulky package like tailwind, any JS that manipulates global CSS might cause strange effects like Layout shifts and unexpected delays. But this would fill another post I suppose.