Third-party scripts slowing your Next.js site is the most common performance problem I see after the code itself has been cleaned up. The pages are server-rendered, the Sanity images are optimised, and the scores are still poor because a tag manager is loading eleven things nobody remembers approving. This post covers how to find the offenders, what ignoring them costs, and the fix path.

Symptoms of third-party scripts slowing your Next.js site

The pattern is consistent. Lighthouse on a clean local build looks fine, but field data in Search Console or Vercel Analytics is worse. The usual signs:

  • INP is poor even though the React code is small. Clicks on menus and filters feel delayed by a beat.
  • LCP is fine in development and bad in production, because production has the marketing tags and development does not.
  • Total Blocking Time is high in Lighthouse, and the trace shows long tasks attributed to domains you do not own.
  • The site gets slower every quarter without a single code deploy, because someone in marketing keeps adding tags in the tag manager.
  • A cookie banner, chat bubble, or A/B testing tool causes visible layout shift or a flash of changed content.

That last point matters. Because tags are managed outside the repository, the slowdown does not show up in a pull request. Nobody reviewed it.

What it costs to ignore it

Google uses real-user Core Web Vitals as a ranking signal, and slow interaction hurts conversion before it hurts rankings. If you want the business case, I wrote about what fast pages are actually worth. The less obvious cost is that developers get blamed. A team spends weeks rewriting components to shave kilobytes while a single chat widget holds the main thread for hundreds of milliseconds. That is wasted budget.

Step 1: Inventory every script that actually loads

Start with what loads, not what you think loads. Open the live site in an incognito window, open DevTools, and look at the Network tab filtered to JS. Sort by size and group by domain. Then open the Performance panel, record a page load plus a few real interactions, and use the bottom-up view grouped by domain.

You will usually find three categories:

  • Scripts someone still needs: analytics, consent management, payment.
  • Scripts nobody remembers: an old heatmap trial, a retired ad pixel, a survey tool from a campaign two years ago.
  • Scripts that load other scripts: the tag manager container pulling in a dozen more.

Get the marketing owner in a room and decide, tag by tag, keep or remove. Deleting is the cheapest optimisation there is, and in my experience it usually accounts for the biggest single improvement.

Step 2: Load the keepers correctly

In the App Router, scripts belong in the layout, loaded through the framework rather than pasted into the head as raw tags. The official @next/third-parties package ships components for Google Tag Manager and Google Analytics that handle loading order for you.

// app/layout.tsx import { GoogleTagManager } from "@next/third-parties/google"; import Script from "next/script"; export default function RootLayout({ children, }: { children: React.ReactNode; }) { return ( <html lang="en"> <body>{children}</body> <GoogleTagManager gtmId="GTM-XXXXXXX" /> {/* Non-critical script: wait until the browser is idle */} <Script src="https://example.com/heatmap.js" strategy="lazyOnload" /> </html> ); } 

The strategy prop is the lever. afterInteractive is the default for most tags and loads after hydration starts. lazyOnload waits until the browser is idle, which suits heatmaps, surveys and anything that does not need to run before the first interaction. Reserve beforeInteractive for things that genuinely must run first, such as a consent check, and use it rarely because it blocks.

One caveat: moving a script to lazyOnload can break tracking if the tag needs to fire on page view. Test with the vendor's debug tool, not by assuming.

Step 3: Load widgets on interaction, not on page load

Chat bubbles are the worst offenders. They ship a large bundle so a button can sit in the corner that perhaps two percent of visitors click. Use a facade: render a lightweight button, and load the real widget only when someone asks for it.

// components/chat-launcher.tsx "use client"; import { useState } from "react"; export function ChatLauncher() { const [loading, setLoading] = useState(false); function loadWidget() { if (loading) return; setLoading(true); const s = document.createElement("script"); s.src = "https://example.com/chat-widget.js"; s.async = true; document.body.appendChild(s); } return ( <button type="button" onClick={loadWidget} className="fixed bottom-4 right-4 rounded-full bg-black px-4 py-3 text-white" > {loading ? "Loading chat..." : "Chat with us"} </button> ); } 

The button has a fixed size and position, so there is no layout shift when the real widget replaces it. The same idea works for embedded video, maps and social feeds. The @next/third-parties package also provides facade-style embeds for YouTube and Google Maps.

Step 4: Audit what the tag manager injects

A tag manager container is a script that loads other scripts, and it is also where the problem regrows. Open the container and look for tags firing on all pages that only matter on one. Restrict them with triggers. Remove tags with no recent activity. Agree a rule that any new tag needs an owner and a reason, because otherwise the container becomes a dumping ground.

Step 5: Verify with field data, not a single Lighthouse run

Lighthouse is a lab test and varies run to run. After changes, check that total blocking time drops in the trace, then watch real-user INP and LCP over the following weeks in Search Console or your analytics. Field data lags by roughly a month in Search Console, so do not panic early. If INP is still poor after the scripts are tamed, the problem is probably your own code, and profiling INP in React apps is the next step. For the overall order of operations, see fixing Core Web Vitals on a Next.js and Sanity site.

What to ask a developer

If you hire help, these questions separate people who profile from people who guess:

  • Can you show me which domains consume the most main-thread time on my site today?
  • Which tags can be removed, and who signs off?
  • How will you stop the tag manager from regrowing the problem?
  • What will you measure afterwards, and over what period?

A good answer includes a before and after trace and a list of tags with a decision beside each. If you want to know what a paid review should contain, I broke that down in what a Next.js website audit covers.

This is work I do. My Performance & Sanity audit starts from $800 and includes a third-party script inventory with a prioritised fix list; details are on the services and packages page.