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
pull/178/head
Simon Willison 2017-12-08 19:10:09 -08:00
rodzic 446f4b8322
commit 16dfccb1c5
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 17E2DEA2588B7F52
2 zmienionych plików z 14 dodań i 2 usunięć

Wyświetl plik

@ -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', {}

Wyświetl plik

@ -2,7 +2,7 @@
<html>
<head>
<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">
{% for url in extra_css_urls %}
<link rel="stylesheet" href="{{ url.url }}"{% if url.sri %} integrity="{{ url.sri }}" crossorigin="anonymous"{% endif %}>