My server is hosting an application which preloads >100 thumbnail images on each page load. The thumbnail images do not change often.
I am trying to make consecutive thumbnail loads faster by using Cache-Control: public,max-age=MANY_SECONDS
where MANY_SECONDS
is up to a year.
The thumbnails are requested via a Flask endpoint that looks like this:
@app.route('/api/thumbnail/<string:filename>/<int:max_width>/<int:max_height>/')
def api_thumbnail(filename, max_width, max_height):
thumb_filename_template = '{filename}_{orig_digest}-{dimensions}-thumb.png'
# [...] PIL thumbnailing logic [...]
key = Key(thumb_filename_template.format(
filename=filename,
orig_digest=md5_hexdigest_of_original_image,
dimensions='x'.join([max_width, max_height])
))
# Upload the thumbnail image to Amazon S3
key.set_contents_from_string(local_thumbnail_file.read())
redirect(key.generate_url(0, query_auth=False), code=301)
I set the Cache-Control
header to public,max-age=MANY_SECONDS
for all the *-thumb.png
keys, still Firefox fires a request against /api/thumbnail/...
and gets the 301, then a 304 from Amazon S3.
I'm under the impression that 301 responses are cached seemingly for an eternity, and the Cache-Control
headers for the thumbnail files served from Amazon S3 should allow Firefox to cache the thumbnail files locally for up to a year.
All these thumbs × 2 requests are really an overhead. I want them cached for an eternity.
My solution was to use a HTML5 manifest file.