Introduction
Partytown moves third-party scripts like analytics, tag managers, and tracking pixels off the main thread and into a web worker. This frees the main thread for your application code, leading to faster page loads and better responsiveness.
What Partytown Does
- Executes third-party scripts inside a web worker instead of the main thread
- Provides synchronous DOM access from the worker via a service worker proxy
- Forwards global function calls (like
dataLayer.push) from main thread to worker - Reduces main-thread blocking caused by analytics, ads, and chat widgets
- Integrates with Next.js, Nuxt, Astro, Remix, and other frameworks via plugins
Architecture Overview
Partytown uses a service worker as a middleware layer between the web worker and the main thread. When a third-party script running in the worker tries to access the DOM, the request is proxied through the service worker to the main thread using synchronous XHR. This approach allows scripts that expect synchronous DOM access to work transparently inside a worker without code changes.
Self-Hosting & Configuration
- Install via npm and copy library files to your public directory with
npx partytown copylib - Add the Partytown snippet to the
<head>of your HTML before any third-party scripts - Change third-party script tags from
type="text/javascript"totype="text/partytown" - Configure the
forwardarray to proxy global function calls likedataLayer.pushorfbq - Use framework-specific integrations for Next.js (
@builder.io/partytown/nextjs), Astro, or Nuxt
Key Features
- Zero changes to third-party script code required
- Configurable forwarding for global function calls and event queues
- Built-in integrations for Next.js, Nuxt, Astro, Remix, and SvelteKit
- Sandboxing: third-party scripts cannot access cookies or localStorage unless explicitly allowed
- Atomic DOM operations batched for minimal cross-thread communication overhead
Comparison with Similar Tools
- Google Tag Manager server-side — runs tags on a server; Partytown runs them client-side in a worker
- Zaraz (Cloudflare) — edge-based third-party script management; Partytown is self-hosted and framework-agnostic
- Script defer/async — reduces parser blocking but scripts still run on the main thread; Partytown moves execution entirely off-thread
- Web Workers (manual) — require rewriting scripts; Partytown provides transparent DOM proxying
- Inline script lazy loading — delays execution; Partytown runs scripts concurrently without blocking
FAQ
Q: Does Partytown work with Google Analytics and GTM?
A: Yes. Google Tag Manager and Google Analytics are common use cases. You add them as type="text/partytown" scripts and forward dataLayer.push.
Q: Will every third-party script work in Partytown? A: Most analytics and tracking scripts work. Scripts that heavily manipulate the visible DOM (like chat widgets that render UI) may need testing. The project maintains a list of tested integrations.
Q: Does Partytown add latency to events? A: There is a small overhead from cross-thread communication, but for analytics and tracking this is negligible. The net effect is positive because your main thread runs faster.
Q: Is Partytown production-ready? A: It is used in production by multiple companies. The library is still labeled beta, so thorough testing with your specific third-party scripts is recommended.