# HG changeset patch # User Julien Cristau # Date 1448039828 -3600 # Node ID e35d23686d1f501f1ad49b9de7037d1d57e9a48e # Parent 70b21cf8b45cbc5aa6ed400fcdd3d6ce9146a284 [web] set proper Cache-Control header for static files - set Cache-Control max-age to match Expires - use the max_age method (defaults to 1 week) instead of hardcoded 6 months diff -r 70b21cf8b45c -r e35d23686d1f web/test/unittest_views_staticcontrollers.py --- a/web/test/unittest_views_staticcontrollers.py Fri Nov 20 18:23:17 2015 +0100 +++ b/web/test/unittest_views_staticcontrollers.py Fri Nov 20 18:17:08 2015 +0100 @@ -70,6 +70,9 @@ with self._publish_static_files(fname) as req: self.assertEqual(200, req.status_out) self.assertIn('last-modified', req.headers_out) + self.assertIn('expires', req.headers_out) + self.assertEqual(req.get_response_header('cache-control'), + {'max-age': 604800}) next_headers = { 'if-modified-since': req.get_response_header('last-modified', raw=True), } diff -r 70b21cf8b45c -r e35d23686d1f web/views/staticcontrollers.py --- a/web/views/staticcontrollers.py Fri Nov 20 18:23:17 2015 +0100 +++ b/web/views/staticcontrollers.py Fri Nov 20 18:17:08 2015 +0100 @@ -66,9 +66,10 @@ if not debugmode: # XXX: Don't provide additional resource information to error responses # - # the HTTP RFC recommands not going further than 1 year ahead - expires = datetime.now() + timedelta(days=6*30) + # the HTTP RFC recommends not going further than 1 year ahead + expires = datetime.now() + timedelta(seconds=self.max_age(path)) self._cw.set_header('Expires', generateDateTime(mktime(expires.timetuple()))) + self._cw.set_header('Cache-Control', 'max-age=%s' % self.max_age(path)) # XXX system call to os.stats could be cached once and for all in # production mode (where static files are not expected to change)