Reputation: 6470
I use google.maps.KmlLayer('http://mywebsite.com/my.kml') to set objects from KML file. It is working, but when I change kml and try to refresh website...I still have the same state as before...without my changes. When I change file name to my2.kml - it is working... Are google caching my kml? What I need to do to update changes with the same kml file name?
Upvotes: 7
Views: 5670
Reputation: 1
I would like to add a strong caveat to the advice to add cache-busting to the KML URLs: please note that if you use a timestamp like (new Date()).getTime()
, this means that Google will try to fetch the KML file from your server almost every time a user tries to display your KML layer.
Two consequences:
A better strategy would be to only add a cache-busting parameter when you know the KML has changed. Maybe the browser could receive a hash of your KML file from your server, or a version number of the KML, and add that as an extra parameter to the request. You'd need to recompute the hash, or generate a new version number, every time the KML file got updated.
A lazier, far less efficient idea, would be for the server to generate a new token at regular intervals (e.g. once every 10 minutes, make it a reasonable period for your use-case), and have the browser use that token when it needs to display the KML file.
A much worse idea, but still a little bit better than using a browser-side millisecond timestamp to do cache-busting, is to only change the parameter at most once every 10 minutes on the browser side.
For example, instead of (new Date()).getTime()
, you could use something like: Math.floor((new Date()).getTime()/1000/(10*60))*(10*60)
Note that this is a pretty bad solution, because this will still be computed based on the end-user's computer clock. If the end-users have set up incorrect times on their machines, many different timestamps can still be generated within a short period of time.
One of the advantages of using KML layers in Google Maps JavaScript API is that they get cached on Google side: if you don't need that, you might be better served with layers built directly on the browser-side. E.g. use GeoJSON. There is even a doc for that for the Google JS API: https://developers.google.com/maps/documentation/javascript/datalayer#load_geojson
Upvotes: 0
Reputation: 664
Yes, google servers cache the KML data. So avoid this caching, change the kml url to
"http://www.kmlsource.com/foo.kml?dummy=" + (new Date()).getTime();
This will always generate a new website and the caching problem will be solved.
Upvotes: 5
Reputation: 889
The Google servers do in fact cache KML data. Because the Google servers are processing your KML and not your browser, clearing your cache will not help. This is why changing the file name works. In order to prevent caching, add a cache-buster to your KML URL that you create the KML layer with, such as a random number or the current timestamp. http://mywebsite.com/my.kml?rev=323626252346 where the value of rev changes every time you refresh the page would work. You could also write Javascript so that you can click a button that updates the URL on the KML Layer object, removing the need to refresh the page.
Upvotes: 10