How to Optimize Third-Party Scripts
· Category: Web Performance
Short answer
Third-party scripts for analytics, ads, and widgets often degrade performance. Optimizing their loading reduces their impact on Core Web Vitals.
Steps
- Audit all third-party scripts and remove unused ones.
- Load non-critical scripts asynchronously with
asyncordefer. - Use facades for heavy embeds like video players and chat widgets.
- Self-host critical third-party fonts or analytics if permitted.
- Implement a Content Security Policy to control what can load.
Tips
- Use the
partytownlibrary to offload analytics scripts to web workers. - Preconnect to third-party origins to reduce connection overhead.
- Monitor the performance impact of each script with Chrome DevTools.
Common issues
- Deferred scripts may still compete for main thread time.
- Some third-party scripts load additional sub-resources unpredictably.