Skip to main content

filter · HTML filters

Collapse whitespace (collapse_whitespace)

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

collapse_whitespace shrinks the HTML payload by folding every run of whitespace (spaces, tabs, carriage returns, newlines) down to a single character. The markup keeps its structure; only the formatting bytes go. Live demo: collapse_whitespace.

How it works

When it helps and when it does not

The saving scales with how much pretty-printing the templates do: indented server-side templates carry many collapsible bytes, already-compacted HTML almost none. Gzip and Brotli compress whitespace runs well, so the on-the-wire saving is much smaller than the source saving suggests. The filter earns most where HTML is served uncompressed, and least on an already minified template. Not a CoreFilter; enable it by name.

How it decides

A whitespace run that contains a newline collapses to a newline, any other run to a single space; a run never collapses to nothing, so inline elements keep their separating space and text layout is preserved. Content inside pre, code, script, style, and textarea is never touched, because whitespace is meaningful there.

When to use it

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

Risks

  • Pages whose rendering depends on whitespace-sensitive CSS, such as white-space: pre on ordinary elements, can change appearance; the filter sees the markup, not the stylesheet. Test such pages before enabling.

  • Verify with the X-Mod-Pagespeed response header and a ?PageSpeedFilters=-collapse_whitespace comparison; Is it working? has the steps.

Configuration

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

Apache

ModPagespeedEnableFilters collapse_whitespace

nginx

pagespeed EnableFilters collapse_whitespace;

IIS (pagespeed.config)

pagespeed EnableFilters collapse_whitespace
<!-- before -->
<ul class="nav">
    <li>  <a href="/a">Alpha</a>  </li>
    <li>  <a href="/b">Beta</a>   </li>
</ul>

<!-- after: each run that held a newline keeps one newline -->
<ul class="nav">
<li> <a href="/a">Alpha</a> </li>
<li> <a href="/b">Beta</a> </li>
</ul>

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

Live example

Removes redundant whitespace from the HTML.

Measured on the demo page, original against optimized:

  • HTML size

    274 B → 232 B −15%

See the before and after, with the source diff →

Frequently asked questions

What does the collapse_whitespace filter do?
collapse_whitespace shrinks the HTML payload by folding every run of whitespace (spaces, tabs, carriage returns, newlines) down to a single character. The markup keeps its structure; only the formatting bytes go. Live demo: collapse_whitespace.
Is collapse_whitespace enabled by default?
No. collapse_whitespace is not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name with EnableFilters.
How do I enable collapse_whitespace on Apache and nginx?
Add ModPagespeedEnableFilters collapse_whitespace on Apache or pagespeed EnableFilters collapse_whitespace; on nginx. On IIS, add pagespeed EnableFilters collapse_whitespace to pagespeed.config.

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

Search