From a9b9a7c5c69e6582f49f98a4f6dacc2a02f5863b Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Tue, 3 Jan 2012 05:07:31 +0800 Subject: [PATCH] Improve browser cache behavior with tiles (cache more often) --- web/js/dynmaputils.js | 2 +- web/js/map.js | 31 +++++++++++-------------------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/web/js/dynmaputils.js b/web/js/dynmaputils.js index e4dfb893..fbdeb58f 100644 --- a/web/js/dynmaputils.js +++ b/web/js/dynmaputils.js @@ -69,7 +69,7 @@ var DynmapTileLayer = L.TileLayer.extend({ var tileName = this.getTileName(tilePoint, zoom); var url = this._cachedTileUrls[tileName]; if (!url) { - this._cachedTileUrls[tileName] = url = this.options.dynmap.getTileUrl(tileName) + '?' + new Date().getUTCMilliseconds(); + this._cachedTileUrls[tileName] = url = this.options.dynmap.getTileUrl(tileName); } return url; }, diff --git a/web/js/map.js b/web/js/map.js index 1798a662..c3dbaa49 100644 --- a/web/js/map.js +++ b/web/js/map.js @@ -412,7 +412,12 @@ DynMap.prototype = { var prevzoom = me.map.getZoom(); var prevworld = me.world; - + + if(worldChanged) { // World changed - purge URL cache (tile updates unreported for other worlds) + me.registeredTiles = []; + me.inittime = new Date().getTime(); + } + if(worldChanged && me.world) { me.world.lastcenter = me.maptype.getProjection().fromLatLngToLocation(me.map.getCenter(), 64); } @@ -623,30 +628,16 @@ DynMap.prototype = { var me = this; var tile = me.registeredTiles[tileName]; - if(tile) { - return me.options.tileUrl + me.world.name + '/' + tileName + '?' + tile.lastseen; - } else { - return me.options.tileUrl + me.world.name + '/' + tileName + '?' + me.inittime; /* Browser cache fix on reload */ + if(tile == null) { + tile = this.registeredTiles[tileName] = me.options.tileUrl + me.world.name + '/' + tileName + '?' + me.inittime; } - }, - registerTile: function(mapType, tileName, tile) { - this.registeredTiles[tileName] = { - tileElement: tile, - mapType: mapType, - lastseen: '0' - }; - }, - unregisterTile: function(mapType, tileName) { - delete this.registeredTiles[tileName]; + return tile; }, onTileUpdated: function(tileName,timestamp) { var me = this; - var tile = this.registeredTiles[tileName]; + + this.registeredTiles[tileName] = me.options.tileUrl + me.world.name + '/' + tileName + '?' + timestamp; - if (tile) { - tile.lastseen = timestamp; - tile.mapType.onTileUpdated(tile.tileElement, tileName); - } me.maptype.updateNamedTile(tileName); }, addPlayer: function(update) {