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
This commit is contained in:
Simon Willison 2017-12-08 19:10:09 -08:00
commit 16dfccb1c5
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
2 changed files with 14 additions and 2 deletions

View file

@ -9,6 +9,7 @@ import sqlite3
from pathlib import Path from pathlib import Path
from concurrent import futures from concurrent import futures
import asyncio import asyncio
import os
import threading import threading
import urllib.parse import urllib.parse
import json import json
@ -45,7 +46,11 @@ connections = threading.local()
class RenderMixin(HTTPMethodView): class RenderMixin(HTTPMethodView):
def render(self, templates, **context): def render(self, templates, **context):
return response.html( 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.template_dir = template_dir
self.static_mounts = static_mounts or [] 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): def get_canned_query(self, database_name, query_name):
query = self.metadata.get( query = self.metadata.get(
'databases', {} 'databases', {}

View file

@ -2,7 +2,7 @@
<html> <html>
<head> <head>
<title>{% block title %}{% endblock %}</title> <title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="/-/static/app.css"> <link rel="stylesheet" href="/-/static/app.css?{{ app_css_hash }}">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
{% for url in extra_css_urls %} {% for url in extra_css_urls %}
<link rel="stylesheet" href="{{ url.url }}"{% if url.sri %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}> <link rel="stylesheet" href="{{ url.url }}"{% if url.sri %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}>