vercel · GitHub

This PR extends loadScriptsOnMainThread functionality to inline scripts. This modification is required to address a common scenario in which an inline script sets up the environment for another script loaded from the network.

Inline scripts are targeted using the id attribute.

Let's assume that the following snippet is a custom HTML tag in GTM:

<script src="https://third-party-service-that-needs-main-thread.com/tracking.js" async></script>
<script id="third-party-service">
window._thirdParty = window._thirdParty || [];
</script>

Now, using the following configuration:

<Partytown
  loadScriptsOnMainThread={[
    'https://third-party-service-that-needs-main-thread.com/tracking.js',
    'third-party-service'
  ]}
  ...

both scripts will be executed on main thread, but I can still controlled them with GTM from Partytown.


This PR is not enough to run GTM's custom html tag on the main thread. A bug in innerHTML implementation for the HTMLScriptElement should be fixed too. I created a PR with proposed, although not ideal fix #234

Read the original on github.com ↗