docs/src/routes/how-does-partytown-work/index.mdx
Traditionally, communicating between the main thread and worker thread must be asynchronous. Meaning that for the two threads to communicate, they cannot use blocking calls.
Partytown is different. It allows code executed from the web worker to access DOM synchronously. The benefit from this is that third-party scripts can continue to work exactly how they're coded.
For example, the code below works as expected within a web worker:
const rect = element.getBoundingClientRect();
console.log(rect.x, rect.y);
First thing you'll notice is that there's no async/await, promise or callback. Instead, the call to getBoundingClientRect() is blocking, and the returned rect value contains the expected x and y properties.
Partytown relies on Web Workers, Service Workers, JavaScript Proxies, and a communication layer between them all.
There are currently two ways to communicate synchronously between the web worker and main thread, and that's sync xhr requests combined with Service Workers, and Atomics.
It's important to note that Partytown does not automatically move all scripts to the web worker, but prefers an opt-in approach. Meaning, it's best that the developer can pick and choose exactly which scripts should use Partytown, while all the others would go unchanged. Please see the Partytown Scripts for more info.
Partytown is only enabled for specific scripts when they have the type="text/partytown" attribute. This type attribute does two things:
document.querySelectorAll('script[type="text/partytown"]')Below is an example of adding the type="text/partytown" attribute to an existing <script>.
- <script>...</script>
+ <script type="text/partytown">...</script>
type="text/partytown" attribute on the <script/> tag.onfetch handler to intercept specific requests.Please see the Atomics communication layer docs on how to enable them. When Atomics are not enabled, the fallback is to use the Service Worker instead. In the end, Atomics are preferred because they're roughly 10x faster in transfering data between the web worker and main thread.
type="text/partytown" attribute on the <script/> tag.postMessage() to send the data to the main thread, and run Atomics.wait().Data passed between the main thread and web worker must be serializable. Partytown automatically handles the serializing and deserializing of data passed between threads. This works for primitive values, such as a string, boolean or number, and also functions, which get unique IDs assigned that are passed to the opposite thread and can be called.