HomeMethod

Knowledge

Web VitalsTechniquesSnippetsAnswersImpactsBottlenecks

Page-agnostic JavaScript

  • JavaScript

Problem Summary

Page-agnostic JavaScript means scripts are loaded globally even when only a small number of pages need the related feature.

Performance Impact

  • Slower page load: Extra scripts increase download and execution time
  • Poor INP: Global listeners and hydration work block interactions
  • Plugin bloat: Forms, sliders, maps, and ecommerce scripts can load everywhere

How to Detect

  1. Network tab: Compare scripts across different templates
  2. Coverage tab: Identify scripts with little or no executed code
  3. Query Monitor: Review enqueued scripts and their sources

Solutions

1. Conditional Enqueueing

Code
if ( is_page( "contact" ) ) { wp_enqueue_script( "contact-form" ); }

2. Disable Plugin Assets Where Unused

Use plugin settings or custom dequeue logic to prevent global asset loading.

3. Lazy Load Interactive Features

Load maps, chat, and embeds only after intent or viewport visibility.

4. Audit Theme Defaults

Remove global scripts for components that are not used on the current design.

Support our family now