changeset 1739:6776bf3fe376

use cache control headers for +template to have less http requests
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 26 Aug 2012 15:37:14 +0200
parents 7d258c74032d
children 87bed5e28b0b
files MoinMoin/apps/frontend/views.py
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/apps/frontend/views.py	Sat Aug 25 11:55:59 2012 +0200
+++ b/MoinMoin/apps/frontend/views.py	Sun Aug 26 15:37:14 2012 +0200
@@ -1904,10 +1904,29 @@
 def template(filename):
     """
     serve a rendered template from <filename>
+
+    used for (but not limited to) translation of javascript / css / html
     """
     content = render_template(filename)
     ct, enc = mimetypes.guess_type(filename)
     response = make_response((content, 200, {'content-type': ct or 'text/plain;charset=utf-8'}))
+    if ct in ['application/javascript', 'text/css', 'text/html', ]:
+        # this is assuming that:
+        # * js / css / html templates rarely change (maybe just on sw updates)
+        # * we are using templates for these to translate them, translations rarely change
+        # * the rendered template output is just specific per user but does not change in time
+        #   or by other circumstances
+        cache_timeout = 24 * 3600  # 1 day
+        is_public = False  # expanded template may be different for each user (translation)
+    else:
+        # safe defaults:
+        cache_timeout = None
+        is_public = False
+    if cache_timeout is not None:
+        # set some cache control headers, so browsers do not request this again and again:
+        response.cache_control.public = is_public
+        response.cache_control.max_age = cache_timeout
+        response.expires = int(time.time() + cache_timeout)
     return response