mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
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
This commit is contained in:
parent
9d21914069
commit
12f7e1dc56
1 changed files with 18 additions and 6 deletions
24
app.py
24
app.py
|
|
@ -75,22 +75,31 @@ class BaseView(HTTPMethodView):
|
||||||
template = None
|
template = None
|
||||||
|
|
||||||
async def get(self, request, db_name, **kwargs):
|
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:
|
if should_redirect:
|
||||||
return response.redirect(should_redirect)
|
r = response.redirect(should_redirect)
|
||||||
|
r.headers['Link'] = '<{}>; rel=preload'.format(
|
||||||
|
should_redirect
|
||||||
|
)
|
||||||
|
return r
|
||||||
try:
|
try:
|
||||||
as_json = kwargs.pop('as_json')
|
as_json = kwargs.pop('as_json')
|
||||||
except KeyError:
|
except KeyError:
|
||||||
as_json = False
|
as_json = False
|
||||||
data = self.data(request, name, hash, **kwargs)
|
data = self.data(request, name, hash, **kwargs)
|
||||||
if as_json:
|
if as_json:
|
||||||
return response.json(data)
|
r = response.json(data)
|
||||||
else:
|
else:
|
||||||
return jinja.render(
|
r = jinja.render(
|
||||||
self.template,
|
self.template,
|
||||||
request,
|
request,
|
||||||
**data,
|
**data,
|
||||||
)
|
)
|
||||||
|
# Set far-future cache expiry
|
||||||
|
r.headers['Cache-Control'] = 'max-age={}'.format(
|
||||||
|
365 * 24 * 60 * 60
|
||||||
|
)
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
def sqlerrors(fn):
|
def sqlerrors(fn):
|
||||||
|
|
@ -157,7 +166,7 @@ app.add_route(DatabaseView.as_view(), '/<db_name:[^/]+?><as_json:(.json)?$>')
|
||||||
app.add_route(TableView.as_view(), '/<db_name:[^/]+>/<table:[^/]+?><as_json:(.json)?$>')
|
app.add_route(TableView.as_view(), '/<db_name:[^/]+>/<table:[^/]+?><as_json:(.json)?$>')
|
||||||
|
|
||||||
|
|
||||||
def resolve_db_name(db_name):
|
def resolve_db_name(db_name, **kwargs):
|
||||||
databases = ensure_build_metadata()
|
databases = ensure_build_metadata()
|
||||||
hash = None
|
hash = None
|
||||||
name = None
|
name = None
|
||||||
|
|
@ -178,9 +187,12 @@ def resolve_db_name(db_name):
|
||||||
raise NotFound()
|
raise NotFound()
|
||||||
expected = info['hash'][:7]
|
expected = info['hash'][:7]
|
||||||
if expected != hash:
|
if expected != hash:
|
||||||
return name, expected, '/{}-{}'.format(
|
should_redirect = '/{}-{}'.format(
|
||||||
name, expected,
|
name, expected,
|
||||||
)
|
)
|
||||||
|
if 'table' in kwargs:
|
||||||
|
should_redirect += '/' + kwargs['table']
|
||||||
|
return name, expected, should_redirect
|
||||||
return name, expected, None
|
return name, expected, None
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue