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

Gzip compressed files #1123

Open
kno10 opened this issue Mar 15, 2023 · 1 comment
Open

Gzip compressed files #1123

kno10 opened this issue Mar 15, 2023 · 1 comment

Comments

@kno10
Copy link

kno10 commented Mar 15, 2023

I tried using a .svg.gz file with Lektor, but it would not correctly serve this file.

The file would be served with headers

HTTP/1.1 200 OK
Server: Werkzeug/2.2.2 Python/3.11.2
Date: Wed, 15 Mar 2023 14:07:54 GMT
Content-Type: image/svg+xml; charset=utf-8
Cache-Control: no-store, public
Content-Length: 10195
ETag: "lektor-1678845790.2140253-10195-362676959"
Connection: close

but the file is still gzip compressed, i.e., without

Content-Encoding: gzip

This causes the browsers to attempt to parse the compressed data as XML, which obviously fails.

Of course I can just use .svg, and have my main web server do on the fly compression. The main reason I was looking into storing the images as .svg.gz was to be able compare transfer sizes for different formats and decide whether to store the images as .svg, .png or .webp. The .svg.gz size more accurately reflects when it is beneficial to switch to bitmap formats.

@dairiki
Copy link
Contributor

dairiki commented Mar 15, 2023

This seems like a bit of an edge case.

I agree that the current behavior is not correct, but I'm not sure that I agree with you on what the correct behavior should be.

If the contents of foo.svg.gz were being served at URL .../foo.svg then clearly Content-Type: image/svg plus Content-Encoding: gzip is appropriate. In this case, though, the webserver is serving a request for, e.g., foo.svg.gz, not for foo.svg. I would think the correct response would be Content-Type: application/gzip with no Content-Encoding. (What if the file in question were foo.tar.gz?)

FWIW, Werkzeug's send_file (which is what is used by Flask to server files from the app's static dir) does appear to behave as you suggest.


As to your use case, specifically: if one wants to know the compressed size of a file, that can, of course, be computed without actually storing the file in compressed form. E.g.

    gzip -c foo.svg | wc -c

or similar.

If, for optimization purposes, one wants to store pre-compressed data, I think that's more of a deployment issue. E.g. when using nginx's gzip_static module one would normally want both to deploy both a foo.svg (for serving clients that don't support gzip encoding) and foo.svg.gz (for those that do).

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

No branches or pull requests

2 participants