Skip to main content

Handling the impact of third-party scripts

Third-party scripts often refer to elements embedded in a webpage and served by a domain different from the one being accessed. These scripts can include social sharing buttons, video player embeds, ads, analytics, and other scripts that make the web more dynamic and interactive.

Third-party scripts

Performance impact

Unfortunately, third-party scripts tend to slow down website performance significantly. Because they run on the main thread by default, these scripts are likely to block the main thread, preventing all other tasks from executing and delaying user interaction. Related issues include long execution times, slow server response, slow DNS lookup, server response errors, etc.

Dealing with third-party scripts

Still, some third-party scripts, such as the ones responsible for injecting analytics and processing payments, might be necessary. Hence, to preserve your website performance while not ultimately resigning from the functionalities provided by third-party scripts, we suggest considering the following:

  • Review your third-party scripts and only keep the ones that are actually adding value to your website.
  • Self-host the script if the third-party server is slow.
  • Move third-party scripts into a Web Worker, i.e., a JavaScript that runs in the background and independently of other scripts.

In the following section, you'll learn how to move your essential third-party scripts into a Web Worker using Partytown.

"Partytown is a lazy-loaded library to help relocate resource intensive scripts into a web worker, and off of the main thread." Notice that Partytown is a beta project. Therefore, there are limitations and trade-offs, you should check before proceeding any further.


Step by step

In the following step by step, you'll learn how to use Partytown and organize your third-party scripts. We will use the Google Tag Manager script as an example.


Notice that if you quick started your FastStore project from one of our official Starters, the Google Tag Manager script is already set up.

Step 1 - Installing Partytown

  1. Open your FastStore project in any code editor of your preference.
  2. Install Partytown.
    yarn add

Step 2 - Defining and exporting third-party scripts

  1. Declare your key-value configurations (e.g., the GTM container ID) in the store.config.js file as in the following:

    analytics: {
    gtmContainerId: 'GTM-XXXXXX',
  2. Create a folder named ThirdPartyScripts inside src/components. Use this folder to organize and save all third-party scripts you want to run on your website.

  3. Create the GoogleTagManager.tsx file inside src/components/ThirdPartyScripts and declare the Google Tag Manager script as in the following:

    interface Props {
    containerId: string
    dataLayerName?: string

    const GTM_DEBUG_QUERY_STRING = 'gtm_debug'

    /* Google Tag Manager script adapted to be executed only when necessary, according to*/
    const useSnippet = (opts: Props & { partytownScript: boolean }) => `${
    opts.partytownScript ? '!' : ''
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    opts.dataLayerName ?? 'dataLayer'

    function GoogleTagManager(props: Props) {
    return (
    /*Adds GoogleTagManager script to Partytown.*/
    __html: useSnippet({
    partytownScript: true,
    /*Adds GoogleTagManager script to the page, using GTM_DEBUG_QUERY_STRING for debugging reasons.*/
    __html: useSnippet({
    partytownScript: false,

    export default GoogleTagManager

Step 3 - Injecting the scripts into Partytown

  1. Create the ThirdPartyScripts.tsx file inside src/components/ThirdPartyScripts. Use this file to inject your scripts into the <Partytown/> component.

  2. Import the <Partytown/> component from the submodule, your scripts (e.g., the GoogleTagManager script) and your configurations from store.config.js.

    import React from 'react'
    import { Partytown } from ''
    import storeConfig from '../../../store.config'
    import GoogleTagManager from './GoogleTagManager'
  3. Declare all the necessary variables related to the script and handle the case of a missing configuration (e.g., no GTM container ID).

     const isString = (obj: unknown): obj is string => typeof obj === 'string'

    const gtmContainerId = /*The GTM Container ID specified in the store.config.js file*/

    const includeGTM = typeof gtmContainerId === 'string'

    if (process.env.NODE_ENV === 'development' && !includeGTM) {
    'No GTM container id found. Check the analytics section on your store.config.js file for enhanced observability of your store.'
  4. Set up a function named ThirdPartyScripts that runs the <Partytown/> component and use the forward attribute to inject your scripts. Set up the other attributes (debug, lib, loadScriptsOnMainThread, and resolveUrl) according to your scenario. Please refer to Partytown documentation for more information.

    function ThirdyPartyScripts() {
    return (
    {includeGTM && <GoogleTagManager containerId={gtmContainerId} />}
    forward={[ /* Variables to forward from main to worker. See for more information */
    includeGTM && 'dataLayer.push',

    export default ThirdyPartyScripts

For additional examples, please refer to our Official Starters implementations (Next.js/Gatsby).

Step 4 - Running the ThirdyPartyScripts function

  1. Import and run the ThirdyPartyScripts function in your page's Head as in the following. For Next.js projects, use the src/pages/_document.tsx file. For Gatsby projects, use the gatsby-ssr.tsx file.
   import { Head, Html, Main, NextScript } from 'next/document'

import WebFonts from 'src/fonts/WebFonts'
+ import ThirdPartyScripts from 'src/components/ThirdPartyScripts'
import storeConfig from 'store.config'

function Document() {
return (
+ <ThirdPartyScripts />
<WebFonts />
<body className={storeConfig.theme}>
<Main />
<NextScript />

export default Document

Didn't find your answers? Ask the Community. For documentation suggestions, submit your feedback.