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.writewill fail. -
Scripts that expect to run before
DOMContentLoadedmay break. -
Order-dependent scripts may execute in unexpected order.
-
Inserts a
<noscript>redirect by default. Disable withSupportNoScriptEnabled false. -
Since v1.15.0+r18, with
HonorCsp(default on) the filter stands down entirely on pages whoseContent-Security-Policyforbids inline scripts — deferral relies on injected inline JavaScript, which such a policy would block. -
Since v1.15.0+r21,
defer_javascript— along withdisable_javascript,defer_iframe,fix_reflow, and the sharedsupport_noscriptfallback — switches off for clients identified as automated, including clients that send noUser-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_javascriptnginx
pagespeed EnableFilters defer_javascript;IIS (pagespeed.config)
pagespeed EnableFilters defer_javascriptOn 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.
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_javascriptis not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name withEnableFilters. - How do I enable defer_javascript on Apache and nginx?
- Add
ModPagespeedEnableFilters defer_javascripton Apache orpagespeed EnableFilters defer_javascript;on nginx. On IIS, addpagespeed EnableFilters defer_javascriptto pagespeed.config.
Related filters
- inline_javascript — Inline JavaScript
- combine_javascript — Combine JavaScript
- lazyload_images — Lazy-load images
- extend_cache_scripts — Extend cache for scripts
- include_js_source_maps — Include JS source maps
- make_show_ads_async — Async AdSense
- outline_javascript — Outline JavaScript
- rewrite_javascript_external — Minify external JavaScript
This page is drawn from the defer_javascript entry on JavaScript filters. Every filter in one table: PageSpeed filters.