From 5c6e78b9c14a41a3fff9c77b1d952ced962c1622 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 22 Apr 2018 12:17:31 +0200 Subject: [PATCH] api: ensure /feed is easily cacheable by issuing etag/last-modified headers --- isso/views/comments.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/isso/views/comments.py b/isso/views/comments.py index ddc2b09..120fef3 100644 --- a/isso/views/comments.py +++ b/isso/views/comments.py @@ -950,7 +950,16 @@ class API(object): ET.ElementTree(feed).write(output, encoding='utf-8', xml_declaration=True) - return XML(output.getvalue(), 200) + response = XML(output.getvalue(), 200) + + # Add an etag/last-modified value for caching purpose + if comment0 is None: + response.set_etag('empty') + response.last_modified = 0 + else: + response.set_etag('{tid}-{id}'.format(**comment0)) + response.last_modified = comment0['modified'] or comment0['created'] + return response.make_conditional(request) def preview(self, environment, request): data = request.get_json()