Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: adding async to Analytics #221

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

yuriassuncx
Copy link

@yuriassuncx yuriassuncx commented Sep 5, 2023

PR Description

This Pull Request aims to make the analytics script asynchronous and add the async attribute to the corresponding <script> element in the Analytics component. These changes are intended to improve website performance by ensuring that the analytics script is loaded asynchronously, preventing rendering delays.

Motivation

The motivation behind these changes is to ensure that the analytics script is loaded asynchronously, allowing the page to continue rendering without significant delays caused by script loading. Additionally, making this script asynchronous can enhance the user experience, especially on pages with numerous scripts and resources.

Tests Conducted

Local tests have been conducted to verify that the analytics script is loaded asynchronously and that other scripts on the page are not affected by the async attribute.

Impact on PageSpeed

We have encountered a significant drop in PageSpeed on the Abracadabra website with the insertion of GTM (Google Tag Manager). Therefore, the primary goal of these changes is to mitigate this impact and reduce Total Blocking Time (TBT) to enhance the overall site performance.

@tlgimenes
Copy link
Contributor

Why defer and not async?

@yuriassuncx
Copy link
Author

Why defer and not async?

in fact, the real intention is to postpone the loading of this script as much as possible, so that it does not load the first time. Do you believe that asynchronous can be the best choice? if yes, we can do that.

@tlgimenes
Copy link
Contributor

Why defer and not async?

in fact, the real intention is to postpone the loading of this script as much as possible, so that it does not load the first time. Do you believe that asynchronous can be the best choice? if yes, we can do that.

I think so. defer will hang the main thread and synchronously wait for this script to run before the load event. Async will allow the browser to do other things before and not hang the main thread

@yuriassuncx
Copy link
Author

Why defer and not async?

in fact, the real intention is to postpone the loading of this script as much as possible, so that it does not load the first time. Do you believe that asynchronous can be the best choice? if yes, we can do that.

I think so. defer will hang the main thread and synchronously wait for this script to run before the load event. Async will allow the browser to do other things before and not hang the main thread

done!

@yuriassuncx yuriassuncx changed the title perf: adding defer to Analytics perf: adding async to Analytics Sep 6, 2023
@drawveloper
Copy link
Contributor

cc @tlgimenes

@tlgimenes
Copy link
Contributor

tlgimenes commented Sep 21, 2023

In between my reviews, we found a performance improvement by adding type="module". The whole idea is to allow the parser to parse the whole document before executing anything. This makes paint/reflows/layout to happen once, decreasing TBT. The differences between async/defer/type=module can be seen on the graph below

So I think it's kind of already working like the PR description intended to:

The motivation behind these changes is to ensure that the analytics script is loaded asynchronously, allowing the page to continue rendering without significant delays caused by script loading

I don't know, should we proceed with this PR? It feels like adding async would increase TBT and we are already deferring script execution afterwards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants