grav icon indicating copy to clipboard operation
grav copied to clipboard

Precompression for (pipelined) assets (nginx::gzip_static support).

Open Eihrister opened this issue 7 years ago • 4 comments

Compressing resources on-the-fly adds CPU-load and latency (wait for the compression to be done) every time a resource is served. nginx offers another way of doing things, and it's called gzip_static.

http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html

If you enable gzip_static, nginx will look for $filename.gz and serve that directly, so no extra CPU-cost or latency is added to your requests, speeding up the serving of your website.

This feature adds support to directly compress asset files when they are written to disk by the Grav\Common\Assets class. Using this feature you can also specify the compression level (default to the maximum of 9).

Since you only have to compress every resource only once, using the maximum compression level is an ideal default.

Eihrister avatar Aug 24 '17 16:08 Eihrister

This seems to be specific for Nginx? Whilst handy, it would be superfluous for all other webservers. Also, have you tested it with other webservers to ensure that the settings causes no issue with them?

OleVik avatar Aug 28 '17 17:08 OleVik

Hi Ole,

For other webservers it will not cause problems, as it is merely creating an additional file.

nginx supports it by default without requiring any extra work. However, support is not limited to nginx. Other webservers can also make use of it, if configured accordingly. Some searching can find some ways to set it up:

Lighttpd: http://redmine.lighttpd.net/issues/611 Apache: https://stackoverflow.com/questions/16883241/how-to-host-static-content-pre-compressed-in-apache

I'm sure it could work in any webserver software as long as you configure it properly.

If I could've done it in a plug-in, I would've. I have purposefully avoided using "nginx" in the code / descriptions of things, as it is what it is: precompression of files. I understand that it might seem unusual to put this in the core, but to me it didn't really seem to have another place. It's either upon creation of said resources, or doing everything by hand externally out of Grav, not knowing when asset files are created.

Daniël

Eihrister avatar Aug 28 '17 22:08 Eihrister

Any update about this PR @Eihrister ?

jimblue avatar Feb 10 '18 04:02 jimblue

lighttpd mod_deflate can compress and save the compressed result in a cache if deflate.cache-dir is set in lighttpd.conf. Then, subsequent requests (with Accept-Encoding) will receive the already-compressed file from the cache.

If you pre-compress the files yourself (gzip, brotli, zstd, ...), you can use lighttpd mod_magnet to run a small lua script for content negotiation, e.g. content-negotiation.lua

gstrauss avatar Mar 03 '22 07:03 gstrauss