Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

Eihrister
Copy link
Contributor

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.

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.
@OleVik
Copy link
Contributor

OleVik commented Aug 28, 2017

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?

@Eihrister
Copy link
Contributor Author

Eihrister commented Aug 28, 2017

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

type: number
min: 1
max: 9

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you have provided updates to system.yaml with translations for this field, however, those translations need to be added to the admin/languages/en.yaml file as the 'source' language file.

}
return false;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it not possible to perform this functionality with the RocketTheme\Toolbox\File\File class? We use this class throughout Grav to read/save files. That way it would be more consistent an not need all the manual low-level PHP file calls.

@jimblue
Copy link

jimblue commented Feb 10, 2018

Any update about this PR @Eihrister ?

@gstrauss
Copy link

gstrauss commented Mar 3, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants