14

Partytown, Offloading 3RD Party Scripts to Web Workers

 2 years ago
source link: https://www.infoq.com/news/2022/07/partytown-web-workers-library/?itm_source=infoq&itm_medium=popular_widget&itm_campaign=popular_content_list&itm_content=
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Partytown, Offloading 3RD Party Scripts to Web Workers

Jul 27, 2022 1 min read

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"

<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

Guy Nesher

Developer at Locusview focusing on web technologies and active speaker/meetup organizer.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK