<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vipul Tiwari on Medium]]></title>
        <description><![CDATA[Stories by Vipul Tiwari on Medium]]></description>
        <link>https://medium.com/@vipul.t220?source=rss-a7bba072cf82------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*hjfB0JUNNVAosheH</url>
            <title>Stories by Vipul Tiwari on Medium</title>
            <link>https://medium.com/@vipul.t220?source=rss-a7bba072cf82------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 05:46:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vipul.t220/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Modular Frontend in AEM: Lazy-Load Components with ES Modules + IntersectionObserver]]></title>
            <link>https://medium.com/@vipul.t220/modular-frontend-in-aem-lazy-load-components-with-es-modules-intersectionobserver-53b1c60a14aa?source=rss-a7bba072cf82------2</link>
            <guid isPermaLink="false">https://medium.com/p/53b1c60a14aa</guid>
            <category><![CDATA[web-page-performance]]></category>
            <category><![CDATA[intersection-observer]]></category>
            <category><![CDATA[aem]]></category>
            <category><![CDATA[frontend-development]]></category>
            <category><![CDATA[lazy-loading]]></category>
            <dc:creator><![CDATA[Vipul Tiwari]]></dc:creator>
            <pubDate>Mon, 04 Aug 2025 17:11:08 GMT</pubDate>
            <atom:updated>2025-08-04T17:11:08.968Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fZcb4k5AoknLrKGXga_pcA.png" /></figure><p>A chill walkthrough of how I stopped shipping 700KB of unused JavaScript and made my AEM site smarter, faster, and a little crishpier.</p><p><strong>If you’ve ever worked on an AEM project, this probably sounds familiar:</strong></p><ul><li>You ship one <strong>giant JS file</strong>.</li><li>Every component’s logic — whether visible or not — loads upfront.</li><li>Your Lighthouse scores cry.</li><li>You cry too.</li></ul><p>And for what? Half the components aren’t even visible on load. Some are buried in carousels, modals, accordions, or five scrolls down a page.</p><p><strong>So why are we loading everything at once?</strong></p><h4>My “Wait a Second…” Moment</h4><p>I wanted to:</p><ul><li>JS should only load when a component <em>actually shows up on screen</em>.</li><li>Each component should be in its own neat little folder.</li><li>Webpack config shouldn’t turn into a Jenga tower of entry points.</li><li>And I should be able to support both Vanilla JS and React components (because enterprise projects always have both 🙃).</li></ul><p><em>Then it hit me.</em></p><blockquote><em>What if I use just </em><strong><em>one </em></strong><strong><em>main.js</em></strong><em> file to manage everything, and inside it, use </em><strong><em>IntersectionObserver</em></strong><em> to lazy-load component scripts </em><strong><em>on demand</em></strong><em>?</em></blockquote><p>And booooom the modular frontend was born.</p><h4>The Setup (It’s Easier Than You Think)</h4><h4><strong><em>Project Structure</em></strong></h4><p>Let me break it down real quick:</p><ul><li>main.js: One single entry file, the brains of the operation.</li><li>component-loader.js: The bouncer. Only lets components in when they’re ready (i.e., visible).</li><li>component-name.js: Each component&#39;s personal script file.</li><li>Webpack handles chunking like a pro.</li><li><strong><em>IMP: Always keep component folder name and name of component js same.</em></strong></li></ul><pre>src/<br>├── components/<br>│   ├── card/card.js<br>│   ├── carousel/carousel.js<br>├── component-loader.js<br>└── main.js ← just this one file in Webpack!</pre><p>Every component lives in its own folder (You can add html, jsx or hbs files inside the component folde). No sharing toothbrushes here. 👌</p><h4>How It Works:</h4><p>Here’s how this system behaves:</p><ol><li>When the page loads, only main.js runs. That’s it.</li><li>Inside main.js, we import component-loader.js.</li><li>component-loader.js looks for every element on the page with a data-component=&quot;xyz&quot; attribute.</li><li>It sets up an IntersectionObserver to watch those elements.</li><li>As soon as one of them <strong>scrolls into view</strong>, we dynamically import its corresponding JS file:<br> → import(&#39;./components/carousel/carousel.js&#39;)</li><li>That file runs, initializes whatever logic it contains, and everyone’s happy.</li></ol><p><strong>HTML Example (AEM Markup)</strong></p><p>Let’s say AEM renders this:</p><pre>&lt;section class=&quot;carousel&quot; data-component=&quot;carousel&quot;&gt;<br>  &lt;!-- carousel markup --&gt;<br>&lt;/section&gt;</pre><p>That data-component=&quot;carousel&quot; is our golden ticket. We use it to figure out <strong>which JS to load and when</strong>.</p><p><strong>The Component Loader:</strong></p><pre>// component-loader.js<br><br>const observer = new IntersectionObserver((entries, obs) =&gt; {<br>  entries.forEach(async (entry) =&gt; {<br>    if (!entry.isIntersecting) return;<br><br>    const el = entry.target;<br>    const componentName = el.dataset.component;<br><br>    try {<br>      // This line dynamically imports JS only when needed<br>      await import(`./components/${componentName}/${componentName}.js`);<br>      obs.unobserve(el); // Done, no reloading this again<br>    } catch (err) {<br>      console.error(`Couldn&#39;t load ${componentName}:`, err);<br>    }<br>  });<br>});<br><br>document.querySelectorAll(&#39;[data-component]&#39;).forEach((el) =&gt; {<br>  observer.observe(el);<br>});</pre><p>It’s like saying:</p><blockquote><em>“Hey Webpack, only load </em><em>carousel.js when someone’s actually looking at a carousel.”</em></blockquote><p>And Webpack’s like: “Gotchu.”</p><p><strong>main.js — The One Entry to Rule Them All</strong></p><pre>// main.js<br>import &#39;./component-loader.js&#39;;</pre><p>That’s it. Just one line. No long list of components to import manually.</p><p>Webpack Config</p><p>No more entry: { card, carousel, modal, ... }. This is all you need:</p><pre>entry: {<br>  main: &#39;./src/main.js&#39;,<br>},<br>output: {<br>  filename: &#39;js/[name].js&#39;,<br>  chunkFilename: &#39;js/chunks/[name].js&#39;,<br>  module: true,<br>  chunkFormat: &#39;module&#39;,<br>  chunkLoading: &#39;import&#39;,<br>  environment: {<br>    module: true,<br>  },<br>},<br>experiments: {<br>  outputModule: true,<br>},</pre><p>Now Webpack only generates main.js and lazy-loaded chunks like carousel.js when needed.</p><h4>Component Example — React or Vanilla? or both?</h4><p>For Vanilla JS:</p><pre>// carousel.js<br>console.log(&#39;Carousel loaded!&#39;);<br>initCarousel(document.querySelector(&#39;.carousel&#39;));</pre><p>For React:</p><pre>// carousel.js<br>import React from &#39;react&#39;;<br>import { createRoot } from &#39;react-dom/client&#39;;<br>import Carousel from &#39;./Carousel.jsx&#39;;<br><br>const el = document.querySelector(&#39;.carousel&#39;);<br>createRoot(el).render(&lt;Carousel /&gt;);</pre><p>The loader doesn’t care what’s inside the file — as long as it runs, you’re good.</p><h4><strong>FINAL RESULT</strong></h4><p>Initial JS Loaded <strong>~700 KB</strong> to <strong>~150 KB</strong><br>Time to Interactive <strong>~4.2 sec</strong> to <strong>~1.3 sec</strong><br>Webpack Entry Files <strong>20+ to 1</strong><br>Dev Happiness 😵‍💫 to😎</p><p><em>— If this made you smile, taught you something, or gave you a better Lighthouse score — share it, comment, or send me a meme of your current bundle size. I’ll cry with you. </em>😅</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53b1c60a14aa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>