I wanted to improve the problem that bootstrap.min.css
was slow to load, so I wondered how I could send css with gzip ... and I found out that it was successful, so I'll take note of it.
For the time being, let's create a new View.
This time we will send a Gzip compressed version of bootstrap.min.css
, so create a URI with the name /bootstrap.gz.css
.
@app.route("/bootstrap.gz.css")
Then load the Gzip-compressed CSS file. (Probably it's okay to read a regular CSS file and compress it with the gzip
module)
fp = open("static/css/bootstrap.min.css.gz")
content = fp.read()
fp.close()
This is no good, so let's add some effort to the header. This time it is compressed with GZIP, so add the information to the Content-Encoding
header.
res = make_response(content)
res.headers["Content-Type"] = "text/css"
res.headers["Content-Encoding"] = "gzip"
Putting these together gives the following code. This time I sent a compressed version of CSS, but I think you can do the same with Javascript and images ...!
@view.route("/bootstrap.gz.css")
def Bootstrap():
fp = open("static/css/bootstrap.min.css.gz")
content = fp.read()
fp.close()
res = make_response(content)
res.headers["Content-Type"] = "text/css"
res.headers["Content-Encoding"] = "gzip"
return res
Please refer to it!
Recommended Posts