PageSpeed Service

Optimize JavaScript

Objective

Reduce payload size of external JavaScript resources by compiling the JavaScript contents.

PageSpeed rule

This rewriter implements the PageSpeed rule for minimizing payload size.

Description

This rewriter minifies JavaScript code, using Closure Compiler. It works only on external JavaScript resources, that are defined in src attribute of <script> tags and are proxied via Google (using either the Proxy JavaScript or Combine JavaScript rewriter). Actions taken by this rewriter include: stripping of comments and most whitespaces and rewriting of the content using the compiler. This optimization can reduce the byte count in common JavaScript code and can be used to avoid the extra step of compiling Javascript before pushing changes to your site. While it is great to put comments, tabs and whitespace in code to improve readability and maintenance, these are bytes that take up space on the wire, and bytes that a browser's JavaScript parser has to parse unncessarily, and such bytes are removed by this rewriter.

By default this rewriter is disabled, but you can enable it from the Rewriter Settings pane of the Google APIs Console.

Operation

This rewriter is applied on all proxied or combined JavaScript resources served from the PageSpeed Service.

Example

The example below shows the external Javascript before rewriting:

/*
 * This external script is part of the optimize javascript example.
 * This comment will be removed.
 */
// Print on the screen. This comment will also be removed.
document.write("This is updated by external javascript");

and after rewriting:

document.write("This is updated by external javascript");

Limitations

Since all comment blocks are removed by this rewriter, you will need to use @preserve or @license to retain comment blocks containing legal licenses or copyright text, as shown below.

/**
 * @preserve Copyright 2011 SomeThirdParty.
 * Here is the full license text and copyright
 * notice for this file. Note that the notice can span several
 * lines and is only terminated by the closing star and slash:
 */

Since this rewriter is only applied to JavaScript resources whose src attribute is already modified by the Proxy JavaScript or Combine JavaScript rewriter, limitations applicable to the Proxy JavaScript rewriter are also applicable to this rewriter.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.