Question

Just curious, whether the Google Maps API v3 javascript be cached on the local server?

Because, sometimes my intranet pages are loading slow because of slower internet connection. Otherwise, it would load the file from local server and slow down only when the map request is made.

I am even ready to run a cron job to update the javascript file once in a while.

Thanks for any input.

Was it helpful?

Solution

It's impossible "as is".

When you request the script from Google, they send headers along the script and those headers contains the "no-cache" directive.

So if you want them to be cachable, you must create a proxy. Instead of pointing the script src at Google, you point it to your server. Your server then make the call to Google and send the response back to the client.

This way you'll have control over the HTTP header and the caching. You could do caching on the script content as well to make less connections to Google.

I wouldn't advise someone to do it on a production or a mission critical website. All Google APIs are updated frequently and are bound more or less together. If something goes out of synch with something else, you have a hard to track bug on your hands.

Hope that helps.

EDIT: I heard you were putting your scripts in the HEAD section of your document. Maybe thats hurting your "perceived" page loading time. Try to move the download of the script just before the </body> tag and the map initialization in the onload event of the page.

Mike

OTHER TIPS

As of 2016, the js is returned with "Cache-Control: public, max-age=1800" header, so it is cached for at least for a half hour.

IMHO you can't cache it. The API script calls objects on the Google server. At most, you can capture the results and cache them as images (but then you lose interactivity).

If it would be possible to cache GMap results, people would just cache Google's entire DB locally and I don't think this is part of the user agreement ;).

If you want offline maps, you'll have to do with non-interactive images, or buy a map server.

An option that would not have you violating Google's Terms of Service would be to keep a local cache of OpenStreetMap tiles with the OpenLayers script for viewing them. Basically, using free data to create your own map server.

If there are particular points of interest that are important for your intranet, you can make sure that they are in OpenStreetMap and setup a rendering server yourself with just the features you require. After all, overlaying each school in a district over some png's is going to take more work then just showing the png's made with the schools in them.

It also would take a lot less lag on a slow outside connection if your map server gets/generates the tiles during the weekend instead of hitting the Google Map API all the time.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top