On nice use, that took me maybe 30m to code up, is a `<remote-fragment>` element.
<remote-fragment src=/fragments/topbar.htmlf />
simply retrieves the specified file and adds it to the DOM where the `remote-fragment` is declared.No need for server-side scripting to include common parts of the HTML page.
There's a bunch of similar use-cases currently satisfied by fat client frameworks or server-side languages that simply go away when you have custom elements.
So, yeah, you can do components with webcomponents they way you do much of react components, but there's some stuff that you can do with webcomponents that just makes life a little bit better[1].
[1] Experimenting with propagating state throughout a (client-side) application using only declarations in HTML, with custom elements performing stuff.
After all, a custom element that takes a front-end newcomer 30m to write is absolutely preferable to a large infrastructure-based standard that, after 22 years, is still not included in the spec.
Look at the options for including HTML fragments in web pages:
1. Complex client-side react/vue/etc,
2. A server language ,
3. A build-step including a non-spec bundler/packer,
4. A `<remote-fragment src="..." />
My point is that webcomponents make quite a lot of complicated things redundant for particular use-cases. As time goes on I see the fat front-end frameworks having more of their functionality being replaced with less complex (to use) custom elements.
(Hence my experiments with value propagation using custom elements)
IOW, I expect this to be an experiment that I eventually throw away after having gained some insight. The `ps` in `psjs` below means "publish/subscribe".
The big draw for me about htmx is that common usage does not require knowledge of Javascript! This means that htmx (and my set of custom elements) does not require the creator of the front-end to know that npm even exists, much less how to use it. The same goes for Virtual DOMs, Hooks, tree-shaking/web-packing, promises and async, functions, and the whole container-ship full of arbitrary things that common front-end stacks need you to know.
My custom elements require javascript (for now), and require that the user know what is meant by 'publish a message onto a queue' and 'subscribe to a queue for messages of a particular subject'.
-------------------------------------
I have at least four custom elements: psjs-tree, psjs-bind, psjs-subscribe and psjs-publish.
I also have two JS functions, `publish` and `subscribe`: `publish` publishes an arbitrary payload (a JS object) to a channel (string) with a subject (also of string type). `subscribe` registers a callback function for a channel (string) with a pattern for the subject.
These are all global. A piece of code could do:
subscribe("ERROR", "*", (sender, subject, payload) => {
dialog.innerHTML = `${sender.id}: Error ${subject} ${payload.errMessage}`;
dialog.showModal();
});
and then any code, anywhere else can do `publish(this, "ERROR", "Rpc Request", { errMsg: "Rxed 404"});`My web components psjs-publish and psjs-subscribe execute those two functions (for publish, there is an `onevent` attribute). The psjs-publish component uses the closest psjs-tree ancestor to determine which fields go into the payload. The fields are set by psjs-bind. The psjs-subscribe does the opposite of psjs-publish, in that it uses the closest psjs-tree ancestor to determine which fields of the payload should be used to update the contents or values of the psjs-tree descendents.
For example, a form that can have the values reset by some other code (say, fetching them from a server) and can have the values submitted:
<psjs-tree>
<psjs-subscribe channel="MYFORM" subject="update-fields"> </psjs-subscribe>
<psjs-bind for="team-name"> </psjs-bind>
<psjs-bind for="notifications"> </psjs-bind>
<psjs-bind for="user-name"> </psjs-bind>
<psjs-bind for="user-email"> </psjs-bind>
<form>
<div id="title"> Example</div>
<div id="team-name"> </div>
<div id="notifications"> </div>
<input id="user-name"> </input>
<input id="user-email"> </input>
<psjs-publish onevent="click" channel=MYFORM subject=submit-fields>
<button>Submit</button>
</psjs-publish>
</form>
</psjs-tree>
Of course, this means that I have a line of js somewhere that looks like this: subscribe("MYFORM", "submit-*", (sender, subject, payload) => { /* send payload to server */ });
And another that looks like this: const rsp = successfulFetchFromServer();
publish(this, "MYFORM", "update-fields", { "team-name": rsp.teamName, ... });
This is still very much a WIP, and I'm playing with having custom elements for performing RPC without using the publish/subscribe functions; this is so that, during usage of these components, there will be zero JS to write. Right now there is still the publish/subscribe calls that intercept messages and tx/rx messages from the server to local components.I'd like it to look like this eventually:
<rpc-tree>
<rpc-rx subject="Some Subject">
<rpc-bind for="team-name"> </rpc-bind>
<rpc-bind for="notifications"> </rpc-bind>
<rpc-bind for="user-name"> </rpc-bind>
<rpc-bind for="user-email"> </rpc-bind>
<form>
<div id="title"> Example</div>
<div id="team-name"> </div>
<div id="notifications"> </div>
<input id="user-name"> </input>
<input id="user-email"> </input>
<rpc-tx onevent="click" href="/some/path/to/submit" subject="Some Subject">
<button>Submit</button>
</rpc-tx>
</form>
</rpc-tree>
The psjs-subscribe/publish elements are still useful to propagate value changes locally within the client, but for non-local state an RPC set of elements seems preferable.All in all (and I realise that I wrote an exceptionally long post, so sorry!), I feel that htmx is better right now, but ... it forces the creation of a backend-for-frontend layer, so you have the layers of front-end -> backend-for-front-end -> API. With RPC calls only, I don't need to write the middle layer and can simply use the API directly from the client application, especially when using the custom element for specifying fragments.
In websites, search engines ignoring the common elements that you would use this for, such as navbars, isn't a deal-breaker.
When I search Google for keyword FOO, results that have FOO only in the elements common to all web pages are useless results.
I'm typically looking for FOO in the content, not in the navigational elements or other common elements.
So for example, some of the HTML in the "template:" section of a VueJS component would be tags which are defined by web components...
A framework component is effectively a reusable block of code. It's an organizational principle more than anything; modular and reusable, but not meaningful to the browser in its own right once rendered. A web component, on the other hand, is very literally a new HTML element, and is treated by the browser as such. Once defined, the browser itself sees that component as a meaningful entity in its own right.
While the difference between those two ideas is fairly subtle on the surface, there's a whole nest of smaller differences that result from it. For just one example, web components are perfectly AJAX-compatible with no extra strings attached - you simply add the HTML tag representing a component to the DOM, and it works out of the box, no matter when or from where you got it. Achieving that with a framework component requires some additional work at best, which may or may not have been done for you by the framework authors.
That's not to say one is necessarily better than the other either, to be clear - they're just different, and treating them as if they were the same does both a disservice.
I think that, for me as a newcomer to front-end development, this is the biggest takeaway: a framework component is a reusable block of javascript while a webcomponent is a reusable HTML element.
As a newcomer[1] it does not make sense to even learn the front-end frameworks. As an example, the time spent in learning the entire framework simply so I can 'watch' an element's value for changes, or propagate an element's changed value to other elements is going to be order of magnitudes greater than simply writing 3 custom elements that monitor and propagate values for any child element.
(PS. I like your post. I think a followup blog post with two concrete examples may make sense in refining your thesis:
Example 1. This is a popular React mechanism to do $FOO, here's how it is easier in a webcomponent.
Example 2. This is a complex webcomponent, here's how it is easier in React.)
[1] One who is not looking to put "react", "vue", etc on their CV.
So is a web component…
I think if you are new to webcomponents[1] it might help if you list all the useful functionality you get from a front-end framework, and look up how to achieve the same thing using webcomponents (Custom element, templates, slots).
[1] As I am. I am, in fact, new to front-end in general.
I loved her opinion, I've always shared that sentiment but I was afraid of saying it.
I also wish there was a way to develop such abstractions (WebComponents) with/in HTMX.
<template mixin="htmx" global>
<script on="connect">htmx.process(root)</script>
</template>Are you new or something?
Web components are so 2012
> [Framework components] usually consist primarily of JavaScript, with only a very thin layer of HTML and CSS to provide their structure and are usually compiled away on the server side before they ever reach the DOM [...]
Is there any decent data on how many sites built with SPA frameworks end up using SSR, or even just anecdotal reports? A decent amount of what I read online seems to assume devs are using SSR, but I'm not sure if that's an accurate representation or just hype/pushing newer technology.