Learn how to use third-party scripts on your website without sacrificing performance.
The use of third-party scripts can have a significant impact on website performance. These scripts often operate in a blocking manner and can hinder page loading times. To reduce the time spent on these scripts, it is recommended to:
- Avoid using third-party scripts whenever possible
- Delay the loading of scripts until after user interaction
- Use async or defer attributes for scripts
- Preload connection to the script’s origin
<script src="script.js">
tag causes a delay in the execution
of DOM loading. This is because the browser will respect the order of the
scripts to download and execute them.
If the script itself does not manipulate the DOM, it is a strong candidate to be
delayed using async. An async script will be downloaded asynchronously and then
executed at the first opportunity before the DOM is loaded:
static
folder). If the script is hosted on another domain, the recommendation is to use
prefetch to speed up the resolution of the script’s server DNS:
important: use preconnect only when necessary and for scripts with a significant impact.