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

Serve pre-compressed files with StaticFileHandler #2957

Open
ChouJoe opened this issue Nov 20, 2020 · 1 comment
Open

Serve pre-compressed files with StaticFileHandler #2957

ChouJoe opened this issue Nov 20, 2020 · 1 comment
Labels

Comments

@ChouJoe
Copy link

ChouJoe commented Nov 20, 2020

in order to reduce the size of static resources,i use webpack to compress the static file, for example, compressing "chunk-vendors.665e9203.js" like this:‘’chunk-vendors.665e9203.js.gz‘’. but i do not know how to set the tornado to return the gz file in the hope?

@bdarnell
Copy link
Member

This kind of works, but not exactly how you're expecting. If you upload foo.js.gz, Tornado will serve that compressed file at /static/foo.js.gz as an application/x-gzip content-type. That's not going to work for most browser assets. Instead, you need to send the file with content-type text/javascript and content-encoding gzip. StaticFileHandler can't do this for files that are compressed on disk, although it would be a nice feature to add.

Instead, if you put the file on disk uncompressed and pass compress_response=True to your Application constructor, Tornado will transparently compress the file and serve it up with the correct encoding to reduce the data transferred (but it will recompress the file every time it's requested, so it's not ideal for cpu usage).

You can also consider serving your static files from somewhere other than Tornado. Tornado is a relatively expensive way to serve static files so pairing it with e.g. nginx for production environments is a good solution, described in the user's guide

@bdarnell bdarnell changed the title How to upload compressed static files Serve pre-compressed files with StaticFileHandler Nov 29, 2020
@bdarnell bdarnell added the web label Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants