Skip to main content

Minify inline JavaScript (rewrite_javascript_inline)

Category:
JavaScript
CoreFilters:
Yes
OptimizeForBandwidth:
Yes
Risk:
Generally safe

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.

How it works

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. How safe JavaScript minification handles automatic semicolon insertion has the details.

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.
  • Also switched on by rewrite_javascript.
  • 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-Pagespeed response header and a ?PageSpeedFilters=-rewrite_javascript comparison; Is it working? has the steps.

Configuration

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

Apache

ModPagespeedEnableFilters rewrite_javascript_inline

nginx

pagespeed EnableFilters rewrite_javascript_inline;

IIS (pagespeed.config)

pagespeed EnableFilters rewrite_javascript_inline

It is on by default under CoreFilters; to turn it off:

ModPagespeedDisableFilters rewrite_javascript_inline   # Apache
pagespeed DisableFilters rewrite_javascript_inline;   # nginx
pagespeed DisableFilters rewrite_javascript_inline   # IIS

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

Frequently asked questions

What does the rewrite_javascript_inline filter do?
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.
Is rewrite_javascript_inline enabled by default?
Yes. rewrite_javascript_inline is in CoreFilters, the default RewriteLevel, so it runs unless you turn it off with DisableFilters. It is also part of the OptimizeForBandwidth level.
How do I turn off rewrite_javascript_inline?
Add ModPagespeedDisableFilters rewrite_javascript_inline on Apache or pagespeed DisableFilters rewrite_javascript_inline; on nginx. On IIS, add pagespeed DisableFilters rewrite_javascript_inline to pagespeed.config.

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

Search