Make Your Site Faster with Async and Deferred JavaScript: Introducing script_loader_tag

Today I’m going to discuss a new, and very nice, site speed improvement that became possible in WordPress 4.1. What changed? The introduction of a new filter, script_loader_tag. This filter lets us easily change the HTML markup of enqueued script elements—that is, of JavaScript files that were correctly added into a WordPress site using WordPress’s wp_enqueue_script function.

With script_loader_tag, we can now easily fix a problem that can significantly impact page speed: lots of render-blocking JavaScript.

The Problem: Render-Blocking JavaScript

Long JavaScript files in your head can delay your browser from displaying page content, because its default behavior is first to interpret the JS files themselves.

Properly enqueued JavaScript shows up in the head section of your HTML document. On the internet as in nature, the main thing about a head is that it’s above a body—and this means something fairly serious for site speed, because JavaScript can be render-blocking.

“Render-blocking” comes from a web browser’s default behavior: It wants to completely receive and process everything that’s come higher up in the page, before it moves any further down.

This means that long JavaScript files in your head can actually delay your browser from displaying the page content in the body, because its default behavior is first to interpret the JS files themselves. In other words, JS is blocking the browser’s crucial function of rendering the page out for the user to actually see. The result can be slow sites and frustrated users.

.. The Fix: Defer and Async your JavaScript

The first thing to understand is the alternatives to render-blocking JS: defer and async. We’ll explain the difference, but both work similarly: They let the browser load a JS resource “as time permits,” while attending to other things (like page rendering) as well. This means that you can’t rely on a deferred or asynced JavaScript file being in place prior to page render, as you could without these attributes—but the advantage is that the file won’t slow the speed at which the page becomes visible to users.

Those are concepts—now for code. (The full code is available on GitHub.)

 

.. 2. Deferring and Asyncing Render-Blocking JavaScript

We found that we needed to use defer and not async for WPShout, so I’ll walk through the defer code. Most of the heavy lifting here is from an article by Scott Nelle; thanks, Scott!

<span class="token function">add_filter<span class="token punctuation">(</span></span> <span class="token string">'script_loader_tag'</span><span class="token punctuation">,</span> <span class="token string">'wsds_defer_scripts'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">3</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function">wsds_defer_scripts<span class="token punctuation">(</span></span> <span class="token variable">$tag</span><span class="token punctuation">,</span> <span class="token variable">$handle</span><span class="token punctuation">,</span> <span class="token variable">$src</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>

<span class="token comment" spellcheck="true">	// The handles of the enqueued scripts we want to defer
</span>	<span class="token variable">$defer_scripts</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span> 
		<span class="token string">'prismjs'</span><span class="token punctuation">,</span>
		<span class="token string">'admin-bar'</span><span class="token punctuation">,</span>
		<span class="token string">'et_monarch-ouibounce'</span><span class="token punctuation">,</span>
		<span class="token string">'et_monarch-custom-js'</span><span class="token punctuation">,</span>
		<span class="token string">'wpshout-js-cookie-demo'</span><span class="token punctuation">,</span>
		<span class="token string">'cookie'</span><span class="token punctuation">,</span>
		<span class="token string">'wpshout-no-broken-image'</span><span class="token punctuation">,</span>
		<span class="token string">'goodbye-captcha-public-script'</span><span class="token punctuation">,</span>
		<span class="token string">'devicepx'</span><span class="token punctuation">,</span>
		<span class="token string">'search-box-value'</span><span class="token punctuation">,</span>
		<span class="token string">'page-min-height'</span><span class="token punctuation">,</span>
		<span class="token string">'kamn-js-widget-easy-twitter-feed-widget'</span><span class="token punctuation">,</span>
		<span class="token string">'__ytprefs__'</span><span class="token punctuation">,</span>
		<span class="token string">'__ytprefsfitvids__'</span><span class="token punctuation">,</span>
		<span class="token string">'jquery-migrate'</span><span class="token punctuation">,</span>
		<span class="token string">'icegram'</span><span class="token punctuation">,</span>
		<span class="token string">'disqus'</span><span class="token punctuation">,</span>
	<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token function">in_array<span class="token punctuation">(</span></span> <span class="token variable">$handle</span><span class="token punctuation">,</span> <span class="token variable">$defer_scripts</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token string">'<script src="'</span> <span class="token punctuation">.</span> <span class="token variable">$src</span> <span class="token punctuation">.</span> <span class="token string">'" defer="defer" type="text/javascript"></script>'</span> <span class="token punctuation">.</span> <span class="token string">"\n"</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">return</span> <span class="token variable">$tag</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> 

The add_filter line tells us that this code should run anytime an enqueued JavaScript file is about to be printed onto the page as an HTML script element. Letting us filter that HTML is what script_loader_tag is for. (If you need an update on filters and WordPress’s hooks system in general, start here!)

 

GitHub project:

 

How To Fix Defer Parsing of Javascript in WordPress (5 Ways)

As somebody who is trying to understand how to defer parsing of Javascript, we believe you have a problem. You own or have visited a website of somebody you know (maybe a client?) and it takes forever to load. And when you ran it through website speed testing tools, you got a recommendation which you’re not sure how to implement.

In this article, we’re going to share several ways of how to fix this warning and ultimately, make the website load faster, without throwing this error!

If you’re short on time, here are some quick actions you can do:

Instructions for How To Defer Parsing of JavaScript

  1. Download the Async plugin here.
  2. Click on Plugins > Add New > Upload plugin and select the file you have just downloaded.
  3. Click on Activate of the installed plugin.
  4. Go to Plugins and click on Settings for the Async plugin you have just installed.
  5. Clicking on Enable Async Javascript, or Apply Async as two of the most common ways to apply the fix.
  6. Test your website, to see that everything is still working well.

 

More:

Getting Started with Sphinx (video)

Sphinx is a powerful documentation generator that has many great features for writing technical documentation including:

  • Generate web pages, printable PDFs, documents for e-readers (ePub), and more all from the same sources
  • You can use reStructuredText or Markdown to write documentation
  • An extensive system of cross-referencing code and documentation
  • Syntax highlighted code samples
  • A vibrant ecosystem of first and third-party extensions

Quick start video

This screencast will help you get started or you can read our guide below.

Flask: PyCharm Integration

Prior to PyCharm 2018.1, the Flask CLI features weren’t yet fully integrated into PyCharm. We have to do a few tweaks to get them working smoothly. These instructions should be similar for any other IDE you might want to use.

In PyCharm, with your project open, click on Run from the menu bar and go to Edit Configurations. You’ll be greeted by a screen similar to this: