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
WIP: Removing render-blocking javascript #2300
Conversation
Awesome. I think we tried this and GTM barfed, but maybe something has been updated there to make it work? |
I think the way we consistently got the problem is testing on slow devices. If you test in Chrome Developer tools with 2G speeds, then GA would load before our JS and cause errors. |
Yeah, per @jimmynotjim's comment, IIRC we moved jquery out of the head (and removed it entirely on pages that didn't use it) and it broke the GTM code that assumes jquery is in the head when it executes. The larger problem that would be great to address is that the GTM code should be moved to version control (maybe even this repo). We essentially have JavaScript introduced by GTM that circumvents all our code review process. |
@KimberlyMunoz Good to know, I'll look into that. @anselmbradford AFAIK GTM is not conducive to external version control, it would involve a lot of copying and pasting. It has its own versioning system that keeps a snapshot of every version of our GTM container. Here's a dump of all the GTM snippets for the curious: |
Chuck looked into injecting scripts directly into the object GTM adds to the page, potentially making it so the code could live here and not in GTM @wernerc where was the thread where you discussed that? |
I think this explains it in detail: |
How about this:
This ensures that jQuery is loaded before loading GTM and adds the async tag to the jQuery scripts. This might break V1 scripts in some cases because we are still dependent upon jQuery is some places. Hopefully that will change soon. |
These issues with GTM looking for jQuery on page load should be resolved (GTM should no longer barf on page load). There might be some specific pages where this still occurs, but I'm not currently aware of any (I think?). If this issue comes up again, or if someone sees a console error coming from GTM, please let me know! |
Thanks for the feedback, everyone! Super helpful. After chatting with folks, reading through the previous threads, and testing several script variants, I'm going to pause work on this for now. The odds of introducing a breaking change are high and the performance benefit doesn't justify the risk. We can reapproach it after the CF atomic stuff has wrapped up. Or if someone smarter than I wants to take a stab at it, please do. |
One of cf.gov's biggest performance faux pas is loading jquery in the
<head>
. After exploring all sorts of ways to mitigate this (including exporting all the GTM snippets in an attempt to rewrite them all without jQuery), I finally tried just removing it from the head and added it to the common.js bundle.And, surprisingly, it seems to work fine. I'm still in the process of testing this across all devices but so far everything appears to be working as expected.
Is this a horrible idea @sebworks @virginiacc? Have I overlooked something?