Search code examples
htmlperformancenginxhttp-compression

How to remove white space in web page before serving to client?


I am deploying a web application and I am able to compress CSS and js in my web application using page speed module in nginx/apache, but couldn't able to remove HTML white space.

Does anyone has done this before, I have seen this implementation in a major website such as LinkedIn Facebook, and Google.

Does removing white space in HTML add performance boost? As per my understanding removing whitespace reduces some extra bytes.

Here is an example of a condensed version of HTML page from google.

enter image description here


Solution

  • Does removing white space in HTML add performance boost?

    Unlikely will you benefit much given that gzip is enabled for your site. The more you save during such a stripping phase the less benefit you would gain from gzipping and vice versa.

    BTW, mod_pagespeed has the collapsible module doing what you're asking.

    If mod_pagespeed doesn't meet your requirements

    Multiple options

    • if you have a static html pages that just need to be returned to the user it's quite easy to do in offline mode
    • you can also do it at backend level using your framework batteries. I.e. in case you use a python framework this module could be used

    django-html is an HTML minifier for Python, with full support for HTML 5. It supports Django, Flask and many other Python web frameworks. It also provides a command line tool, that can be used for static websites or deployment scripts.

    • if none of the options above are viable use some 3rd party modules doing it at nginx level

    • depending on your needs you might also consider services like cloudflare