cowboy icon indicating copy to clipboard operation
cowboy copied to clipboard

How to gzip static files?

Open gotthardp opened this issue 6 years ago • 7 comments

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?

gotthardp avatar Nov 07 '17 18:11 gotthardp

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.

essen avatar Nov 07 '17 19:11 essen

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.

gotthardp avatar Nov 11 '17 11:11 gotthardp

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

yushli avatar Nov 11 '17 14:11 yushli

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.

essen avatar Dec 12 '17 15:12 essen

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?

etxemag75 avatar Feb 02 '19 17:02 etxemag75

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.

essen avatar Feb 02 '19 18:02 essen

@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.

gotthardp avatar Feb 02 '19 18:02 gotthardp