proxytown
Run third-party scripts in web workers
Proxytown moves third-party scripts to a web worker, freeing the main thread for your app. This library is inspired by Partytown but with a different approach. Proxytown automatically handles cross-thread global variables - scripts just work without manual configuration.
The Problem
Third-party scripts are a performance killer. Analytics, ads, trackers, A/B testing. They all compete with your code for main thread resources. Even a fast, well-optimized site can feel sluggish once these scripts load.
We can't just simply move third-party scripts to a web worker because, but web workers have no access to the DOM. No document, no window, no localStorage. Third-party scripts assume these APIs exist. An analytics script that calls document.cookie or window.location will immediately fail in a worker.
You could rewrite every third-party script to use postMessage for DOM access, but that's not practical. These scripts are minified, obfuscated, and frequently updated. You don't control them.
How Proxytown Solves This
Proxytown creates a fake document and window inside the worker. When a script reads document.cookie, Proxytown intercepts the call, sends it to the main thread, gets the real value, and returns it synchronously. The script never knows it's running in a worker.
This proxy layer handles reads, writes, and method calls. Scripts created dynamically by third-party code also execute in the worker automatically. Globals created in the worker are accessible from the main thread, and vice versa.
Benefits
- Main thread stays responsive for user interactions
- Third-party scripts can't block rendering or cause jank
- No manual configuration for global variable access
- Dynamically created scripts run in the worker automatically
- Lightweight: 6KB total for all files
Usage
Download the Proxytown folder from proxytown-download.vercel.app, add the proxytown.html iframe to your page, and set text/proxytown as thetypefor all third-party scripts.