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

Improve request parallelization, particularly on Chrome #1201

Merged
merged 1 commit into from
Jul 18, 2023

Conversation

ChristopherChudzicki
Copy link
Contributor

@ChristopherChudzicki ChristopherChudzicki commented Jul 14, 2023

What are the relevant tickets?

Related to #1196

Description (What does it do?)

This PR moves Google Tag Manager script to the top of <head />, as recommended:

Place the <script> code snippet in the of your web page's HTML output, preferably as close to the opening tag as possible, but below any dataLayer declarations.

Why? The goal is to better parallelize network requests, particularly in Chrome. Previously, in Chrome, many requests were being queued after some CSS. Similar queueing seemed to occur in firefox, though to a much lesser extent.

Screenshot 2023-07-14 at 4 33 05 PM

Notes:

  • Despite research and trying to reproduce with very minimal examples, I don't have a great understanding of why moving the GTM script above the <link /> has this effect. But since putting GTM at the top is recommended anyway, I feel good about this change.

How can this be tested?

Warning
This behavior is hard to observe locally:

  • the assets need to be served via http2, which will only happen if using https and a server configured for it.
  • the hugo build environment needs to have GTM_ACCOUNT_ID set.

Consequently, you can observe the behavior easily in Prod or on RC, but not on our default PR builds deployed with netlify (no GTM_ACCOUNT_ID) and not easily on localhost (http1.1, which is limited to 6 concurrent connections per host).

To help test this change, I've made two separate PRs just for the netlify deployments:

  1. https://ocw-hugo-themes-course-v2-pr-1202--ocw-next.netlify.app/ is exactly the main branch netlify deployment, but with GTM_ACCOUNT_ID set to the RC value.
  2. https://ocw-hugo-themes-course-v2-pr-1200--ocw-next.netlify.app/ is exactly the cc/move-gtm branch netlify deployment, but with GTM_ACCOUNT_ID set to the RC value.

To observe old blocking behavior:

  1. Use Chrome in Incognito mode.
  2. In the dev console network tab, disable caching and set throttling to Fast 3G. Effect is easier to see on slow networks.
  3. In performance tab, start recording.
  4. Visit https://ocw-hugo-themes-course-v2-pr-1202--ocw-next.netlify.app/
  5. Stop recording.
  6. View the "Network" panel of performance tab.
  7. Observe the blocking behavior shown above: requests do not start (only queued) until after the first CSS request finishes.

To observe new behavior: Repeat steps above, but use https://ocw-hugo-themes-course-v2-pr-1202--ocw-next.netlify.app/

Additional Tests:

  • repeat in firefox if you like. There's an improvement there, too, but it's not as dramatic.
  • assure yourself that performance hasn't gotten worse. It should be noticeably better, particularly on slow connections.

Additional Notes

Because Lighthouse and PageSpeed Insights both use Chromium, I expect this to help or Core Web Vitals scores:

  • First Contentful Paint (FCP) should benefit from faster loading
  • Largest Contentful Paint (LCP) should benefit from faster loading
  • Cumulative Layout Shift (LCS) benefits indirectly: The page won't render untill CSS has been downloaded. That happens just as quickly as before. But now images may have downloaded before CSS finishes, so we may get less content shift.

Lighthouse performance scores are variable, but they do seem better on 1200 link than on 1202` link. Try https://pagespeed.web.dev/ if you're interested.

@github-actions github-actions bot temporarily deployed to pull request July 14, 2023 18:16 Inactive
@github-actions github-actions bot temporarily deployed to pull request July 14, 2023 18:16 Inactive
@github-actions
Copy link

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ChristopherChudzicki ChristopherChudzicki changed the title move gtm script to very top Improve request parallelization, particularly on Chrome Jul 14, 2023
@gumaerc gumaerc self-assigned this Jul 18, 2023
Copy link
Collaborator

@gumaerc gumaerc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I wasn't able to properly test the PR with the URLs provided. I saw the same blocking behavior on both Netlify URLs, so I deployed this PR to my private web server and tested it there building it myself. I was able to verify that this works as intended and the blocking behavior is gone:

image

@ChristopherChudzicki
Copy link
Contributor Author

@gumaerc tried again and saw the intended behavior. We suspect using wrong urls before. Should be:

@ChristopherChudzicki ChristopherChudzicki merged commit 3a51a24 into main Jul 18, 2023
@gumaerc
Copy link
Collaborator

gumaerc commented Jul 18, 2023

results on my web server after enabling http2:

image

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

Successfully merging this pull request may close these issues.

2 participants