Third-Party Scripts Blocking Load Fix
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
asyncordeferon third-party scripts - Load scripts after
window.loadevent when possible - Limit preconnect hints to 3-4 critical origins
- Replace heavy widgets with simple links where possible
- Use
sandboxattribute on third-party iframes for security and performance
Common Mistakes with third party
- Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro