homeLogo

~ 2 min read

Using Partytown to load your site faster

Partytown is a lazy-loaded library to help relocate resource intensive scripts into aweb worker, and off of themain thread.

Partytown

Partytown is a lazy-loaded library to help relocate resource intensive scripts into a web worker, and off of the main thread.

If you’re using third-party scripts for things like analytics or ads, Partytown is a great way to make sure that they don’t slow down your site.

The Astro Partytown integration installs Partytown for you and makes sure it’s enabled on all of your pages.

Installation

The astro add command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren’t sure which package manager you’re using, run the first command.) Then, follow the prompts, and type “y” in the terminal (meaning “yes”) for each one.

# Using NPM
npx astro add partytown
# Using Yarn
yarn astro add partytown
# Using PNPM
pnpm astro add partytown

Finally, in the terminal window running Astro, press CTRL+C and then restart the dev server.

If you run into any issues, feel free to report them to us on GitHub and try the manual installation steps below.

Manual Install

First, install the @astrojs/partytown package using your package manager. If you’re using npm or aren’t sure, run this in the terminal:

npm install @astrojs/partytown

Then, apply this integration to your astro.config.* file using the integrations property:

astro.config.mjs

import { defineConfig } from 'astro/config';
import partytown from '@astrojs/partytown';

export default defineConfig({
  // ...
  integrations: [partytown()],
})

Then, restart the dev server.

Usage

Partytown should be ready to go with zero config. If you have an existing 3rd party script on your site, try adding the type="text/partytown" attribute:

-  <script src="fancy-analytics.js"></script>
+  <script type="text/partytown" src="fancy-analytics.js"></script>

If you open the “Network” tab from your browser’s dev tools, you should see the partytown proxy intercepting this request.

Configuration

To configure this integration, pass a ‘config’ object to the partytown() function call in astro.config.mjs.

astro.config.mjs

export default defineConfig({
  integrations: [partytown({
    config: {
      //options go here
    }
  })]
});

This mirrors the Partytown config object, but only debug and forward are exposed by this integration.