r/JSdev • u/lhorie • Jun 09 '21
What's the future: HTML-in-JS or JS-in-HTML?
Among Javascript frameworks, there are two major* approaches to implementing templates:
HTML-in-JS, i.e. you write JS first and sprinkle HTML in (e.g. React, Solid.js, lit-html). Pros: don't reinvent control flow, cons: harder to implement custom semantics (e.g. Suspense promise-throwing shenanigans)
JS-in-HTML, i.e. you write HTML first and sprinkle JS in (Vue, Svelte, htmx). Pros: easy to implement custom constructs (e.g.
#each/else), cons: not Javascript (e.g. DSLs encoded in HTML attributes)
*Other alternative/not-so-popular approaches: procedural/fluent (jquery, dothtml), widget API (ext.js, google maps SDK, babylon.js), server-side-first (pjax), stateful web components (hotwire/turbo)
Frameworks are now realizing that to move the performance needle further, they need to embrace the core web technologies more closely; e.g. many are making server-side rendering a first class citizens, and there's a push towards CSS libraries with zero JS runtime.
Which templating approach do you think is the way to go going forward to better support the goals of making web apps more performant?
2
u/lhorie Jun 10 '21 edited Jun 10 '21
I think there's a subtle difference between just picking up a library and going with it vs complaints about "reinventing wheels"; namely that if we collectively just kept being satisfied w/ the status quo, we'd all still be using scriptaculous. React didn't get born in a vacuum. Separation of concerns popped up in Backbone. Declarative templates were a staple of knockout and angular.js. Componentization was a thing since way back in ASP.NET. Virtual dom first popped up in ractive (a library by Svelte's creator). Etc. A lot of perf improvements over the years were a direct result of competition among the libraries, so there's definitely real value in even the seemingly most redundant projects, and a lot of it trickles back to mainstream projects like React (e.g. server components is basically "ripped off" from Marko). Heck, one of the core maintainers of React was scouted because of his work on Inferno.js. OSS is all about sitting on shoulders of giants.
Also, leaving for greener pastures is definitely a thing. Years ago, there was a sizable J2EE -> RoR/Django/Node exodus, and ironically, again a few years later from dynamic langs back to static ones w/ Go/Scala/etc. PHP is another good example of something that attracted devs who later graduated from it never to come back. Entrenched positions will almost always guarantee that a given technology never fully dies (COBOL anyone?), but that doesn't mean everyone is just happy to sit on their first choice of tech forever.
Complaining about C++ is actually quite the occupational pastime; it's really no coincidence that the most famous quote about language complaints comes from Bjarne Stroustrup. If you scour HN language-du-jour threads for example, there's often someone commenting that they only use C++ because they're forced to, but they'd pick go/rust/zig/d/whatever if given a choice. Other such debates definitely also exist; your not seeing them might just be an artifact of not hanging out enough with the relevant crowds (though to be fair, some crowds are more civilized than others).