From 16dfccb1c569359fa2a03639ada233e7661156af Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 8 Dec 2017 19:10:09 -0800 Subject: [PATCH] Include sha1 hash in /static/app.css URL This means that when Datasette's CSS changes the new CSS will be loaded even though browsers may have cached the previous version. Closes #154 --- datasette/app.py | 14 +++++++++++++- datasette/templates/base.html | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/datasette/app.py b/datasette/app.py index 7cd56cda..8312c10c 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -9,6 +9,7 @@ import sqlite3 from pathlib import Path from concurrent import futures import asyncio +import os import threading import urllib.parse import json @@ -45,7 +46,11 @@ connections = threading.local() class RenderMixin(HTTPMethodView): def render(self, templates, **context): return response.html( - self.jinja_env.select_template(templates).render(**context) + self.jinja_env.select_template(templates).render({ + **context, **{ + 'app_css_hash': self.ds.app_css_hash(), + } + }) ) @@ -835,6 +840,13 @@ class Datasette: self.template_dir = template_dir self.static_mounts = static_mounts or [] + def app_css_hash(self): + if not hasattr(self, '_app_css_hash'): + self._app_css_hash = hashlib.sha1( + open(os.path.join(app_root, 'datasette/static/app.css')).read().encode('utf8') + ).hexdigest()[:6] + return self._app_css_hash + def get_canned_query(self, database_name, query_name): query = self.metadata.get( 'databases', {} diff --git a/datasette/templates/base.html b/datasette/templates/base.html index 0c21af0e..159d0f91 100644 --- a/datasette/templates/base.html +++ b/datasette/templates/base.html @@ -2,7 +2,7 @@ {% block title %}{% endblock %} - + {% for url in extra_css_urls %}