News for hackers. Stuff that matters.
Re:Web Components Aren't Framework Components by nesarkvechnep on Monday December 11, 2023 @01:35AM (#38598185)
It seems like you reinvented Edge Site Includes (https://en.m.wikipedia.org/wiki/Edge_Side_Includes).
Re:Web Components Aren't Framework Components by lelanthran on Monday December 11, 2023 @01:55AM (#38598272)
I like to think that I didn't reinvent it (just like react, et al didn't reinvent it), I only refined the concept into the simplest form that works :-)

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)

Re:Web Components Aren't Framework Components by nesarkvechnep on Monday December 11, 2023 @03:50AM (#38598794)
Refined the concept into the simplest form that works… as long as you have JavaScript enabled.
Re:Web Components Aren't Framework Components by lelanthran on Monday December 11, 2023 @05:18AM (#38599227)
What's the alternative other than simply doing without this feature?
Re:Web Components Aren't Framework Components by Quekid5 on Monday December 11, 2023 @06:40AM (#38599726)
E.g. Apache's Server Side Includes[0] allows for

   <!--#include virtual="/footer.html" --> 
Tbf, you did specify 'server language' in a post further up the comment chain, but the the thing is that it doesn't have to be much more complicated that SSI -- assuming it's available for your deployment target, of course.

(SSI obviously does not require JavaScript.)

[0] https://httpd.apache.org/docs/current/howto/ssi.html

Re:Web Components Aren't Framework Components by lelanthran on Monday December 11, 2023 @07:50AM (#38600193)
I've used those before, but I feel that having something that works within the spec, independent of any particular implementation's extensions is much more valuable for longevity.

Sure, you can use $A if using $B server, or use $C when using $D language, or use $E when using $F framework, or use $G when using $H reverse-proxy, or use $I when using $J CDN, or use $K when using $L hosted cloud service ...

Or you can just use what's in the specification which works on all of the above, but won't work on clients with Javascript disabled.

It's a "choose your poison" scenario.

Re:Web Components Aren't Framework Components by mewpmewp2 on Monday December 11, 2023 @01:24PM (#38603350)
Technically you could also just write a bit of JS within the HTML to do without needing any of the first 3.

You could querySelectorAll and check the src attribute etc, then fetch the content put it in the innerHTML.

    <remote-fragment src="https://..." />

    <script>

      document.querySelectorAll("remote-fragment").forEach(async (el) => {

        const src = el.getAttribute("src");

        const result = await fetch(src).then((res) => res.text());

        el.innerHTML = result;

      });

    </script>

Or if you want to react to any such component inserted at times down the line or src attribute changes, you could use MutationObserver.
Re:Web Components Aren't Framework Components by lelanthran on Tuesday December 12, 2023 @12:41AM (#38609935)
That breaks on edge cases.

For example it won't work recursively when a fetched fragment has fragments of its own.

Or when the js snippet loads before some fragment element is loaded (say, from a DOM update).

The nice thing about custom elements is that you get to treat them exactly like normal elements.

Re:Web Components Aren't Framework Components by mewpmewp2 on Tuesday December 12, 2023 @05:56PM (#38619950)
In this case you could use MutationObserver - although we are doing manually the logic web components provide, but my point is that you can do it also without using web components. My code doesn't include attribute changes, but this should be possible to also do with MutationObserver.

    <body>
    <script>
      const onRemoteFragmentMount = async (el) => {
        const src = el.getAttribute("src");
        const result = await fetch(src).then((res) => res.text());
        el.innerHTML = result;
      };

      const callback = (mutationsList, observer) => {
        for (let mutation of mutationsList) {
          if (mutation.type === "childList") {
            mutation.addedNodes.forEach((node) => {
              if (node.tagName === "REMOTE-FRAGMENT") {
                onRemoteFragmentMount(node);
              }
            });
          }
        }
      };

      const observer = new MutationObserver(callback);
      const bodyElement = document.querySelector("body");
      observer.observe(bodyElement, {
        childList: true,
        subtree: true,
      });
    </script>

    <remote-fragment src="..." />
    </body>
Re:Web Components Aren't Framework Components by KatrinaKitten on Tuesday December 12, 2023 @06:00PM (#38620000)
While you certainly could do this, a major benefit of web components is that you get all of this for free out of the box. It's less error/mistake prone, keeps your code cleaner and more obvious exactly what it does, etc.
Re:Web Components Aren't Framework Components by dannye on Tuesday December 12, 2023 @07:53AM (#38611511)
why waste 28 minutes writing your own component? copy/paste takes 2 minutes https://dev.to/dannyengelman/load-file-web-component-add-ext...