<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Oleh Mertviacheko</title>
    <description>The latest articles on DEV Community by Oleh Mertviacheko (@osxisl).</description>
    <link>https://dev.to/osxisl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F433314%2Fcc9b4aa4-f9ab-4a8f-9678-eadd49063968.jpeg</url>
      <title>DEV Community: Oleh Mertviacheko</title>
      <link>https://dev.to/osxisl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/osxisl"/>
    <language>en</language>
    <item>
      <title>Lazy load any charts and ads with lazysizes.js</title>
      <dc:creator>Oleh Mertviacheko</dc:creator>
      <pubDate>Sun, 04 Oct 2020 12:18:01 +0000</pubDate>
      <link>https://dev.to/osxisl/lazy-load-any-charts-and-ads-with-lazysizes-js-4obo</link>
      <guid>https://dev.to/osxisl/lazy-load-any-charts-and-ads-with-lazysizes-js-4obo</guid>
      <description>&lt;p&gt;After adding emoji trends charts on  &lt;a href="https://emojigraph.org/chart-decreasing/"&gt;emojigraph.org/chart-decreasing/&lt;/a&gt;, I faced a significant drop 📉 in page loading speed on mobile devices. And the same problem I had with ads.&lt;/p&gt;

&lt;p&gt;I’ve already used  &lt;a href="https://github.com/aFarkas/lazysizes"&gt;&lt;strong&gt;&lt;em&gt;lazysizes.js&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt;  for lazy loading images, so I started to search for a way to use the same library to lazy load charts and ads.&lt;/p&gt;

&lt;p&gt;Hopefully, there is a  &lt;strong&gt;lazybeforeunveil&lt;/strong&gt;  event fired on each lazyload element right before the “unveil” transformation.&lt;/p&gt;

&lt;p&gt;First, add your chart library dns-prefetch in  &lt;code&gt;&amp;lt;head&amp;gt;…&amp;lt;/head&amp;gt;&lt;/code&gt;  section, so you won’t waste time loading the library but will be ready to load a bit faster once you need it:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;link rel=”preconnect dns-prefetch” href="https://cdn.jsdelivr.net/npm/apexcharts@3.19.3/dist/apexcharts.min.js" crossorigin=”anonymous”&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Then add the&lt;code&gt;lazyload&lt;/code&gt;  class to the div containing your chart.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="tracker_chart" class="chart lazyload" data-expand="-150" style="height: 510px;"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;You can also set  &lt;code&gt;data-expand&lt;/code&gt;  attribute to set in pixels when to start to lazy preload images/iframes. It could be set to a negative value, I use&lt;code&gt;data-expand="-150"&lt;/code&gt;  to load chart only after 150px of my chart placeholder are in the viewport area.&lt;/p&gt;

&lt;p&gt;I’m using an URL-encoded SVG to make a nice looking chart placeholder.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="chart_brush" style="  
    height: 510px; width: 100%;   
    background-image: url('data:image/svg+xml;charset=utf8,%3Csvg xmlns="[http://www.w3.org/2000/svg](http://www.w3.org/2000/svg)" ...%3E ... %3C/svg%3E');  
    background-repeat: no-repeat;" &amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Add following js code before closing  &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt;  tag:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script type="text/javascript"&amp;gt;  
document.getElementById("tracker_chart").addEventListener("lazybeforeunveil", function() {  
    var scriptLibrary = document.createElement('script');  
    scriptLibrary.onload = function () {  
        var scriptChart = document.createElement('script');  
        scriptChart.src = '/js/chart.js';  
        document.body.appendChild(scriptChart);  
    };  
    scriptLibrary.src = 'https://cdn.jsdelivr.net/npm/apexcharts@3.19.3/dist/apexcharts.min.js';  
    document.head.appendChild(scriptLibrary);  
});  
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Here we add EventListener to your chart div, which loads your chart library and javascript to plot the chart after the event is fired.&lt;br&gt;&lt;br&gt;
Where  &lt;strong&gt;scriptLibrary.src&lt;/strong&gt;  — link to your library, and  &lt;strong&gt;scriptChart.src&lt;/strong&gt; — link to js plotting a chart that is dependent on that library.&lt;/p&gt;

&lt;p&gt;✅ And that’s all!&lt;/p&gt;

&lt;p&gt;To lazy load ads — we can use the same technique:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script type="text/javascript"&amp;gt;  
window.addEventListener('DOMContentLoaded', (event) =&amp;gt; {  
    if (window.innerWidth&amp;lt;992){  
       /*your functions for big screen*/  
document.getElementById("widget_ad_mobile").addEventListener("lazybeforeunveil", function() {  
        var scriptAds = document.createElement('script');  
        scriptAds.onload = function () {  
                (adsbygoogle = window.adsbygoogle || []).push({});  
            };  
        scriptAds.src = '[https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'](https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js');  
        document.getElementById("widget_ad_mobile").appendChild(scriptAds);  
        });  
    }  
});  
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;In my case I use lazy ads loading only on mobile devices (window.innerWidth&amp;lt;992) for widget that are way below the fold (“widget_ad_mobile”).&lt;/p&gt;

&lt;p&gt;After implementing chart and ads lazy load I got back to 90+ score and  &lt;em&gt;&amp;lt;3s&lt;/em&gt; load time on Google PageSpeed for mobile.&lt;/p&gt;

&lt;p&gt;If you have any questions, or suggestions for improvement — please let me know in the comments.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>charts</category>
      <category>lazyload</category>
    </item>
  </channel>
</rss>
