How can we optimize the crucial Rendering Path in WordPress

Apr 20, 2022
critical rendering path

The Critical Rendering Path describes the set of actions that the browser does to render a page on the screen i.e. download, process, and convert HTML, CSS, and JavaScript code into pixels, and then paint them on the screen.

The crucial Rendering Path Optimization is the process of minimizing the time spent by the browser to perform each step of the sequence , and prioritizing presentation of the content that is related to the present user's action.

A large portion of this procedure relates to the part of the webpage that is viewable with no scrolling in the window of your browser. This section is also referred to as Above the Fold. To make it easier to use The ATF is required to be rendered as soon as possible, and this can be done by reducing the number of network round-trips to a minimal. The components required for rendering the ATF are considered to be critical making sure that you optimize the Above the Fold means minimizing the effect of these resources on the initial rendering time of the page.

In this article, we will walk through the Critical Rendering Path optimization sequence.

  • Then, I'll give an overview of the browser's tasks to render a page's content.
  • Then, I'll break down the best steps we can take to improve the critical Rendering Path.
  • In the final section, I'll list some useful (and widely used) WordPress optimization plugins.

The Critical Rendering Path Sequence

Here is the sequence of actions performed by the browser when rendering an internet page:

  • The browser first downloads and interprets the HTML markup, then builds the DOM
  • Then it downloads and process the CSS markup, and then constructs the CSS Object Model
  • It is a combination of DOM as well as CSSOM to allow for rendering the page. the Render Tree, which is the tree-like structure that includes the visible nodes
  • It calculates the dimensions and the position of every object on the page
  • Then the screen is painted with pixels the screen

The DOM

As well explained in the Google's Critical Rendering Path Optimization manual The browser creates the Document Object Model in a four-step procedure:

  • The browser first reads rows of bytes, then converts the row bytes into distinct characters.
  • It then converts the strings of characters that are enclosed in angle brackets into tokens.
  • The tokens converted to node objects.
  • Node objects are linked in an inverse tree structure that contains HTML information, properties as well as relationships between nodes. The structure used is called the Document Object Model.

Important to know here is that the browser creates the DOM incrementally. This allows us to speed up the rendering of the webpage by creating effective DOM structures.

DOM structure diagram
Structure of the DOM

The CSSOM

When the parser is confronted with an link tag that is referring to an external CSS stylesheet, it ceases the parsing and sends out an request to the resource. When the CSS stylesheet has been accepted, the browser begins building a tree data structure of CSS nodes.

  • The browser is able to read the row bytes from the .css file and converts these into individual characters
  • It converts strings of characters enclosed within curly brackets to tokens
  • These tokens are converted into node objects.
  • Node objects are connected in the form of a tree-like data structure which includes the CSS properties of each node, as well as relationships between nodes. The structure that is used for this is known as known as the CSS object model (CSO). Model( CSSOM).

Contrary to DOM construction, CSSOM construction is not an incremental. The browser can't make use of only a small section of a stylesheet as styles can be refined or redeclared within the same stylesheet. Because of this, the browser blocks the rendering process until it has received and reads the entire CSS. That means CSS blocks rendering.

CSSOM structure diagram
CSSOM structure.

The Render Tree

The browser blends DOM with CSSOM in it's Render Tree, the final tree structure containing the properties and nodes used to render the page on the screen.

The Render Tree is comprised of only nodes that are required to render a page. As a consequence, invisible nodes are not included.

The browser makes use of it's Render Tree to calculate node dimensions and positions as an input for the painting process.

Render Tree structure diagram
Render Tree Structure.

Paint and Layout

At the stage of layout, the browser calculates the dimensions and location of each node on the Render Tree. The browser scans all the Render Tree starting from its base and creates the model of a box at the process. This model is later converted by every single node of the Render Tree into actual pixels that appear on the screen.

Optimizing Critical Rendering Paths

The duration required for the entire process can be different. This is based on the document's size, the number of requests, the styles that are applied, the user device and so on.
 The most pertinent Google guidelines is prioritizing visible content in order to create The Above the Fold as quick as you can. Google also offers two primary rules to adhere to:

  • Then, you must structure the HTML to load the vital, above-the-fold content first
  • Reducing the amount of information utilized by HTML, CSS, and JS resources
Examples of above the Fold content on mobile, tablet and desktop devices
The Above the Fold content differs based on the user device

However, optimization doesn't end by the creation of an effective DOM structure. It's more of a method of improvement and measurement which encompasses the entire Critical Rendering Path sequence.

Let's dive deep.

Reduce the Dimensions of Resource

We can cut down on the amount of data a browser is going to download by minifying, compressing and caching HTML, CSS, and JavaScript Resources:

  • Minification is the process to remove unnecessary characters, such as comments and white space from the source code. They are essential to developing, however, they're ineffective for rendering the page.
  • Compression can be described as the capacity of clients and web servers to decrease the size of transmitted files for increased speed and bandwidth utilization
  • Caching: every browser is equipped with the installation that makes use of an HTTP cache. The only thing we have to take care of is ensuring that each server response includes the appropriate HTTP headers that instruct the browser on what time it should cache the requested resource.

Optimize CSS

CSS is able to be adapted for specific situations as well as we can improve it using media types and media queries. If you're looking at a web page on the screen, the browser will send a request for print media type, but it will not block rendering of the site for this resource.
 Click on the link tag:

The stylesheet that is referenced in this tag is applicable under any condition, independently from the current media type and resolution of screen, orientation or orientation. This means that the CSS resource is always render-blocking.

Luckily, we can send a request for a CSS resource under specific conditions. Print styles can be moved into a separate file and then use an attribute called media attribute to notify the browser that the requested style sheet will only be loaded when printing the page It doesn't have to hinder the rendering of the screen.

The browser downloads it's print.css stylesheet, however, it will not block rendering. Furthermore, it has to download less data for the main CSS file, which will help us speed up the downloading. You can also specify any media query on the link attribute, so we can break the CSS into multiple files and load them with a precondition:


 
 

Make sure that your style is needed to render the webpage. Add the appropriate value to the media tag attribute and then unblock rendering if not.

Media types and queries can assist in speeding up web page's rendering speed, however there is a lot more.

  • Minify CSS whitespace and comments only help us understand CSS declarations. When we remove comments as well as whitespace in the stylesheet it is possible to drastically reduce the amount of bytes contained in a CSS file.
  • Inline critical CSS Some styles are critical due to the fact that they must render the top-of-the-fold page. It is recommended to consider inline critical styles directly into the HTML markup to avoid additional HTTP requests. But avoid inlining large CSS documents as it could need additional round trips to render the above-the-fold, resulting in a PageSpeed warning.

The Speed-Up Layout Process and Paint Processes

The time spent by the browser in laying out the page is contingent on the amount of DOM elements needed to layout the document and also the difficulty of the layouts.

  • If there are a number of DOM elements, your browser may take a lengthy period of time to compute the positions and the dimensions of all of them Beware of layout when you can.
  • Prefer the new Flexbox model, as it's much faster than Flexbox as well as floating layouts.
  • Do not use forced synchronous layouts when using JavaScript.

Computing element size and position takes time and reduces efficiency. Make the DOM as straightforward as it is and eliminating the use of JavaScript to anticipate the layout process will help the browser speed up the page rendering ( read more on optimization of layout).

Strictly connected to the Layout will be the Paint procedure. It could be the slowest step in the Critical Rendering Path sequence. Anytime you change the arrangement of an object or other non-geometric properties it creates a paint event. It is important to keep things as straightforward as is possible in this phase will help the browser accelerate the process of painting. As an example, a box shadow property that requires computations, will take more time to paint than a simple border color.

Chrome DevTools allow to identify the portions of the page that are being painted
Chrome DevTools can identify specific areas of the page that are being painted.

Making the process easier to paint may not be that easy, and you should make use of your browser's tools for developers to determine how long it takes for the browser to trigger each paint trigger. Learn more about this topic in Google's Rendering Performance guide.

Want to know the ways we have increased traffic over 1000%?

Join 20,000+ others who receive our newsletter every week with insider WordPress tips!

Use JavaScript unblocking

If the browser comes across script-related tags that is a script tag, it must cease parsing the HTML code. Inline scripts are executed exactly at the point of the page and block DOM construction till the JS engine has finished. Also, inline JavaScript could significantly slow down rendering the initial webpage. But JavaScript also allows to manipulate CSS properties. Therefore, the browser must stop its script's execution until it's completed downloading and building the CSSOM. That means JavaScript blocks parsers.

If you are using external JS files, the parser must also wait until the resource is retrieved from the cache or remote server which can significantly reduce the speed of initial page rendering.
 So, how is the best way to cut down the time spent by the browser loading and run JavaScript?

  • Load JavaScript in asynchronous fashion using the variable async attribute in the script tag directs the browser to execute the script asynchronously, if feasible, but without stopping the DOM construction. The browser makes an HTTP request for the script to continue parsing the DOM. The script also does not block the CSSOM construction which means that it doesn't block crucial rendering Path (see MDN docs for additional information regarding script tag attributes)
  • Defer JavaScript: The variable defer attribute in the script tag instructs the browser to execute the script after parsing the document, however before activating the DOMContentLoaded event. This attribute cannot be used in the event that the src attribute is absent, i.e. scripts inline (read more on Mozilla Hacks)

Wrapping Up Optimization Rules

There's plenty to do to think about, isn't it? Take a deep breath and write down a list of optimization steps that have been described in the past.

  • Minimize, compress or cache HTML CSS in addition to JavaScript resources.
  • Reduce the use of rendering blockers (specifically CSS)
  • Use media queries on link tags
  • Inline stylesheets, split stylesheets and split CSS
  • Combine several CSS files
  • Reduce the use of blocking parser resources (JavaScript)
  • Use defer attribute to the script tags
  • Make use of the the async attribute in the script tags
  • Inline JavaScript and move script tags towards the bottom of the document

Once we've mastered the fundamental concepts behind Critical Rendering Path Optimization, we can examine some WordPress famous optimization plugins.

Optimizing the critical Rendering Path in WordPress

WordPress users can take advantage of a variety of plugins covering all aspects of optimization. You can install a fully-featured plugin or set up several plugins at once with each one offering specific optimization features.

W3 Total Cache

The plugin is able to cover all stages in the Critical Rendering Path optimization process. At first glance, the plugin configuration can be confusing. Still, once you become more acquainted to the Critical Rendering Path sequence, you'll be able to make use of an effective performance toolset.

W3 Total Cache WordPress plugin
W3 Cache Total Cache WordPress plugin

Here's a brief list of the plugin's features:

  • HTML (posts and pages), CSS and JavaScript caching in memory, either on the disk or in CDN
  • Caching of feeds, search results Database objects WordPress objects and fragments
  • HTML (posts and pages and posts) miniaturization
  • JavaScript and CSS minification
  • JavaScript optimization with async and defer
  • Browser caching using cache-control and future expire headers and tag tags for entities
  • Google PageSpeed Results on WordPress Dashboard
W3 Total Cache JavaScript minify settings
W3 Total Cache JavaScript minify settings

WP Super Cache

WP Super Cache is another well-known plugin to improve the performance of your site.

WP Super Cache WordPress plugin
WP Super Cache WordPress plugin

It comes with many optimization features, but it's not as extensive as the W3 Total Cache and might be less accessible to intermediate and novice users.

WordPress Super Cache tester
WordPress Super Cache test

It basically provides the ability to cache data as well as HTTP compression, but lacks resource minification as well as JavaScript optimization, which includes the async and delay attributes. Yet, over one million installations active show that the plugin is worth a try.

GZIP option in WP Super Cache
GZIP option in WP Super Cache

Autoptimize

With more than one million active installations, Autoptimize is one of the most well-known free plugins for minification.

Autoptimize WordPress plugin
Autoptimize WordPress plugin

It comes with an options section that is divided into sections that allow the administrator of the website is able to set the individual settings for HTML, CSS, and JavaScript minification.

Autoptimize HTML optimization option in Autoptimize settings
Autoptimize HTML optimization feature

Over the Fold Optimization

This plug-in offers a full set of tools to help with Above the Fold optimization. It's a tool for professionals and advanced users to score 100/100 on the Google PageSpeed test.

Above the Fold Optimization WordPress plugin
The Above-the-Fold Optimization WordPress plugin

Here are a few of the most interesting characteristics:

Important CSS tools:

  • Conditional CSS loading
  • Critical CSS management via Text editor
  • Gulp.js critical CSS creator
  • Quality test for CSS critical

CSS load optimization:

  • Asynchronous loading CSS
  • CSS extraction from HTML
  • External stylesheet caching

Optimization of load in JS:

  • Asynchronous JS loading
  • localStorage cache
  • Lazy loading JavaScript
  • External script cache

Additionally, it provides support for google's Progressive Web App as well as Google Web Font optimization. Some other optimization plugins that you might be interested in:

Swift Performance

Swift Performance WordPress plugin
Swift Performance WordPress plugin

The main characteristics include:

  • Not only can you merge and reduce CSS as well as Javascript files you can also create critical CSS in real time for your webpages.
  • Intelligent caching, along with AJAX and dynamic request.
  • Built-in lossless image compression.
  • CDN Support for CDN.

Conclusions

Here on Blog, we attempt to address every aspects of optimizing performance. Below is a list of further readings:

How long will it take you to improve the critical Rendering Path of your websites? What elements of optimizing are most difficult for you to be able to master? Let us know in the comments section below.

Reduce time, money and increase site performance:

  • 24/7 help and support from WordPress hosting experts, 24/7.
  • Cloudflare Enterprise integration.
  • Reaching a global audience with 29 data centers across the globe.
  • Optimization using the integrated Application to monitor performance.