Defer and delay JavaScript

Interaction, idle, viewport, and priority strategies with first-click safety guidance.

2 min readPart of Performance Optimisation

Defer and delay move eligible JavaScript out of the critical rendering path. They are separate controls: defer lets the browser download and run a script after parsing, while delay holds a script until an interaction, idle period, or viewport trigger.

Current strategies

  • Interaction is the default strategy and waits for a visitor signal.
  • Idle waits for browser idle or delayJSIdleTimeout.
  • Viewport waits for the associated placeholder to enter the viewport.
  • Priority assigns a high or low fetch priority to selected handles.

The safe preset and builder/commerce presets are opt-in compatibility policies. They do not replace site-specific exclusions.

First-interaction safety

A delayed navigation, menu, consent, payment, or builder script may consume the first tap before its handler is attached. Exclude those handles before changing the idle timeout or moving more scripts to delay. The per-post Asset Manager can override a single page; global exclusions belong in the File Optimisation tab or the documented filters.

Settings

KeyDefaultPurpose
deferJSfalseApply native defer to eligible scripts.
delayJSfalseEnable trigger-based delay.
delayJSDefaultStrategyinteractionDefault trigger for unlisted handles.
delayJSIdleTimeout3000Idle timeout in milliseconds.
delayJSPresetsafePreset level for the opt-in delay workflow.
delayJSSafeModetrueKeep the conservative safety gate.
excludeDeferJS, excludeDelayJS\'\'Global exclusions.

Programmatic exclusions

add_filter( \'wppo_exclude_delay_js\', function ( array $excluded ) {
    $excluded[] = \'theme-main-navigation\';
    return array_values( array_unique( $excluded ) );
} );

Use the filter only when the site owns a known integration. Keep the callback narrow, return the expected array, and test the first click on mobile and desktop.

Verify

  1. Clear the page cache after saving.
  2. Load a logged-out page with the network panel open.
  3. Test menu, search, modal, consent, checkout, and builder interactions.
  4. Compare INP and interaction latency before and after; do not judge only by page size.