Minify JavaScript (rewrite_javascript)
- Category:
- JavaScript
- CoreFilters:
- Yes
- OptimizeForBandwidth:
- Yes
- Risk:
- Generally safe
The three filters on this section share one minifier. It removes comments and collapses whitespace; identifiers, string literals, regular expressions and property names are emitted unchanged. It keeps a line break wherever automatic semicolon insertion could otherwise merge two statements into one.
What it switches on
rewrite_javascript is a compound name. Enabling it enables these 2
filters; each can be disabled on its own.
- rewrite_javascript_external — Minify external JavaScript
- rewrite_javascript_inline — Minify inline JavaScript
How it works
How safe JavaScript minification handles automatic semicolon insertion has the details.
rewrite_javascript is the compound CoreFilter for JavaScript minification: enabling it switches on the external and the inline sub-filter together, so every script on the page is minified wherever it lives. It is also part of OptimizeForBandwidth, the level that minifies resources in place without changing their URLs. To minify only one scope, enable rewrite_javascript and disable the other sub-filter by name. Under CoreFilters the compound is on already. A script delivered with an integrity attribute is left untouched by the whole family, because subresource integrity pins the file’s bytes. Since v1.15.0+r21 the tokenizer-based minifier is the only one behind rewrite_javascript, and files that use template literals (backtick strings) minify normally; IE conditional-compilation comments (/*@ ... @*/) and a leading #! line are the only comments it keeps. Live demo: rewrite_javascript.
rewrite_javascript_external handles the external half. Each <script src> file on a domain the module is authorized to fetch is minified and served from a rewritten .pagespeed.jm. URL with a long cache lifetime, so repeat visitors download the minified file once and keep it; the original file on disk is never modified. In OptimizeForBandwidth mode the minified bytes replace the original response in place and the URL stays as authored. Minification saves the most on hand-formatted source; a file a bundler already minified gains nothing and only costs rewrite time. rewrite_javascript_external runs as part of the compound and can also be enabled on its own.
Enabled on its own, rewrite_javascript_external leaves inline <script> blocks exactly as authored; minifying those is the inline sub-filter’s job. Files on domains the configuration does not authorize keep their original URLs as well, so a page that mixes own and third-party scripts sees only the own files rewritten.
rewrite_javascript_inline minifies the contents of <script> blocks in the HTML itself, in place. No URL changes and nothing new is cached; the smaller script simply rides along inside every page view. Blocks whose type does not denote executable JavaScript, such as JSON-LD data islands and HTML templates, are left as they are. Inline scripts tend to be short, so the saving per block is small; the filter’s job inside the compound is to leave no script unminified. Like the external half, rewrite_javascript_inline is in CoreFilters and in OptimizeForBandwidth, and it can be enabled by itself.
rewrite_javascript_inline never moves a block or changes when it runs; it only shrinks the text between <script> and </script>. On pages whose HTML is served many times between deploys that small saving repeats on every serve, which is where the filter earns its place.
When to use it
- In CoreFilters, the default RewriteLevel: it runs unless you disable it.
- Part of OptimizeForBandwidth, the level that optimizes resources without changing their URLs.
- Risk rating on these docs: Generally safe.
Risks
-
The minifier never renames identifiers; the historical failure mode is a script that inspects its own source text, for example through
Function.prototype.toString(), and reacts to the changed formatting. -
Verify with the
X-Mod-Pagespeedresponse header and a?PageSpeedFilters=-rewrite_javascriptcomparison; Is it working? has the steps. -
rewrite_javascriptignores the deprecatedUseExperimentalJsMinifierdirective, which is accepted for compatibility and logs a warning at configuration load (ModPagespeedUseExperimentalJsMinifieron Apache,pagespeed UseExperimentalJsMinifieron nginx). Remove it from your configuration.
Configuration
Enable it in the module configuration, at server, virtual-host or location scope:
Apache
ModPagespeedEnableFilters rewrite_javascriptnginx
pagespeed EnableFilters rewrite_javascript;IIS (pagespeed.config)
pagespeed EnableFilters rewrite_javascriptIt is on by default under CoreFilters; to turn it off:
ModPagespeedDisableFilters rewrite_javascript # Apache
pagespeed DisableFilters rewrite_javascript; # nginx
pagespeed DisableFilters rewrite_javascript # IIS/* before: sum a shopping cart */
function cartTotal(cart) {
var total = 0; // running sum
for (var i = 0; i < cart.items.length; i++) {
total = total + cart.items[i].price;
}
return total;
}
/* after */
function cartTotal(cart){var total=0;for(var i=0;i<cart.items.length;i++){total=total+cart.items[i].price;}return total;}
On the worker
The worker runs its own pipeline, configured by flags. Its equivalent of this filter
is the
js minification
transform. Always-on under pagespeed on;; use --disable-js to skip JS rewriting entirely.
Scoping, ForbidFilters and the thresholds filters read:
Choosing filters
.
Live example
Strips comments and whitespace from JavaScript.
Measured on the demo page, original against optimized:
-
Total bytes
1.8 KB → 1.3 KB −26%
-
HTML size
1.6 KB → 1.3 KB −17%
Frequently asked questions
- What does the rewrite_javascript filter do?
- The three filters on this section share one minifier. It removes comments and collapses whitespace; identifiers, string literals, regular expressions and property names are emitted unchanged. It keeps a line break wherever automatic semicolon insertion could otherwise merge two statements into one.
- Is rewrite_javascript enabled by default?
- Yes.
rewrite_javascriptis in CoreFilters, the default RewriteLevel, so it runs unless you turn it off withDisableFilters. It is also part of the OptimizeForBandwidth level. - How do I turn off rewrite_javascript?
- Add
ModPagespeedDisableFilters rewrite_javascripton Apache orpagespeed DisableFilters rewrite_javascript;on nginx. On IIS, addpagespeed DisableFilters rewrite_javascriptto pagespeed.config.
Related filters
- combine_javascript — Combine JavaScript
- inline_javascript — Inline JavaScript
- defer_javascript — Defer JavaScript
- rewrite_javascript_external — Minify external JavaScript
- rewrite_javascript_inline — Minify inline JavaScript
- extend_cache_scripts — Extend cache for scripts
- include_js_source_maps — Include JS source maps
- make_show_ads_async — Async AdSense
This page is drawn from the rewrite_javascript entry on JavaScript filters. Every filter in one table: PageSpeed filters.