From 12f7e1dc5624d14f644abead18bd90b420b6d97e Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Mon, 23 Oct 2017 19:36:44 -0700 Subject: [PATCH] Hashed URLs now have far-future cache expiry Since the URL now includes a hash of the database, we can return a Cache- Control: max-age=31536000 header for every response. The exception is our 302 redirects. These we now serve with a Link: header that tells any HTTP/2 server-push aware fronting proxies (such as Cloudfront) to push the target of the redirect. Closes #4 --- app.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/app.py b/app.py index 66555ccf..26af1c98 100644 --- a/app.py +++ b/app.py @@ -75,22 +75,31 @@ class BaseView(HTTPMethodView): template = None async def get(self, request, db_name, **kwargs): - name, hash, should_redirect = resolve_db_name(db_name) + name, hash, should_redirect = resolve_db_name(db_name, **kwargs) if should_redirect: - return response.redirect(should_redirect) + r = response.redirect(should_redirect) + r.headers['Link'] = '<{}>; rel=preload'.format( + should_redirect + ) + return r try: as_json = kwargs.pop('as_json') except KeyError: as_json = False data = self.data(request, name, hash, **kwargs) if as_json: - return response.json(data) + r = response.json(data) else: - return jinja.render( + r = jinja.render( self.template, request, **data, ) + # Set far-future cache expiry + r.headers['Cache-Control'] = 'max-age={}'.format( + 365 * 24 * 60 * 60 + ) + return r def sqlerrors(fn): @@ -157,7 +166,7 @@ app.add_route(DatabaseView.as_view(), '/') app.add_route(TableView.as_view(), '//') -def resolve_db_name(db_name): +def resolve_db_name(db_name, **kwargs): databases = ensure_build_metadata() hash = None name = None @@ -178,9 +187,12 @@ def resolve_db_name(db_name): raise NotFound() expected = info['hash'][:7] if expected != hash: - return name, expected, '/{}-{}'.format( + should_redirect = '/{}-{}'.format( name, expected, ) + if 'table' in kwargs: + should_redirect += '/' + kwargs['table'] + return name, expected, should_redirect return name, expected, None