Skip to main content

filter · HTML filters

Remove comments (remove_comments)

Category:
HTML
CoreFilters:
No
OptimizeForBandwidth:
No
Risk:
Generally safe

remove_comments deletes HTML comments (<!-- ... -->) from the served page, cutting bytes that only developers read. The document tree is unchanged apart from the removed nodes. Live demo: remove_comments.

How it works

When it helps and when it does not

It helps on pages whose templates carry heavy commentary: build markers, TODO notes, and section labels add up on large documents. It does nothing for pages that ship few comments, and it must not remove comments that carry a function. Comments you are obliged or willing to ship stay behind a RetainComment wildcard, and IE conditional comments (<!--[if IE]> ... <![endif]-->) are parsed as directives rather than comments, so they always survive.

How it decides

Every comment node is dropped unless its text matches one of the configured RetainComment wildcard patterns. There is no size threshold and no content analysis beyond the pattern match.

When to use it

  • Not in CoreFilters: it runs only when you enable it by name.
  • Risk rating on these docs: Generally safe.

Risks

  • Copyright or license notices that must ship with the page need a RetainComment entry before the filter goes on.

  • A rare third-party snippet that reads the page’s own comments breaks; retain its marker comment or disable the filter for that path. Verify with the X-Mod-Pagespeed header and ?PageSpeedFilters=-remove_comments; Is it working? has the steps.

Configuration

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

Apache

ModPagespeedEnableFilters remove_comments

nginx

pagespeed EnableFilters remove_comments;

IIS (pagespeed.config)

pagespeed EnableFilters remove_comments
<!-- before -->
<!-- TODO: replace with the component version -->
<div class="banner">Sale ends Friday</div>

<!-- after -->
<div class="banner">Sale ends Friday</div>
# Apache
ModPagespeedEnableFilters remove_comments
ModPagespeedRetainComment "*copyright*"
# Nginx
pagespeed EnableFilters remove_comments;
pagespeed RetainComment "*copyright*";

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

Live example

Strips HTML comments from the markup.

Measured on the demo page, original against optimized:

  • HTML size

    439 B → 283 B −36%

See the before and after, with the source diff →

Frequently asked questions

What does the remove_comments filter do?
remove_comments deletes HTML comments (<!-- ... -->) from the served page, cutting bytes that only developers read. The document tree is unchanged apart from the removed nodes. Live demo: remove_comments.
Is remove_comments enabled by default?
No. remove_comments is not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name with EnableFilters.
How do I enable remove_comments on Apache and nginx?
Add ModPagespeedEnableFilters remove_comments on Apache or pagespeed EnableFilters remove_comments; on nginx. On IIS, add pagespeed EnableFilters remove_comments to pagespeed.config.

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

Search