News for hackers. Stuff that matters.
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.