Skip to content

Third-Party Scripts Blocking Load Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Third. We cover key concepts, practical examples, and best practices.

The Problem

Third-party scripts for analytics, ads, social widgets, and chatbots load synchronously and block the main thread. Each script adds DNS lookups, TLS negotiation, parsing time, and JavaScript execution — delaying page rendering and user interactivity.

Quick Fix

Step 1: Use async and defer for all third-party scripts

<!-- Wrong — sync script blocks rendering -->
<script src="https://analytics.example.com/sdk.js"></script>

<!-- Right — use async for independent scripts -->
<script async src="https://analytics.example.com/sdk.js"></script>

<!-- Use defer for scripts that need DOM order -->
<script defer src="https://widget.example.com/sdk.js"></script>

Step 2: Load non-critical scripts after page load

// Wrong — loads script immediately
const script = document.createElement('script');
script.src = 'https://chat.example.com/sdk.js';
document.head.appendChild(script);

// Right — load after page is fully rendered
window.addEventListener('load', () => {
    const script = document.createElement('script');
    script.src = 'https://chat.example.com/sdk.js';
    script.async = true;
    document.body.appendChild(script);
});

// Or use requestIdleCallback for even later loading
if ('requestIdleCallback' in window) {
    requestIdleCallback(() => {
        const script = document.createElement('script');
        script.src = 'https://chat.example.com/sdk.js';
        document.body.appendChild(script);
    }, { timeout: 5000 });
}

Step 3: Preconnect to third-party origins

<!-- Wrong — DNS/TLS negotiation happens when script loads -->
<script async src="https://analytics.example.com/sdk.js"></script>

<!-- Right — prepare connection early -->
<link rel="preconnect" href="https://analytics.example.com">
<link rel="dns-prefetch" href="https://analytics.example.com">

<!-- Only preconnect to 3-4 critical origins -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

Step 4: Use lightweight alternatives

<!-- Wrong — heavy social widget embed -->
<div id="facebook-widget">
    <div class="fb-page" data-href="https://facebook.com/example"></div>
</div>
<script async defer crossorigin="anonymous"
    src="https://connect.facebook.net/en_US/sdk.js"></script>

<!-- Right — simple text link (no JS) -->
<a href="https://facebook.com/example" rel="noopener" target="_blank">
    Follow us on Facebook
</a>

Step 5: Sandbox third-party iframes

<!-- Wrong — iframe loads all resources freely -->
<iframe src="https://ads.example.com/ad.html"></iframe>

<!-- Right — restrict capabilities -->
<iframe
    src="https://ads.example.com/ad.html"
    sandbox="allow-scripts allow-same-origin"
    loading="lazy"
    width="300"
    height="250"
></iframe>

Prevention

  • Always use async or defer on third-party scripts
  • Load scripts after window.load event when possible
  • Limit preconnect hints to 3-4 critical origins
  • Replace heavy widgets with simple links where possible
  • Use sandbox attribute on third-party iframes for security and performance

Common Mistakes with third party

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world PERF code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How many third-party scripts are too many?

Lighthouse recommends fewer than 10 third-party requests. Each third-party script adds ~50-300ms of blocking time. Audit your page regularly and remove unused integrations. Every script should justify its performance cost.

What is the impact of third-party scripts on Core Web Vitals?

Third-party scripts affect all Core Web Vitals: they delay LCP (network contention), increase TBT/INP (main thread blocking), and can cause CLS (late-loading ads and widgets shifting content).

Should I self-host third-party scripts?

Self-hosting removes DNS/TLS overhead but you lose automatic updates. It is best for scripts you control (custom analytics) or that change rarely. For regularly updated scripts, use preconnect + async loading instead.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro