Trendyol / baklava

Baklava is a design system provided by Trendyol to create a consistent UI/UX for app users.
https://baklava.design/
MIT License
1.26k stars 110 forks source link

Lazy-loading components #103

Open muratcorlu opened 2 years ago

muratcorlu commented 2 years ago

Currently, our main file in CDN fetches and registers all of the components immediately. It's also possible to hand pick components by users to only download and use them like:

<script type="module" src="https://cdn.jsdelivr.net/npm/@trendyol/baklava@2.0.0-beta.14/dist/components/button/bl-button.js"></script>

But the idea in this issue is to check the possibility and feasibility of lazy loading components dynamically once they are used in the DOM. In this case, user will just include the main file (or another main file for the lazy loading approach) as always. But this time, instead of downloading and registering all of the components, the library will register a placeholder component for all of our components. And this placeholder component will load the actual one in its connectedCallback method. In theory, this will prevent downloading most part of the library on landing pages since, most of the time, a single page use a small part of the all components.

But there are some points we need to consider here:

muratcorlu commented 2 years ago

If we decide to implement a feature like this, we can get some inspiration from HMR plugin of @web/dev-server. This plugin uses JavaScript Proxy classes as placeholder components.

muratcorlu commented 7 months ago

Shoelace has this functionality since v2.3.0 with the experimental "autoloader". It tracks DOM changes with MutationObserver and loads components if it sees any new html element that has a tag name starting with sl-. More details: https://shoelace.style/getting-started/installation#cdn-installation-easiest

I'm not sure if this is a good approach though.