BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Partytown, Offloading 3RD Party Scripts to Web Workers

Partytown, Offloading 3RD Party Scripts to Web Workers

Partytown is a small JavaScript library that speeds up web application load times by helping developers to move third party scripts into web workers and off the main thread.

Since JavaScript is single-threaded, developers can not take advantage of the multiple CPU cores available on most devices.

To tackle this challenge, Web Workers were introduced to enable developers to execute code in background threads.

While major browsers have supported Web Workers for roughly ten years, adaptation has been slow due to its complex implementation.

Partytown attempts to simplify the use of Web Workers by providing a simple API to tackle a specific problem - 3rd party scripts like Google Analytics, Facebook Pixel, Mixpanel, etc.

These scripts are essential for modern web applications as they enable companies to track usage, perform AB tests, and provide advertisements - but they come at a performance cost.

By offloading these non-critical scripts onto Web Workers, Partytown can improve web applications' performance without interrupting the application's normal flow and with minimal developer involvement.

Setting up Partytown is a two-step process. First, developers must install and integrate the library into their build process.

Partytown provides a set of short integration tutorials for most major JavaScript frameworks, including React and Angular.

Secondly, developers need to tag the relevant third party scripts that Partytown will manage. This is done by modifying the type of the script tag to type="text/partytown"

i.e.

<script type="text/partytown" src="https://example.com/analytics.js"></script>

Using the text/partytown tag tells the browser not to process the script and provides a query selector through which Partytown can find the tagged scripts and execute them through web workers.

It's important to note that Partytown is still in beta. While several companies have reported using it in production, developers are advised to test that the third party libraries they use continue to work correctly before considering production usage.

Developers can find a list of popular third party libraries tested with Partytown in the following link. These include popular services like Google tag manager and Facebook Pixel.

Developers who wish to learn more about Partytown can find detailed instructions on the official website and follow the builder.io blog for updates on upcoming changes.

Partytown is distributed under the MIT license.

About the Author

Rate this Article

Adoption
Style

BT