{"id":7240,"date":"2024-11-09T19:15:08","date_gmt":"2024-11-09T19:15:08","guid":{"rendered":"https:\/\/impulsebyte.com\/blogs\/?p=7240"},"modified":"2024-11-09T19:20:19","modified_gmt":"2024-11-09T19:20:19","slug":"asynchronous-loading-of-third-party-scripts-with-barrel-asyncload","status":"publish","type":"post","link":"https:\/\/impulsebyte.com\/blogs\/shopify\/asynchronous-loading-of-third-party-scripts-with-barrel-asyncload\/","title":{"rendered":"Asynchronous Loading of Third-Party Scripts with BARREL.asyncLoad"},"content":{"rendered":"<p>When building a website, performance is key. Loading too many third-party scripts can slow down your website, negatively affecting both user experience and SEO. One way to optimize your website&#8217;s performance is by loading third-party scripts asynchronously. In this post, we&#8217;ll explore how to use a custom JavaScript function called <code>BARREL.asyncLoad<\/code> to achieve this.<\/p>\n<h3>What is Asynchronous Loading?<\/h3>\n<p>Asynchronous loading means that your browser can continue loading other parts of your website while waiting for scripts to load. This prevents scripts from blocking your page&#8217;s content from rendering, which can significantly improve page speed.<\/p>\n<h3>Introduction to <code>BARREL.asyncLoad<\/code><\/h3>\n<p>The <code>BARREL.asyncLoad<\/code> function is a custom JavaScript utility that allows you to load multiple external scripts asynchronously. This function is particularly useful if you want to include several third-party resources on your site without impacting its performance.<\/p>\n<p>Here&#8217;s the complete code for <code>BARREL.asyncLoad<\/code>:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\r\nwindow.BARREL = window.BARREL || {};\r\n\r\nwindow.BARREL.asyncLoad = function (urls) {\r\nfor (var i = 0; i &lt; urls.length; i++) {\r\nvar s = document.createElement('script');\r\ns.type = 'text\/javascript';\r\ns.defer = true; \/\/ Load script after HTML parsing is complete\r\ns.src = urls&#x5B;i].src; \/\/ URL of the script to load\r\n\r\n\/\/ Optional: If a callback function is provided, execute it after the script loads\r\nif (typeof urls&#x5B;i].callback === 'function') {\r\ns.addEventListener('load', urls&#x5B;i].callback);\r\n}\r\n\r\n\/\/ Append the script to the page\r\nvar x = document.getElementsByTagName('script')&#x5B;0];\r\nx.parentNode.insertBefore(s, x);\r\n}\r\n};\r\n<\/pre>\n<h3>How to Use <code>BARREL.asyncLoad<\/code><\/h3>\n<p>You can use the <code>BARREL.asyncLoad<\/code> function to load multiple scripts by passing an array of objects. Each object should contain the script <code>src<\/code> URL, and optionally, a <code>callback<\/code> function that runs after the script has loaded.<\/p>\n<h4>Example Usage:<\/h4>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;script&gt;\r\nwindow.addEventListener('scroll', function () {\r\nsetTimeout(function () {\r\nwindow.BARREL.asyncLoad(&#x5B;\r\n{\r\nsrc: 'https:\/\/apis.google.com\/js\/platform.js?onload=renderOptIn'\r\n},\r\n{\r\nsrc: 'https:\/\/cdn.example.com\/library.js',\r\ncallback: function () {\r\nconsole.log('Library.js loaded successfully!');\r\n}\r\n},\r\n{\r\nsrc: 'https:\/\/analytics.example.com\/tracker.js'\r\n}\r\n]);\r\n}, 1000); \/\/ Delay loading by 1 second\r\n}, { once: true });\r\n&lt;\/script&gt;\r\n<\/pre>\n<h3>Explanation<\/h3>\n<ul>\n<li><strong>Scroll Event Listener<\/strong>: In this example, the scripts are loaded only after the user starts scrolling. This can further optimize your page load by deferring non-critical scripts until user interaction.<\/li>\n<li><strong>setTimeout<\/strong>: We add a slight delay (1000ms) before loading the scripts. This can help improve your page&#8217;s initial load speed.<\/li>\n<li><strong>Script Loading<\/strong>:\n<ul>\n<li>The <code>src<\/code> property specifies the URL of the script to be loaded.<\/li>\n<li>If a <code>callback<\/code> function is provided, it will execute after the script loads.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>Use Cases<\/h3>\n<ol>\n<li><strong>Loading Analytics Scripts<\/strong>: Defer loading analytics or tracking scripts until after the initial page load to reduce render-blocking.<\/li>\n<li><strong>Third-Party Widgets<\/strong>: If you&#8217;re using chat widgets or customer support tools, load them only after the user interacts with your page.<\/li>\n<li><strong>Performance Optimization<\/strong>: Great for lazy-loading scripts that are not essential for the initial rendering of your website.<\/li>\n<\/ol>\n<h3>Advantages of Using <code>BARREL.asyncLoad<\/code><\/h3>\n<ul>\n<li><strong>Improved Page Load Time<\/strong>: By loading scripts asynchronously, you ensure they don&#8217;t block the rendering of your page.<\/li>\n<li><strong>Reduced Render-Blocking<\/strong>: Deferred scripts do not block HTML parsing, improving your website&#8217;s speed score.<\/li>\n<li><strong>Better User Experience<\/strong>: Faster load times lead to a better user experience and potentially higher conversion rates.<\/li>\n<\/ul>\n<h3>Best Practices<\/h3>\n<ul>\n<li><strong>Prioritize Critical Resources<\/strong>: Use this method only for non-critical scripts. Load essential scripts like core libraries (e.g., jQuery) using standard <code>&lt;script&gt;<\/code> tags with the <code>defer<\/code> attribute.<\/li>\n<li><strong>Test Thoroughly<\/strong>: Ensure that any deferred scripts do not interfere with the functionality of your website.<\/li>\n<li><strong>Monitor Performance<\/strong>: Use tools like Google Lighthouse, GTmetrix, or PageSpeed Insights to monitor the impact of async loading on your website&#8217;s performance.<\/li>\n<\/ul>\n<h3>Conclusion<\/h3>\n<p>Asynchronous loading of third-party scripts is a simple yet effective way to optimize your website&#8217;s performance. By using the <code>BARREL.asyncLoad<\/code> function, you can control when and how external resources are loaded, helping you build a faster and more responsive website.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When building a website, performance is key. Loading too many third-party scripts can slow down your website, negatively affecting both user experience and SEO. One way to optimize your website&#8217;s &hellip; <a title=\"Asynchronous Loading of Third-Party Scripts with BARREL.asyncLoad\" class=\"hm-read-more\" href=\"https:\/\/impulsebyte.com\/blogs\/shopify\/asynchronous-loading-of-third-party-scripts-with-barrel-asyncload\/\"><span class=\"screen-reader-text\">Asynchronous Loading of Third-Party Scripts with BARREL.asyncLoad<\/span>Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":7243,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,114],"tags":[],"class_list":["post-7240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-shopify","category-pageload-speed"],"_links":{"self":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/comments?post=7240"}],"version-history":[{"count":2,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7240\/revisions"}],"predecessor-version":[{"id":7242,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7240\/revisions\/7242"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/media\/7243"}],"wp:attachment":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/media?parent=7240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/categories?post=7240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/tags?post=7240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}