Parcourir la source

Replaced current time querystring on GeoJSON path by the JSON mtime fixes #13

Revert "Add current timestamp as query string to GeoJSON path to bypass browser cache. #13"

This reverts commit dc29fbf29f65e4b3ed52dd1947218be3a5e9c51c.
Jocelyn Delande il y a 9 ans
Parent
commit
5ccc500851
3 fichiers modifiés avec 15 ajouts et 6 suppressions
  1. 1 2
      assets/form.js
  2. 1 2
      assets/map.js
  3. 13 2
      backend.py

+ 1 - 2
assets/form.js

@@ -41,8 +41,7 @@ $( document ).ready(function() {
 
     // Display tiny circles on existing public points
     var GeoJsonPath = $('#map').data('json');
-    // We add current time to bypass browser cache
-    $.getJSON(GeoJsonPath + '?time=' + Date.now(), function(data){
+    $.getJSON(GeoJsonPath, function(data){
         var featureLayer = L.geoJson(data, {
             pointToLayer: function (feature, latlng) {
                 return L.circleMarker(latlng, {color: '#00B300'});

+ 1 - 2
assets/map.js

@@ -41,8 +41,7 @@ $( document ).ready(function() {
 
     // Get JSON
     var GeoJsonPath = $('#map').data('json')
-    // We add current time to bypass browser cache
-    $.getJSON(GeoJsonPath + '?time=' + Date.now(), function(data){
+    $.getJSON(GeoJsonPath, function(data){
 
         function buildPopupContent(feature, layer) {
             feature.properties.popupContent = '';

+ 13 - 2
backend.py

@@ -95,6 +95,17 @@ def escape(s):
      else:
           return s
 
+def json_url(json_filename):
+    """ Returns (relative) json URL with a querystring mentioning file mtime
+
+    That's to prevent too much browser caching (mtime will change on file
+    generation, changing querystring) while letting browser doing relevant
+    caching.
+    """
+    file_path = join(dirname(__file__), 'json/', json_filename)
+    mtime = os.path.getmtime(file_path)
+    return '{}?mtime={}'.format(json_filename, mtime)
+
 def save_to_db(db, dic):
     # SQLite is picky about encoding else
     tosave = {bytes(k):escape(v.decode('utf-8')) if isinstance(v,str)
@@ -172,7 +183,7 @@ def submit_wifi_form():
 
     if errors:
         return template('wifi-form', errors=errors, data=request.forms,
-                        orientations=ORIENTATIONS, geojson=GEOJSON_NAME)
+                        orientations=ORIENTATIONS, geojson=json_url(GEOJSON_NAME))
     else:
         d = request.forms
         save_to_db(DB, {
@@ -229,7 +240,7 @@ Results Map
 
 @app.route('/map')
 def public_map():
-    return template('map', geojson=GEOJSON_NAME)
+    return template('map', geojson=json_url(GEOJSON_NAME))
 
 @app.route('/public.json')
 def public_geojson():