3

I use OpenLayers.Layer.XYZ to display tiles from TileStache server without using the cache option of the server. However, I notice that the tiles stay in cache (probably the browser cache) until the whole page is refreshed with Ctrl-F5.

If I want to redraw the XYZ layer, it does not works because the map use the browser cache. Is there a way to force the map to not use that cache? So could refresh the layer by asking fresh tiles from the server.

var map = new OpenLayers.Map('map',
          { projection: new OpenLayers.Projection("EPSG:3857"),
            numZoomLevels: 20 });

var tiledLayer = new OpenLayers.Layer.XYZ('TMS',
                "{{ tmsURL }}1.0/layer/{{ shapefile.id }}/${z}/${x}/${y}.png?"+ new Date().getTime());

map.addLayer(tiledLayer);
tiledLayer.redraw(true); //does not make new GET requests to the server
4
  • 2
    You have to post your code. But the standard method for forcing a refresh is adding a current time as a get parameter (ie ?time=1408642981 ). The server will ignore the parameter as it doesn't know it but the browser won't use local cache. Aug 21, 2014 at 17:43
  • that does not works! Aug 22, 2014 at 14:13
  • It does work, it does exactly what you asked for, omits the browser cache. You got what you asked for before I could take a look on your code, that's no reason to get rude. Aug 22, 2014 at 15:24
  • I dont want to be rude :P you'r idea was a good one, but for a reason I dont understand, it does not work. Aug 22, 2014 at 15:30

1 Answer 1

3

Jakub Kania was correct in his comment that the date/time have to be added to the url to make it different from the url of tiles in the cache. You have to subclass OpenLayers.Layer.XYZ for that:

OpenLayers.Layer.CustomXYZ = OpenLayers.Class(OpenLayers.Layer.XYZ, {
  getURL: function () {
    var url = OpenLayers.Layer.XYZ.prototype.getURL.apply(this, arguments);
    return url + '?time='+ new Date().getTime();
  }
});

var tiledLayer = new OpenLayers.Layer.CustomXYZ('TMS',
                "{{ tmsURL }}1.0/layer/{{ shapefile.id }}/${z}/${x}/${y}.png");

tiledLayer.redraw(true);
1
  • That's interesting. Once I saw the code I was gonna suggest to add a new layer, wait for it to load and remove the old one. Didn't thought you can just hack the function. Aug 25, 2014 at 17:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.