gitmyhub

partytown

TypeScript ★ 14k updated 24d ago

Relocate resource intensive third-party scripts off of the main thread and into a web worker. 🎉

Partytown moves slow third-party scripts like analytics and ad trackers off your webpage's main thread into a background worker, keeping your site fast without losing tracking functionality.

TypeScriptJavaScriptWebWorkersHTMLsetup: moderatecomplexity 2/5

Partytown is a small JavaScript library that solves a common website speed problem. When you add third-party tools to a website, things like analytics trackers, advertising scripts, or chat widgets, those scripts run on the browser's main thread. The main thread is also what the browser uses to display your pages and respond to user clicks and scrolls. When third-party scripts compete for that same thread, your site feels slower and scores lower on performance audits like Google Lighthouse.

Partytown's approach is to move those third-party scripts off the main thread entirely, into a separate background area of the browser called a web worker. Web workers run in parallel and cannot block the main thread, so your site's own code gets priority. The third-party scripts still run and do their job (collecting analytics data, tracking conversions, and so on), but they do it without slowing down what the user sees.

Using Partytown requires adding a small snippet to your HTML and marking third-party script tags with a special attribute so the library knows which ones to relocate. Configuration options let you control which scripts are moved and how they communicate back with the main page when needed. The library is designed to integrate with popular frameworks and site builders, and integrations are documented for common setups.

The README notes that Partytown is still in beta and may not work correctly in every situation. The trade-offs section of the documentation explains scenarios where moving a script to a worker can cause problems, so it is worth reviewing those before adding it to a production site. The project is maintained by the Builder.io team and is related to their Qwik framework.

Where it fits