Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

My leaflet app gets the tiles from TileMill's tile server in the standard way:

L.tileLayer('http://<myip>/tile/SteveCountryVic/{z}/{x}/{y}.png', { ... });

These tiles are often old, not showing the latest styles. I'm not sure if they're coming from TileMill itself or some intermediary cache.

I notice that TileMill includes an 'updated' parameter:

http://<myip>/tile/SteveCountryVic/{z}/{x}/{y}.png?updated=1372394902000

Manually copying that updated timestamp means I get the latest timestamp. But what's a good way to not have to manually update the URL every time I change the style?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

The updated=1372394902000 Parameter is basically the Unix epoch time in milseconds.

Your tile is being requested with a HTTP GET request. What you need to know about Http Get's is that you can pile on any arbitrary parameters at the end of a Get Request, and if the parameter does not make sense to the server, it will ignore it.

This fact is exploited often, to make sure that you get a new response from the server, and not from the Client Browser's cache.

If you had request for say: http://example.com/foo/bar.png, the browser would see if the resource from this url was present in the cache. If it is, then a request is Not made, and the resource is served from the cache.

Hence we tag an arbitrary parameter with the timestamp.Every time you make a new request, the timestamp has changed, and it becomes a new URL for the browser.

So in short you can do it like this in JavaScript

var ts=Math.round(new Date().getTime()/1000.0);
var url="http://<myip>/tile/SteveCountryVic/{z}/{x}/{y}.png?updated="+ts;

Hence Automatically, with every time your App runs, the new timestamp is used, and the latest tile is called.

A word of warning though: This will make your application slow, since none of the tiles will every be cached by your browser.

share|improve this answer
    
Great explanation, thanks. So after a bit of testing, I see that TileMill doesn't even pay attention to the updated parameter. Just a browser/cache trick - cool. –  Steve Bennett Jun 28 '13 at 12:50
    
One nifty thing about this is that by changing the 1000.0, you can tweak the cache expiry. eg, replace it with (1000.0 * 86400.0) and tiles will be re-fetched once a day. –  Steve Bennett Jun 28 '13 at 12:51
    
I wouldn't suggest using (1000.0 * 86400.0) because you won't get the day exactly. There are other ways of getting the current Date from JavaScript's Date. Additionally I would suggest that if you are in control of the tiles, you can simply increment the parameter by 1, every-time you update the tiles. –  Devdatta Tengshe Jun 28 '13 at 14:56
    
Well, incrementing the parameter manually is what I do at the moment, but it's not much fun. –  Steve Bennett Jun 28 '13 at 15:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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