Skip to content

JavaScript Optimization

Gaurav Tiwari edited this page Aug 27, 2026 · 1 revision

JavaScript Optimization

Three independent switches, all off by default. They are separate because their risk profiles are completely different.

Setting Risk What it does
Minify Low Minifies same-origin, non-.min.js files under 2 MB, writes the result to the cache directory, and rewrites the tag
Defer Medium Adds defer to eligible scripts
Delay High Holds matching scripts until the first user interaction

Minify

Only files served from your own wp-content are touched. Anything already named .min.js, larger than 2 MB, off-origin, or outside the content directory is left alone. The minified file is named by the SHA-256 of its output, so identical input never regenerates and the URL is safe to cache immutably.

Defer

defer preserves execution order but moves execution to after parsing. It breaks scripts that rely on document.write, and inline scripts that expect a library to already be defined. Excluded scripts keep their original loading.

Delay

The aggressive one. Matching scripts do not run until the visitor scrolls, moves the pointer, taps, or presses a key. It is very effective on third-party tags and very capable of breaking anything interactive above the fold.

Default delay patterns target analytics and pixels, which is where the win usually is and the risk usually is not:

clarity.ms, connect.facebook.net, google-analytics.com, googletagmanager.com, hotjar.com

These are match patterns compared against scripts your site already loads. GT Performance never contacts these hosts and adds nothing to your site that would.

Exclusions

Add URL fragments under Exceptions, or filter them:

add_filter( 'gt_performance_javascript_exclusions', function ( array $fragments ) {
    $fragments[] = '/plugins/my-checkout/';
    return $fragments;
} );

Analytics and consent plugins are excluded automatically when detected. See Integrations.

Recommended order

  1. Turn on minify, confirm the site still works.
  2. Add defer, click through your most interactive templates.
  3. Only then consider delay, and start with third-party tags rather than your own code.

Test each step on staging. A caching plugin that breaks checkout has cost you more than it saved.

Related

CSS Optimization · Integrations · Hooks Reference

Clone this wiki locally