9

Have been reading flask docs in python and building a local website.

Just performed a speed test on my website deployed on openshift with this tool here:-

The analysis report says that my site is not caching static resources. After googling this for all my worth I gather that:-

  • It has something to do with headers.
  • Cache copies are kept in the client machine and also servers between the client and website.

My Question

  • Am I to include expire and tags in the html section? Or in the HTTP header section?

  • If in the HTTP header section how do I do this?

If I have missed something in the docs please let me know.

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
arjoonn
  • 960
  • 1
  • 9
  • 20

2 Answers2

7

Either use 'SEND_FILE_MAX_AGE_DEFAULT' or look into webassests http://webassets.readthedocs.org/en/latest/

Similar question asked here. Flask static file Cache-Control

Community
  • 1
  • 1
PsyKzz
  • 740
  • 4
  • 14
  • I have seen the mentioned question. The problem there is that the cache is set to a very big default value. My problem is that the tests show that my website does not cache static elements.That answers my basic requirement of completing the task. I would also like to know if the tags are added in the HTTP or HTML doc headers. Thus the new question fo ra consolodated future resource. – arjoonn Jul 10 '14 at 08:12
  • HTTP headers. The Response headers. – PsyKzz Jul 10 '14 at 11:18
  • Right then. But why is the content not cached for long? I have further checked at http://tools.pingdom.com/fpt/ – arjoonn Jul 10 '14 at 11:29
0

I had this problem and couldn't find an answer online that worked for me.

Then I realised that my static files are not being served from Flask at all! Flask only generates my HTML. The static files are served directly by my web server (Apache in my case, yours might be Nginx or something else).

Instructions for Apache:

First install relevant modules

sudo a2enmod expires
sudo a2enmod headers

Then add something like this to your .htaccess file:

# Expire headers    
<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"
</ifModule>

# Cache-Control Headers
<ifModule mod_headers.c>

  <filesMatch "\.(ico|jpe?g|png|gif)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>

  <filesMatch "\.(css|js)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>

</ifModule>
# END Cache-Control Headers

Apache config modified from More details on how to configure it in the Apache manual.

brianc
  • 273
  • 3
  • 8