Top
Best
New

Posted by mdhb 6 days ago

The time is right for a DOM templating API(justinfagnani.com)
218 points | 249 commentspage 3
llcooliovice 5 days ago|
> There are in-flight proposals for very low-level DOM update primitives, like DOM Parts, which target framework implementations, but I think higher-level APIs like full declarative templating can take even more load off, help prove out and complete the lower-level API proposals, and be really impactful for developers and users.

There is an alternative suggestion to DOM parts which might be a better bet: https://github.com/whatwg/dom/issues/736

codedokode 5 days ago||
The web platform is over bloated. The proper solution would be a minimum set of APIs and set of reusable by many sites JS/Wasm libraries.

For example, most of Web Audio (thing like filters and oscillators except for actually sending audio to audio card) could be implemented in Wasm making a browser simpler and not allowing to use it for fingerprinting. Also, base64 encoding/decoding, URL handling function, most of canvas code etc. Imagine how less work for browser developer it would be.

yoz-y 5 days ago|
I think web needs an “actually standard” library. Something that vendors could ship with browsers but could be also updated to higher version of a site requires it (and cached forever).

That would allow us to not download a bunch of code every time.

ukuina 5 days ago||
Nit: The post keeps referring to "standard JSX" as though trying to will such a thing into existence.
jmull 5 days ago|
I thought one of the main points the article is making about JSX is that it currently isn’t standardized.
austin-cheney 5 days ago||
DOM templating is just like JavaScript classes. Classes in JavaScript were requested since the earliest of times and always rejected until ES6 (2014), because they are/were:

* always unnecessary

* always artificial

* only vanity

* only desired by insecure persons not familiar in the technology

* only qualified as bad idea but necessary because people were just going to do it anyways

So far the DOM has managed to escape this stupidity only because it is not a part of JavaScript. Java people ignorant of JavaScript desirous of features to make JavaScript feel more like Java has no bearing on the DOM, for example, because they are separate technologies managed by unrelated organizations.

None of the ergonomic reasoning mentioned in the article are qualified. Just because many people lack confidence in the technology and knowingly make poor design decisions doesn’t mean a familiar vanity layer will fix anything. Declarative comfort, for example, is not a resolution to performance and security problems just because other knowingly bad design decisions are worse. Two wrongs don’t make a right.

Furthermore the DOM already has a slow unnecessary declarative abstraction layer insecure people cannot live without called querySelectors. In other words this proposal is to React as querySelectors are to jquery, and classes are to Java. These are/were trends and trends die over time. We really should move past vanity as an alternative to an absence of training.

9rx 5 days ago||
It is slightly different. Classes ended up being worse than what they were trying to supplement. Templating, if done right at least (a big if, granted), can bring small improvement. If classes were better, we wouldn't see them the same way now.
owebmaster 4 days ago||
How classes could be better? I don't think it's missing something that would make me use it
yoz-y 5 days ago||
I’m curious. What is actually wrong with querySelector?
austin-cheney 5 days ago||
String interpolation is so ridiculously slow. Epic slow.

Perhaps just as importantly is that is a crutch for many to avoid accessing the DOM in steps. You can read from the DOM with querySelectors but you cannot modify the DOM with them. If querySelectors is all you can do then you must use some third party template system because you have no idea how any of this works even though it provides maximal expressive freedom.

yoz-y 4 days ago||
Is anything preventing JS from compiling the proper stepwise dom access if the querySelector parameter is a literal? That would basically push the interpolation into the parsing step.
hsn915 6 days ago||
What we need is not templating. What we need is a native implementation of a virtual dom.

More specifically, a native implementation of the "patch" function:

    patch(target_dom_node, virtual_dom)
Where `virtual_dom` is just a plain-data description of the DOM.

Most of the "slowness" of the DOM come from its requirement to be a 90's era Java style object hierarchy.

Don't call it "templating". Just call it "virtual dom". Everyone knows what that means.

ethan_smith 5 days ago||
A native virtual DOM implementation would also drastically reduce memory overhead since browser engines could optimize diffing algorithms at the C++ level instead of requiring megabytes of JavaScript framework code to be downloaded, parsed and executed on every page load.
silverwind 5 days ago|||
Virtual DOM is a useless abstraction, there are numerous libs that perform fine without it.
nine_k 5 days ago||
It's a useful abstraction: you just build the full DOM with every change, a bit like a game engine. It makes so many things simpler.

It's not a free abstraction though.

sethaurus 6 days ago||
Other than quibbling over the word "template", how does that differ from what TFA is describing?
hsn915 5 days ago||
The linked proposal has many "features" that would be "needed" if you frame the problem in terms of a "template api", centered around "binding" variables, and what not.

https://github.com/WICG/webcomponents/issues/1069

My proposal only adds one native function with nothing else: no new data types, no new apis.

WickyNilliams 5 days ago||
Doesn't your proposal implicitly introduce the concept of a virtual DOM, which the browser does not have?

You'd need to spec out what that looks like. It adds one new API from the users perspective but much more from the browsers perspective.

Additionally the next generation of Frameworks do not use virtual DOM. Solid and svelte do not. Vue is moving away from it. Signals are directionally where they're all heading.

hsn915 5 days ago||
The API surface I propose only includes one function. No type needed.

It does not even require the target node to be created with a virtual dom in the first place. Just diff the node with the given tree structure efficiently.

Internally the browser might need to create a few accelerator data structures, but that's an implementation detail.

bravesoul2 6 days ago||
Depends where the platform boundary is for Web. As much as we hate JS fatigues and so many frameworks, choice is good. Maybe if the browser can make it easy for these frameworks to be performant and integrate more deeply (not part of the JS bundle but more like a deeper JS 'plugin' with bonus of sometimes having a cache hit from another site) we could just carry on using React et. al.
PaulHoule 6 days ago||
What about

https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/...

?

The next two documents are part of a set that I made which did DOM-based templating on the back end in Java

https://ontology2.com/the-book/html5-the-official-document-l...

https://ontology2.com/the-book/source-code-transclusion-in-h...

one trouble is that systems that work at the DOM tree level are an order or two magnitudes slower than string-based templating systems. Hypothetically you could do some interesting things like hygenic macros and merge together arbitrary documents, rewriting the CSS classes and such. But by and large people find string-based templates to be good enough and don't way to pay the price for something more expensive.

WorldMaker 6 days ago|
Currently <slot>s only have automatic behavior when attaching a <template> to the Shadow DOM to a node with existing "Light" DOM children, which mostly only happens with Web Components (and for now Web Components require JS).

So it is not yet a full, generic templating solution.

Also, this article goes on at length about how the templating needs to be "reactive" and not just "builds a DOM tree", and <slot> doesn't do that yet at all, even in the automatic behavior scenarios, it's a one time "merge".

Kicking the can along the road of the complexity of "reactive" components is a large part of how we've got the (quite basic) <template> and <slot> tags that we got, and I think why the article is still currently impractical. There needs to be more agreement on what "reactive" means. The article mentions the signals proposal, and that's one possibility that a lot of frameworks are pushing for right now, but it's still a process before browsers agree to support something like that, and something like that is a dependency before agreeing on what a "reactive" template language can be/how it would work out of the box.

bevr1337 6 days ago||
As mentioned, the DOM API is a stinker. Does this address that root issue?

I'd love to see something that builds on the work of hyperscript and HAST. They are great models of the DOM. It would be exciting if a template language were syntax sugar.

JSX is easy to reason about because its elements are 1:1 with a single, uniform function call. That feature means JSX is always optional. Sometimes it is even more verbose or less-performant to use JSX than a hyperscript API like specifying optional properties. I think errors and call stacks are clearer than during string interpolation, but that's possibly BS.

Web components offer limited data binding and the hyperscript approach has clear control flow. The templates seem to be a source of confusion in the GH discussions.

There is still something special and pleasant about jquery because its API was a reflection of the domain. As a developer, I want to query for a node (CSS selector, xpath, etc.) to affect change and traverse to other nodes. After a beer or two I'm convinced emacs and org mode approaches are worth emulating in the web.

Great article and linked discussions. Thanks for sharing.

TheRealPomax 5 days ago||
> This kind of templating is the cornerstone of all modern web frameworks and rendering libraries these days, all of which let you declaratively combine markup with data

Okay but just because fighting the river has become popular doesn't mean wanting to pave over the river is a good idea. It might be the logical _conclusion_ to fighting the river, but you could also just... stop fighting the river and use it the way it was intended to be used again.

ulrischa 5 days ago|
The answer is here on the HN Start-page: XSLT
More comments...