WEB (HTML + CSS)
WEB Rendering Blocking and How to Prevent Them
What is Rendering Blocking
- Render blocking resources are files that are essential to the process of rendering a web page. When the browser encounters a render blocking resource, it stops downloading the rest of the resources until these critical files are processed.
- Non-render blocking resources don’t postpone the rendering of the page and can be safely downloaded in the background after the initial page render.
Why Care About Rendering Blocking
- If you reduce the number of render blocking resources, you can shorten the critical rendering path and reduce page load times, thus improving the user experience and search engine optimization.
Types of Rendering Blocking Resources
- everything found in
<head>
section- CSS style-sheets
- JavaScript files added in
head
section - fonts added from either CDN or a local server
Non Rendering Blocking Resources
- everything placed at the bottom of body tag
- images
- media files
<script>
tags
<!doctype html>
<html lang="en">
<head>
<!-- blocking resource here -->
</head>
<body>
<h1>Hello, world!</h1>
<!-- non blocking resource here -->
</body>
</html>
Solving Render Blocking Resources
- Identify your render blocking resources
- This can be by using tools such as
- Don’t use CSS imports
- instead of using @import tool, which is blocking, consider adding your CSS using the traditional
link:stylesheet
in your html section
- instead of using @import tool, which is blocking, consider adding your CSS using the traditional
- Load conditional CSS with
media
attributes- i.e process CSS that apply for mobile only when the view-port matches mobile only.
<link href="large.css" rel="stylesheet" media="screen and (min-width: 1500px)">
- Tool to convert your existing CSS with media query to conditional CSS
- Defer non-critical CSS
- every CSS included in head section is blocking, however some CSS definition inside the file might not be critical during page load.
- we can split CSS files into critical and non-critical so critical can load in head section and non-critical load at the bottom of body tag as non-blocking resource
- You can split files using
- Use the
defer
andasync
attributes to eliminate render-blocking JavaScript- JavaScript placed in head section are blocking resource
- JavaScript placed at bottom of body are non-blocking,
- but sometimes will cause, some page components to lag and distorted view due to unloaded UI parts
- ex: ads, animations
- both defer and
async
are- ways to include JavaScript in head section as non-blocking
- executed in the order they appear in the HTML document, so if you have multiple scripts with defer, they will be executed in the order they appear.
- defer
<script src="example.js" defer></script>
- This attribute can be used with scripts that don’t need to be executed immediately when the page loads, but can be executed later when the page has finished loading.
async
<script async src="example.js"></script>
- When a script is marked as
async
, it will execute as soon as it is available, regardless of whether or not the page has finished parsing.
- Find and remove unused CSS and JavaScript
- Split code into smaller bundles
Minify
CSS and JavaScript files- Load custom fonts locally
- fonts found in head section are blocking resource
- prefer downloading and using fonts from local instead of directly from CDN
- Use this tool to help you download and self-host google fonts
- consider using
font-display: swap;
- to instruct the browser to immediately begin to use a system font and swap it with the custom font once it downloads
- This enables you to avoid invisible text on the page while the custom font is still loading.
- prefer to use
woff
andwoff2
font formats