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: preon ordinary elements, can change appearance; the filter sees the markup, not the stylesheet. Test such pages before enabling. -
Verify with the
X-Mod-Pagespeedresponse header and a?PageSpeedFilters=-collapse_whitespacecomparison; Is it working? has the steps.
Configuration
Enable it in the module configuration, at server, virtual-host or location scope:
Apache
ModPagespeedEnableFilters collapse_whitespacenginx
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%
Frequently asked questions
- What does the collapse_whitespace filter do?
collapse_whitespaceshrinks 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_whitespaceis not in CoreFilters, the default RewriteLevel; it runs only when you enable it by name withEnableFilters. - How do I enable collapse_whitespace on Apache and nginx?
- Add
ModPagespeedEnableFilters collapse_whitespaceon Apache orpagespeed EnableFilters collapse_whitespace;on nginx. On IIS, addpagespeed EnableFilters collapse_whitespaceto pagespeed.config.
Related filters
- add_base_tag — Add base tag
- add_head — Add head
- add_ids — Add IDs
- add_instrumentation — Add instrumentation
- combine_heads — Combine heads
- compute_statistics — Compute HTML statistics
- convert_meta_tags — Convert meta tags
- debug — Debug comments
This page is drawn from the collapse_whitespace entry on HTML filters. Every filter in one table: PageSpeed filters.