question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running scripts injected by GTM without a url on main thread

See original GitHub issue

There is a script that is loaded using google tag manager that we would like to run on a main thread, while keeping other scripts working using partytown.

To load it a script is injected into the html that sets a config property on window and then creates a new <script> tag with a url. I’ve added the url of the script to loadScriptsOnMainThread and the script that is loaded from the url is working on the main thread, which is what we want, but the first script, directly injected by gtm, is still created with text/partytown.

So the problem is that for the second script with url to work, the first script needs to set config on window, which it sets in the web worker, and the script that is ran on the main thread does not have access to it.

So my question is if there is a way to run a single script (without a url) injected by gtm on the main thread?

The first script basically does this:

window.lib_config = {/* some config in here */};
var a = f.createElement("script");
a.async = !0;
a.type = "text/javascript";
a.src = /* lib url */;
f.head.appendChild(a);

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
michaelbournecommented, Nov 17, 2022

@teunissenstefan it appears to only work via URL, not ID like documented. I still haven’t found the cause of the error, typescript isn’t my forte.

It looks like this line is only checking the URL of scripts, not the ID: https://github.com/BuilderIO/partytown/blob/main/src/lib/web-worker/worker-script.ts#L30

Partytown.j grabs script elements with https://developer.mozilla.org/en-US/docs/Web/API/HTMLScriptElement and HTMLScriptElement inherits properties from Element, so id is available as a property.

It seems like we need to amend that check to something along the lines of this, but I’m not sure how a property from HTMLScriptElement gets sent to this function.

          const shouldExecuteScriptViaMainThread = config.loadScriptsOnMainThread.some(scriptIdentifier => 
            scriptIdentifier === url || scriptIdentifier === id
          )
0reactions
teunissenstefancommented, Oct 20, 2022

Ah understandable. Don’t worry about it. Thanks for letting me know!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Config to ignore specific 3rd party scripts injected by GTM and ...
While implementing PartyTown with many scripts injected by GTM we're finding some scripts to be currently incompatible which is blocking us ...
Read more >
GTMTips: Add A Load Listener To Script Elements - Simo Ahava
The first one is to use a Custom HTML tag, and then fire a dataLayer.push() once the resource has completely loaded. The second...
Read more >
Best practices for tags and tag managers - web.dev
Using a tag manager to inject a script is a very common use case. The recommended way to do this is to use...
Read more >
Pagespeed Insights reports that Google Analytics is blocking ...
I'm getting a thread blocking time for gtm.js around 4,680 ms. I have absolutely no idea what this is. When I load the...
Read more >
Google Tag Manager vs Page Speed: Impact & How to Improve
A comprehensive reports with tests on how does Google Tag Manager impact page speed + a bunch of tips on how to improve...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found