<?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 Mitul shah on Medium]]></title>
        <description><![CDATA[Stories by Mitul shah on Medium]]></description>
        <link>https://medium.com/@mitulshah96?source=rss-b0e3ff906c70------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*3eOtsc9p1y742uVI8UVubw.jpeg</url>
            <title>Stories by Mitul shah on Medium</title>
            <link>https://medium.com/@mitulshah96?source=rss-b0e3ff906c70------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 21:11:35 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mitulshah96/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[Optimise Your Web-App Performance]]></title>
            <link>https://medium.com/@mitulshah96/optimise-your-web-app-performance-1aa9529fb116?source=rss-b0e3ff906c70------2</link>
            <guid isPermaLink="false">https://medium.com/p/1aa9529fb116</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-performance]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[optimise-web-pages]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Mitul shah]]></dc:creator>
            <pubDate>Tue, 16 Jun 2020 04:42:42 GMT</pubDate>
            <atom:updated>2020-06-16T04:42:42.256Z</atom:updated>
            <content:encoded><![CDATA[<p>Ever wonder how Flipkart or Facebook are able to load the web page very fast?. This question always comes in mind. How can I improve my site such that it also able to load as quickly as them. This made me to investigate the thing which need to be taken care off while developing your site. Here in the AR Wealth Team we have implemented following stuff below.</p><p>I am going to cover few things which i came across while developing the site and made my website to load up quickly and enhance the performance from 3 to 37.</p><p>Topic to be covered.</p><ol><li>App Shell Model</li><li>Optimise your JavaScript</li><li>Optimise your CSS</li></ol><p><strong>App Shell Model</strong></p><p>This model is a way to build a PWA (Progressive Web App) that instantly load the user screen by taking minimal HTML,CSS and JavaScript required to power the UI and when cached offline can ensure instant,reliable,good performance.</p><p>For <a href="https://en.wikipedia.org/wiki/Single-page_application">single-page applications</a> with JavaScript-heavy architectures, an application shell is a go-to approach. This approach relies on aggressively caching the shell (using a <a href="https://developers.google.com/web/fundamentals/primers/service-worker">service worker</a>) to get the application running.Next, the dynamic content loads for each page using JavaScript.</p><h3>Example HTML for an app shell</h3><p>This example separates the core application infrastructure and UI from the data. It is important to keep the initial load as simple as possible to display just the page’s layout as soon as the web app is opened. Some of it comes from your application’s index file (inline DOM, styles) and the rest is loaded from external scripts and stylesheets.</p><p>All of the UI and infrastructure is cached locally using a service worker so that on subsequent loads, only new or changed data is retrieved, instead of having to load everything.</p><p>Your index.html file in your work directory should look something like the following code. This is a subset of the actual contents and is not a complete index file. Let&#39;s look at what it contains.</p><ul><li>HTML and CSS for the “skeleton” of your user interface complete with navigation and content placeholders.</li><li>An external JavaScript file (app.js) for handling navigation and UI logic as well as the code to display posts retrieved from the server and store them locally using a storage mechanism like IndexedDB.</li><li>A web app manifest and service worker loader to enable off-line capabilities.</li></ul><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;head&gt;<br>  &lt;meta charset=&quot;utf-8&quot;&gt;<br>  &lt;title&gt;App Shell&lt;/title&gt;<br>  &lt;link rel=&quot;manifest&quot; href=&quot;/manifest.json&quot;&gt;<br>  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;<br>  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;<br>  &lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;styles/inline.css&quot;&gt;<br>&lt;/head&gt;<br><br>&lt;body&gt;<br>  &lt;header class=&quot;header&quot;&gt;<br>    &lt;h1 class=&quot;header__title&quot;&gt;App Shell&lt;/h1&gt;<br>  &lt;/header&gt;<br><br>  &lt;nav class=&quot;nav&quot;&gt;<br>  ...<br>  &lt;/nav&gt;<br><br>  &lt;main class=&quot;main&quot;&gt;<br>  ...<br>  &lt;/main&gt;<br><br>  &lt;div class=&quot;dialog-container&quot;&gt;<br>  ...<br>  &lt;/div&gt;<br><br>  &lt;div class=&quot;loader&quot;&gt;<br>    &lt;!-- Show a spinner or placeholders for content --&gt;<br>  &lt;/div&gt;<br><br>  &lt;script src=&quot;app.js&quot; async&gt;&lt;/script&gt;<br>  &lt;script&gt;<br>  if (&#39;serviceWorker&#39; in navigator) {<br>    navigator.serviceWorker.register(&#39;/sw.js&#39;).then(function(registration) {<br>      // Registration was successful<br>      console.log(&#39;ServiceWorker registration successful with scope: &#39;, registration.scope);<br>    }).catch(function(err) {<br>      // registration failed :(<br>      console.log(&#39;ServiceWorker registration failed: &#39;, err);<br>    });<br>  }<br>  &lt;/script&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;<br></pre><h3>Caching the application shell</h3><p>An app shell can be cached using a manually written service worker or a generated service worker using a static asset precaching tool like <a href="https://github.com/googlechrome/sw-precache">sw-precache</a>.</p><p>Below is example service worker code that caches static resources from the app shell into the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Cache">Cache API</a> using service worker’s install event:</p><pre>var cacheName = &#39;shell-content&#39;;<br>var filesToCache = [<br>  &#39;/css/styles.css&#39;,<br>  &#39;/js/scripts.js&#39;,<br>  &#39;/images/logo.svg&#39;,<br><br>  &#39;/offline.html&#39;,<br><br>  &#39;/&#39;,<br>];<br><br>self.addEventListener(&#39;install&#39;, function(e) {<br>  console.log(&#39;[ServiceWorker] Install&#39;);<br>  e.waitUntil(<br>    caches.open(cacheName).then(function(cache) {<br>      console.log(&#39;[ServiceWorker] Caching app shell&#39;);<br>      return cache.addAll(filesToCache);<br>    })<br>  );<br>}); </pre><p><strong>Optimise your JavaScript</strong></p><ol><li><strong>Preload</strong> the most important resources</li><li><strong>Render</strong> the initial route as soon as possible</li><li><strong>Pre-cache</strong> the remaining assets</li><li><strong>Lazily load</strong> other routes and non critical assets</li></ol><p><strong>Preload the critical resource</strong></p><p><a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content">Preload</a> is a declarative fetch request that tells the browser to request a resource as soon as possible. Preload critical resources by adding a &lt;link&gt; tag with rel=&quot;preload&quot; to the head of your HTML document:</p><p>In my site one of my team member found that the font which was consuming around 50kb was taking more time for Initial load. He found that by preloading the fonts or any critical resource which is essential can be preloaded before window.onLoad event is been called.</p><pre>&lt;link rel=&quot;preload&quot; as=&quot;style&quot; href=&quot;css/style.css&quot;&gt;</pre><p>we just added preload in our index.html file which now help us to boost the performance by 2%</p><p>The browser sets a more appropriate priority level for the resource in order to try to download it sooner while not delaying the window.onload event.</p><h3>Render the initial route as soon as possible</h3><p>To improve First Paint, we found that inlining critical JavaScript and deferring the rest using <a href="https://developers.google.com/web/fundamentals/performance/critical-rendering-path/adding-interactivity-with-javascript">async</a>, as well as inlining critical CSS used above-the-fold. This improves performance by eliminating round-trips to the server to fetch render-blocking assets. However, inline code is harder to maintain from a development perspective and cannot be cached separately by the browser.</p><p>Another approach to improve First Paint is to server-side render the initial HTML of your page. This displays content immediately to the user while scripts are still being fetched, parsed, and executed. However, this can increase the payload of the HTML file significantly, which can harm <a href="https://web.dev/interactive">Time to Interactive</a>, or the time it takes for your application to become interactive and can respond to user input.</p><p>There is no single correct solution to reduce the First Paint in your application, and you should only consider inlining styles and server-side rendering if the benefits outweigh the tradeoffs for your application.</p><p>In our site we defer the non priority resource by using async keyword in index.html which was being used for analytic purpose.</p><p>Here the code snippet for the same.</p><pre>&lt;script async defer&gt;<br> window.dataLayer = window.dataLayer || [];<br> function gtag() {<br> dataLayer.push(arguments);<br> }<br> gtag(‘js’, new Date());</pre><pre>gtag(‘config’, ‘UA-148031339–1’);<br> &lt;/script&gt;</pre><h3>Pre-cache assets</h3><p>By acting as a proxy, service workers can fetch assets directly from the cache rather than the server on repeat visits. This not only allows users to use your application when they are offline, but also results in faster page load times on repeat visits.</p><p>Use a third-party library to simplify the process of generating a service worker unless you have more complex caching requirements than what a library can provide. For example, <strong>Workbox</strong> provides a collection of tools that allow you to create and maintain a service worker to cache assets. For more information on service workers and offline reliability, refer to the <a href="https://web.dev/service-workers-cache-storage">service worker guide</a> in the reliability learning path.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/0*6yZDsV73DSWHOoYE.png" /></figure><h3>Lazy load</h3><p>This includes all asset types, but large JavaScript payloads are especially costly due to the time it takes the browser to parse and compile them.</p><p>To send a smaller JavaScript payload that contains only the code needed when a user initially loads your application, split the entire bundle and lazy load chunks on demand.</p><p>Popular module bundlers like webpack, Parcel, and Rollup allow you to split your bundles using dynamic imports. For example, consider the following code snippet that shows an example of a someFunction method that gets fired when a form is submitted.</p><pre>import moduleA from &quot;library&quot;;<br><br>form.addEventListener(&quot;submit&quot;, e =&gt; {<br>  e.preventDefault();<br>  someFunction();<br>});<br><br>const someFunction = () =&gt; {<br>  // uses moduleA<br>}<br></pre><p>In here, someFunction uses a module imported from a particular library. If this module is not being used elsewhere, the code block can be modified to use a dynamic import to fetch it only when the form is submitted by the user.</p><pre>form.addEventListener(&quot;submit&quot;, e =&gt; {<br>  e.preventDefault();<br>  import(&#39;library.moduleA&#39;)<br>    .then(module =&gt; module.default) // using the default export<br>    .then(someFunction())<br>    .catch(handleError());<br>});<br><br>const someFunction = () =&gt; {<br>    // uses moduleA<br>}<br></pre><p>The code that makes up the module does not get included into the initial bundle and is now lazy loaded, or provided to the user only when it is needed after the form submission.</p><p>Still there are may possible ways to optimise your javascript code will cover up in the next session.</p><p>Now moving towards to</p><h3>Optimize your CSS</h3><p>1 Defer non critical CSS<br>2 Minify CSS</p><p><strong>Defer non critical CSS</strong></p><p>CSS files are render-blocking resources: they must be loaded and processed before the browser renders the page. Web pages that contain unnecessarily large styles take longer to render.</p><p>Let’s take an example. load this following url in your browser</p><p><a href="https://defer-css-unoptimized.glitch.me/">Critical CSS Demo - Unoptimized</a></p><p>This page requests a CSS file with eight classes, but not all of them are necessary to render the “visible” content.</p><p>The goal of this guide is to optimize this page, so only the critical styles are loaded synchronously, while the rest (like the ones applied to paragraphs), are loaded in a non-blocking way</p><p>Run lighout on the page and check performance tab in chrome dev tool.</p><p>The report shows the First Contentful Paint metric with a value of “1s”, and the opportunity Eliminate render-blocking resources, pointing to the style.css file:</p><p>To visualize how this CSS blocks rendering:</p><ol><li>Open <a href="https://defer-css-unoptimized.glitch.me/">the page</a> in Chrome.</li><li>Press Control+Shift+J (or Command+Option+J on Mac) to open DevTools.</li><li>Click the Performance tab.</li><li>In the Performance panel, click Reload.</li></ol><p>In the resulting trace, you’ll see that the FCP marker is placed immediately after the CSS finishes loading:</p><p>This means that the browser needs to wait for all CSS to load and get processed before painting a single pixel on the screen.</p><h3>Optimize</h3><p>To optimize this page, you need to know which classes are considered “critical”. You’ll use the the Coverage Tool for that:</p><ol><li>In DevTools, open the Command Menu, by pressing Control+Shift+P or Command+Shift+P (Mac).</li><li>Type “Coverage” and select Show Coverage.</li><li>Click the Reload button, to reload the page and start capturing the coverage.</li></ol><p>Double-click on the report, to see classes marked in two colors:</p><ul><li>Green (critical): These are the classes the browser needs to render the visible content (like the title, subtitle, and accordion buttons).</li><li>Red (non-critical): These styles apply to content that’s not immediately visible (like the paragraphs inside the accordions).</li><li>With this information, optimise your CSS so that the browser starts processing critical styles immediately after page loads, while deferring non-critical CSS for later:</li></ul><pre>&lt;style type=&quot;text/css&quot;&gt;<br>.accordion-btn {background-color: #ADD8E6;color: #444;cursor: pointer;padding: 18px;width: 100%;border: none;text-align: left;outline: none;font-size: 15px;transition: 0.4s;}.container {padding: 0 18px;display: none;background-color: white;overflow: hidden;}h1 {word-spacing: 5px;color: blue;font-weight: bold;text-align: center;}<br>&lt;/style&gt;</pre><ul><li>Then, load the rest of the classes asynchronously, by applying the following pattern:</li></ul><pre>&lt;link rel=&quot;preload&quot; href=&quot;styles.css&quot; as=&quot;style&quot; onload=&quot;this.onload=null;this.rel=&#39;stylesheet&#39;&quot;&gt;<br>&lt;noscript&gt;&lt;link rel=&quot;stylesheet&quot; href=&quot;styles.css&quot;&gt;&lt;/noscript&gt;</pre><ul><li>link rel=&quot;preload&quot; as=&quot;style&quot; requests the stylesheet asynchronously. You can learn more about preload in the Preload critical assets guide.</li><li>The onload attribute in the link allows the CSS to be processed when it finishes loading.</li><li>“nulling” the onload handler once it is used helps some browsers avoid re-calling the handler upon switching the rel attribute.</li><li>The reference to the stylesheet inside of a noscript element works as a fallback for browsers that don&#39;t execute JavaScript.</li></ul><p>Now you can check the performance again.</p><p>The FCP marker appears before the page requests the CSS, which means the browser doesn’t need to wait for the CSS to load before rendering the page:</p><p><strong>Minify CSS:</strong></p><p>CSS files can contain unnecessary characters, such as comments, whitespaces, and indentation. In production, these characters can be safely removed, to reduce file size without affecting how the browser processes the styles. This technique is called minification.</p><p>The minification can be done with the help of webpack</p><p>By default, the resulting JS bundle that webpack produces would contain the content of the CSS files inlined. Since we want to maintain separate CSS files, we are using two complementary plugins:</p><ul><li><a href="https://github.com/webpack-contrib/mini-css-extract-plugin">mini-css-extract-plugin</a> will extract each style sheet into its own file, as one of the steps of the build process.</li><li><a href="https://github.com/fqborges/webpack-fix-style-only-entries">webpack-fix-style-only-entries</a> is used to correct an issue in wepback 4, to avoid generating an extra JS file for each CSS file listed in webpack-config.js.</li></ul><p>You need to make changes in your webpack file.</p><p>open the webpack.config.js file, and make the following modifications:</p><pre>const OptimizeCSSAssetsPlugin = require(&quot;optimize-css-assets-webpack-plugin&quot;);</pre><p>Then, pass an instance of the plugin to the plugins array:</p><pre>plugins: [<br>    new HtmlWebpackPlugin({template: &quot;./src/index.html&quot;}),<br>    new MiniCssExtractPlugin({filename: &quot;[name].css&quot;}),<br>    new FixStyleOnlyEntriesPlugin(),<br>    new OptimizeCSSAssetsPlugin({})<br>  ]</pre><p>by adding the following changes you can check the css file size in your network tab. Check the file size before making changes in web-pack and check size after making changes to your webpack file.</p><p>You can see there will be around some percent of reduction in the file size of the Css file.</p><p>For more post do let me know in the comment section :)</p><p>Github Id-mitulshah96<br>Twitter Id- @MitulJShah</p><p>Happy to serve you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1aa9529fb116" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>