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

How to gzip static files? #1237

Open
gotthardp opened this issue Nov 7, 2017 · 7 comments
Open

How to gzip static files? #1237

gotthardp opened this issue Nov 7, 2017 · 7 comments

Comments

@gotthardp
Copy link

Shall compression work also for files served by cowboy_static? The cowboy_compress_h.erl:98 says We do not compress sendfile bodies. Is this related to what cowboy_static does? (Because in my environment REST communication is compressed, but static files are not.) Is there a way to gzip also the static files too?

@essen
Copy link
Member

essen commented Nov 7, 2017

It's not currently possible. I'm not 100% sure how it should be done inside Cowboy itself. Outside, you can easily extend cowboy_static or write a stream handler to swap uncompressed with compressed files.

@gotthardp
Copy link
Author

Thanks. Another solution might be to extend the compress handler and when {sendfile, 0, Len, "/path/file"} is received to check if "/path/file.gz" exists too. If so, then replace the file and add the content-encoding header. This would be consistent with how the .gzip tool works.
Would you accept a Pull Request contributing a (your preferred) solution to this, or you want to keep it external? I feel this might be useful for others too and implementing this externally might duplicate (waste) effort.

@yushli
Copy link

yushli commented Nov 11, 2017

I think adding this feature builtin would be nice. It is good to have official support to enable that.

@essen
Copy link
Member

essen commented Dec 12, 2017

I don't disagree this can be useful to many people, but a good solution is complex especially once I add better support for range requests, for example the proposed solution here would not work anymore.

I believe if something is to be done it should be done either by modifying the request coming in (rewrite the path and then add the correct content-encoding header in the response) or directly in cowboy_static.

@etxemag75
Copy link

Even if the websites are fast, web performance scores are always suffering in tools such as http://webpagetest.org or gtmetrix.com etc from now serving compressed files.

If it is easy to write a stream handler to swap, would it be possible for you add it (similar as cowboy_compress_h.erl), to swap uncompressed with compressed in the cowboy framework codebase?

@essen
Copy link
Member

essen commented Feb 2, 2019

Problem is not so much doing it and including it, but rather doing it in a way that makes all the parts fit well together. Right now it seems there's an increasing need for some sort of rewrite stream handler, and this is only one use case of that would be handler.

@gotthardp
Copy link
Author

@etxemag75 Some time ago I wrote an alternate static handler that can serve gzipped files. It also supports authentication (which is another feature the current static handler is missing). I don't claim it is perfect, but it works: https://github.com/gotthardp/lorawan-server/blob/master/src/lorawan_admin_static.erl Feel free to adapt it to your needs.

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

No branches or pull requests

4 participants