Skip to main content

Defer JavaScript (defer_javascript)

Category:
JavaScript
CoreFilters:
No
OptimizeForBandwidth:
No
Risk:
Test first

Defers execution of all JavaScript until after the page finishes loading. This can dramatically improve initial render time but will break scripts that rely on executing during page parse (e.g., document.write).

When to use it

  • Not in CoreFilters: it runs only when you enable it by name.
  • Risk rating on these docs: Test first.

Risks

  • Scripts using document.write will fail.

  • Scripts that expect to run before DOMContentLoaded may break.

  • Order-dependent scripts may execute in unexpected order.

  • Inserts a <noscript> redirect by default. Disable with SupportNoScriptEnabled false.

  • Since v1.15.0+r18, with HonorCsp (default on) the filter stands down entirely on pages whose Content-Security-Policy forbids inline scripts — deferral relies on injected inline JavaScript, which such a policy would block.

  • Since v1.15.0+r21, defer_javascript — along with disable_javascript, defer_iframe, fix_reflow, and the shared support_noscript fallback — switches off for clients identified as automated, including clients that send no User-Agent; those clients receive the page’s normal authored script markup. Search-engine crawlers are included deliberately. An automated client presenting a browser’s exact user-agent string is indistinguishable from that browser and still receives the deferred form.

Configuration

Enable it in the module configuration, at server, virtual-host or location scope:

Apache

ModPagespeedEnableFilters defer_javascript

nginx

pagespeed EnableFilters defer_javascript;

IIS (pagespeed.config)

pagespeed EnableFilters defer_javascript

On the worker

The worker runs its own pipeline, configured by flags. Its equivalent of this filter is the script deferral transform. Runs as part of HTML optimization when browser analysis has produced a script profile; it has no flag of its own.

Scoping, ForbidFilters and the thresholds filters read: Choosing filters .

Live example

Defers script execution until the page has loaded.

mod_pagespeed applies this only after a real browser loads the page and reports back, so the example shows it live rather than as a captured diff.

See the before and after, with the source diff →

Frequently asked questions

What does the defer_javascript filter do?
Defers execution of all JavaScript until after the page finishes loading. This can dramatically improve initial render time but will break scripts that rely on executing during page parse (e.g., document.write).
Is defer_javascript enabled by default?
No. defer_javascript is not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name with EnableFilters.
How do I enable defer_javascript on Apache and nginx?
Add ModPagespeedEnableFilters defer_javascript on Apache or pagespeed EnableFilters defer_javascript; on nginx. On IIS, add pagespeed EnableFilters defer_javascript to pagespeed.config.

This page is drawn from the defer_javascript entry on JavaScript filters. Every filter in one table: PageSpeed filters.

Search