Fixing Main-Thread Blocking From Chat Widgets

Support chat widgets are among the heaviest third-party scripts a static site carries: a loader, a full messaging application, fonts, an iframe or two, a WebSocket and often analytics of their own. They usually load on every page, immediately, because that is what the vendor's snippet does. On a static page that otherwise ships almost no JavaScript, a chat widget can be the entire main-thread cost — and since it runs during and just after load, it lands exactly when readers first try to scroll, tap a menu or open search.

This guide measures the cost of a commercial chat widget on a SaaS documentation and marketing site built with Eleventy, then reduces it in three steps: deciding where chat is needed, delaying the vendor until the page is idle, and replacing the eager widget with a static launcher that loads it on click. It belongs to Third-Party Script Performance on Static Sites; the general facade pattern is in Facades for Chat Widgets and Maps.

Step 1: Measure the Widget's Cost

A Performance panel recording of a docs page on a 4× throttled CPU, grouped by domain, attributed the main-thread time:

SourceMain-thread time during loadLongest taskTransfer
Site's own scripts70 ms32 ms11 KB
Chat vendor (loader + app)690 ms280 ms410 KB
Analytics120 ms64 ms38 KB

Lighthouse's "Reduce the impact of third-party code" audit agreed within 10%. To confirm the effect in the field, a 10% sample of sessions had the chat script blocked through a feature flag for a week, and INP and Total Blocking Time were compared, as in Measuring Third-Party Cost with Request Blocking.

Main-thread milliseconds during load attributed to each source on a throttled CPU Horizontal bars. The chat vendor's loader and application took 690 milliseconds. Analytics took 120 milliseconds. The site's own scripts took 70 milliseconds. Main-thread time during load by source Chat vendor 690 ms Analytics 120 ms Site scripts 70 ms
Chrome Performance panel, 4× CPU throttling, docs article, grouped by domain; median of five recordings.

The field comparison: sessions without the widget had INP p75 of 110 ms on mobile; sessions with it, 230 ms. One widget doubled INP for the whole site.

Step 2: Load Chat Only Where Support Is Offered

The support team was asked where chat mattered. Analytics answered more precisely: 84% of conversations started on pricing, account, billing and three troubleshooting sections of the docs — about 9% of page views. Reference documentation, the blog and the changelog produced almost none. The widget now loads only on templates and sections flagged in front matter:

# docs/troubleshooting/troubleshooting.11tydata.json
{ "chat": true }
{% if chat %}<div id="chat-launcher-slot"></div>{% endif %}

On every other page, the widget simply does not exist. A "Contact support" link in the footer goes to the support page, which has chat.

Step 3: Never Load the Vendor During Page Load

On pages that do offer chat, the vendor script still ran its 690 ms during load. Two changes moved that work out of the window when readers first interact.

First, a static launcher, rendered at build time, that looks like the vendor's button:

<button class="chat-launcher" type="button" aria-label="Open support chat" hidden>
  <span class="icon" aria-hidden="true"></span>
</button>

Second, the vendor loads on the first of: a click on the launcher, or the page being idle for a few seconds after load and the reader having scrolled — a sign they are reading rather than bouncing. A click shows "Connecting…" in the launcher immediately, loads the vendor, and opens the conversation when it is ready:

const btn = document.querySelector('.chat-launcher');
btn.hidden = false;
let loading;
function loadChat(open) {
  loading ??= new Promise((res) => {
    const s = Object.assign(document.createElement('script'), { src: 'https://widget.chat-vendor.example/v3/loader.js', async: true });
    s.onload = () => window.ChatVendor.init({ appId: 'acme', hideDefaultLauncher: true }).then(res);
    document.head.append(s);
  });
  if (open) { btn.dataset.state = 'loading'; loading.then(() => { window.ChatVendor.open(); btn.dataset.state = ''; }); }
}
btn.addEventListener('click', () => loadChat(true));
addEventListener('scroll', () => requestIdleCallback(() => loadChat(false), { timeout: 8000 }), { once: true, passive: true });

hideDefaultLauncher keeps the vendor from adding its own button, so the static one stays in control. The idle preload means that for most readers who do click, the widget is already loaded and opens instantly.

Loading sequence of the chat widget after the fix Three actors, the reader, the page and the chat vendor. The page loads with a static launcher and no vendor script. The reader scrolls, and once the page is idle the page loads the vendor script in the background. If the reader clicks the launcher before that, the page shows a connecting state and loads the vendor immediately. The vendor initialises without its own launcher and opens the conversation. When the chat widget loads now Reader Page Chat vendor page + static launcher scrolls idle: load loader.js clicks launcher conversation opens
The vendor's 690 ms of main-thread work now happens during idle time or after a deliberate click, never during first interactions.

Step 4: Keep the Widget Accessible

The vendor's default launcher had been a <div> with a click handler. The static launcher is a real button with an accessible name, reachable by keyboard, and it moves focus into the chat panel when it opens. The vendor's panel, once loaded, handled focus within itself. An axe scan of the open state showed no new violations, while the old launcher had produced two.

Step 5: Check the Business Metric

Performance changes to support tools need sign-off from the team that owns them. Over four weeks, before and after:

MeasureBeforeAfterSource
Conversations per 1,000 views, chat pages4.14.2Chat vendor reports
Time from launcher click to panel open, median0.3 s (eager)0.4 s (idle-loaded), 1.1 s (cold click)RUM events
Support tickets about "chat not loading"21Help desk

Conversations held steady. The cold-click case — a reader clicking before the idle load — took about a second, with the "Connecting…" state making the wait clear.

Why Not Partytown or a Worker?

Moving third-party scripts into a web worker with Partytown is often suggested for heavy vendors, and it was tried here. It did not suit a chat widget. The widget manipulates the DOM constantly — rendering messages, animating the panel, handling input — and in Partytown every DOM access from the worker becomes a synchronous round trip through the main thread. Opening the panel took 2.3 seconds in a test, and typing lagged visibly. Workers suit scripts that mostly compute and send data, such as some analytics; widgets that are themselves user interfaces belong on the main thread, loaded only when they are needed. Running Third-Party Scripts in a Web Worker with Partytown covers where it does help.

Keeping New Vendors in Check

The chat widget was not the last vendor the site will add. A short policy now applies to any third-party script: it must name the pages it needs, load no earlier than idle time unless it affects the first paint, and come with a before-and-after Lighthouse run on one affected page in the pull request. The Lighthouse CI budget for JavaScript per template enforces the first rule mechanically, since a vendor loaded site-wide pushes every template over its budget.

Measured Impact

MeasureBeforeAfterTool
Pages loading the chat vendor100%9% of viewsAnalytics
TBT, docs article, mobile lab620 ms80 msLighthouse, median of 5
TBT, troubleshooting page, mobile lab640 ms90 msLighthouse (vendor loads after idle)
INP p75, mobile, site-wide230 ms115 msweb-vitals RUM
JS transferred, docs article459 KB49 KBDevTools
Total Blocking Time and field INP before and after the chat changes Paired bars. Total Blocking Time on a docs article went from 620 to 80 milliseconds. On a troubleshooting page that still offers chat, from 640 to 90 milliseconds. Field INP at the 75th percentile on mobile went from 230 to 115 milliseconds. Main-thread cost before and after Before After TBT docs article 620 ms 80 ms TBT troubleshooting 640 ms 90 ms INP p75 field 230 ms 115 ms
Lighthouse 12 mobile, median of five runs; field INP from web-vitals RUM over four weeks.

Pitfalls & Rollback

  • Assuming async solves it. The work still runs during load.
  • Loading chat everywhere. Ask where conversations start; most pages never need it.
  • A launcher that does nothing visible on click. Show a loading state immediately.
  • Changing support tooling without the support team. Track conversation rates before and after.
  • Rollback: the front matter flag and the loader are separate; setting chat: true site-wide restores coverage while keeping the deferred loading.

Conclusion

A chat widget can be the largest main-thread cost on a static site. Measuring it, limiting it to the 9% of page views where support conversations actually start, and replacing the eager vendor snippet with a static launcher that loads the widget during idle time or on click halved site-wide INP and cut Total Blocking Time on docs pages from 620 to 80 ms, with no change in how often readers started a conversation.

FAQ

How much does a chat widget slow a page?

On the site in this guide, the vendor's widget added 410 kilobytes of JavaScript and 690 milliseconds of main-thread work on a mid-range phone during load, split into long tasks of up to 280 milliseconds. That raised Total Blocking Time from 80 to 620 milliseconds and field INP by about 120 milliseconds on pages where it loaded.

How do I measure a third-party widget's main-thread cost?

Record a Performance panel trace with CPU throttling and group activity by domain or by third party, or use Lighthouse's third-party summary, which attributes main-thread time to each vendor. For field impact, compare INP attribution with and without the widget by blocking its domain for a small share of sessions.

Does loading the chat script with async or defer fix the problem?

Only partly. async and defer stop the script from blocking HTML parsing, but the script still runs during or right after load, producing long tasks that delay the first interactions. The main-thread cost has to be moved later, to idle time or the first user interaction, or avoided on pages that do not need chat.

Will readers notice if the chat widget loads on click?

In the test in this guide, chat conversations started per thousand page views were unchanged when the real widget loaded on the first click of a static launcher. The launcher looks the same and shows a loading state for under a second while the widget initialises.

Filed underFixes