The folks from builder.io set out to create a way to prevent Third-Party Scripts from blocking the main thread. The result is Partytown, which runs Third-Party Scripts Within a Web Worker.
Partytown is able to sandbox and isolate third-party scripts within a web worker and allow, or deny, access to main thread APIs. This includes Cookies,
localStorage
,userAgent
, etc. Because the code must go through Partytown’sProxy
in order to access the main thread, Partytown also has the ability to log every read and write, and even restrict access to certain DOM APIs.
It works by creating JavaScript Proxies to replicate and forward calls to the main thread APIs (such as DOM operations) and making calls to them using synchronous XHR requests. Pretty crazy, right?! 🤯
To mark third-party scripts to run in a Partytown web worker, set the type attribute of its opening script tag to text/partytown
.
<script type="text/partytown">
// Third-party analytics scripts
</script>
Also comes with integrations for frameworks like React.
Partytown (GitHub) →
Introducing Partytown: Run Third-Party Scripts From a Web Worker →