Comments on: An Approach to Lazy Loading Custom Elements https://css-tricks.com/an-approach-to-lazy-loading-custom-elements/ Tips, Tricks, and Techniques on using Cascading Style Sheets. Thu, 28 Sep 2023 04:44:22 +0000 hourly 1 https://wordpress.org/?v=6.4.3 By: Brandon Bennett https://css-tricks.com/an-approach-to-lazy-loading-custom-elements/#comment-1805786 Thu, 28 Sep 2023 04:44:22 +0000 https://css-tricks.com/?p=376991#comment-1805786 This is really neat! I think using <script type="module"> may work great here too actually.

Rather than creating a <script> tag to manually load that component’s implementation, you could instead call the dynamic import() function.

Come to think of it, you don’t actually have to migrate to <script type="module">, you would have to ensure running your components in "use strict" mode though, since that’s a requirement to run in ‘module’ mode using the import() statement.

Here would be the updated version of the AutoLoader.load() method with the import()-based syntax (I decided to do it in TS just to help explain things in my demo):

async load(tag: string): Promise<void> {
  const src = this.elementURL(tag);
  await import(src);
}

This essentially just run the other script as a side-effect, you aren’t getting any references from the export of the script. It’s only to load the code that runs the Custom Element definitions.

]]>
By: Frederik Dohr https://css-tricks.com/an-approach-to-lazy-loading-custom-elements/#comment-1802955 Wed, 15 Feb 2023 09:16:41 +0000 https://css-tricks.com/?p=376991#comment-1802955 I’d be interested to see an example of your iframes approach, Michael; care to put it in a CodePen or Gist? (The latter would allow for dedicated discussions.)

I sympathize with the HTML Import mourning, though I go back and forth on what it could or should have been.

]]>
By: Michael Ninin https://css-tricks.com/an-approach-to-lazy-loading-custom-elements/#comment-1802941 Tue, 14 Feb 2023 06:21:27 +0000 https://css-tricks.com/?p=376991#comment-1802941 I am still dreaming of html imports. Think of any landing sections in separate html files. The important thing for me is I want for them to be shown in Sources tab along with others. So fetch is not helpful. So I tried a dedicated custom element with iframes loading separate html files in it, and that element is grabbing their documents contents and move them into our main one. That is solving my issue with showing them in Sources tab. But races are killing me… It works like 15 times out of 16.(

]]>
By: Frederik Dohr https://css-tricks.com/an-approach-to-lazy-loading-custom-elements/#comment-1802939 Mon, 13 Feb 2023 20:27:21 +0000 https://css-tricks.com/?p=376991#comment-1802939 Oh, I hadn’t thought of :defined in this context; that’s an excellent idea and much more elegant, thank you!

I agree that an approach like that elems object can be advantageous in some scenarios (thus the cautionary aside in the introduction). In my case, I specifically didn’t want to rely on bundlers or similar tooling though.

]]>
By: Molly Stewart-Gallus https://css-tricks.com/an-approach-to-lazy-loading-custom-elements/#comment-1802937 Mon, 13 Feb 2023 19:59:18 +0000 https://css-tricks.com/?p=376991#comment-1802937 Why not use :not(:defined) to query for unloaded elements.

I would also use a different sort of autoloader which is more easily WebPackable. Something like

const elems = {
'my-elem': () => import('./my-elem.js')

};
// …

customElements.define(elem, await elemselem. default);`

]]>