Search code examples
cssperformancecompressiongzipdeflate

Gzip static files previously instead of on the fly


If instead of using mod_deflate or mod_gzip I would manually gzip a css or js file obtaining something like:

base.css.gz

And I rename it as:

base.css

And then load it from an HTML page, would it work?

This could be useful on environments with very limited memory and resources, like for examples wireless access points in mesh networks.

I also wanted to ask if it would have sense to do it on normal websites to save resources of the server's cpu? At the moment I use mod_deflate, I think with this method the contents gets gzipped at every request on the fly, is it so? Isn't that a bit of waste of resources?


Solution

  • I answer to myself because no one addressed my question.

    It is possible to serve manually gzipped files, for example css, but they need to be served by the web-server with the correct content-type and compression headers, just loading a gzipped file from a <link> or <javscript> tag won't work.

    Here's an example with php:

    <?php
    header("Content-Encoding: gzip");
    header("Content-Type: text/css");
    echo file_get_contents("base.min.css.gz");
    exit(0);
    
    // no closing tag to prevent occasionally printing any chars