Chapter 9. Loading Third-Party Scripts
Third-party scripts—from analytics platforms to advertising networks, social-media widgets to customer-support chatbots—are essential to modern websites, but they come with a cost. While Chapter 8 covered what third-party scripts are and why they matter, this chapter focuses on how to load them efficiently. The common thread throughout is sequencing: prioritizing what loads first, delaying what can wait, loading conditionally based on user context, and implementing click-to-load patterns that defer heavy resources until they’re actually needed.
Loading third-party scripts effectively requires balancing functionality with performance. Load them too early or carelessly, and they can block your critical content, slowing down the initial page render and frustrating users. Load them too late, and you might miss capturing important analytics data or delay features users expect. The techniques in this chapter—like using async and defer attributes and implementing lazy loading with facades—give you fine-grained control over when and how third-party content appears on your pages. By the end, you’ll understand how to sequence third-party loading to minimize their performance impact while maintaining the functionality your site needs.
Sequencing and Prioritizing
One of the fundamental aspects of optimizing third-party scripts is controlling when they load and execute relative to your page’s critical content. AI-generated HTML often simply includes ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access