Remove Comments

Configuration

The 'Remove Comments' filter is enabled by specifying:

Apache:
ModPagespeedEnableFilters remove_comments
Nginx:
pagespeed EnableFilters remove_comments;

in the configuration file. To retain comments that have semantic reason to be delivered to the browser, you may specify one more wildcard patterns using RetainComment directives. For example:

Apache:
ModPagespeedRetainComment " google_ad_section*"
Nginx:
pagespeed RetainComment " google_ad_section*";

Description

The remove_comments filter eliminates HTML comments, which are often used to document the code or to comment out experiments. Note that this directive applies only to HTML files. CSS comments are eliminated with the rewrite_css filter, and Javascript comments are eliminated with the rewrite_javascript filter.

The filter reduces the transfer size of HTML files by removing most HTML comments. Depending on the HTML file, this filter can significantly reduce the number of bytes transmitted on the network. Also note that the RetainComment directive currently only applies to HTML files -- the wildcard pattern is not currently used for retaining CSS or Javascript comments.

For example, if the HTML document looks like this:

<html>
<body>
<!-- Display the site logo here -->
<img src="logo.png">
<!-- Now show the page contents -->
<div>Some content here</div>
<!-- Apply IE-specific CSS -->
<!-- [if IE ]>
<link href="iecss.css" rel="stylesheet" type="text/css">
<![endif]-->
<!-- google_ad_section_end  -- retained due to RetainComment directive -->
</body>
</html>

Then PageSpeed, with the above directives specified, will rewrite it into:

<html>
<body>
<img src="logo.png">
<div>Some content here</div>
<!-- [if IE ]>
<link href="iecss.css" rel="stylesheet" type="text/css">
<![endif]-->
<!-- google_ad_section_end  -- retained due to RetainComment directive -->
</body>
</html>

Example

You can see the filter in action at www.modpagespeed.com on this example.

Notes

The "Remove Comments" filter is aware of Internet Explorer conditional comments and does not remove them.

Risks

This filter is low risk for most web pages. Some web pages use comments to embed data or JavaScript, in order to reduce the parse time of the HTML document. Such pages sould disable this filter, as it will remove the comments containing the data or JavaScript that is needed by these web pages.