Google Maps API javascript 可以缓存吗?
只是好奇,Google Maps API v3 javascript 是否缓存在本地服务器上?
因为,有时我的内联网页面加载速度很慢,因为互联网连接速度较慢。否则,它会从本地服务器加载文件,并且仅在发出地图请求时减慢速度。
我什至准备好运行一个 cron 作业来偶尔更新 javascript 文件。
感谢您的任何意见。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“按原样”是不可能的。
当您向 Google 请求脚本时,他们会沿着脚本发送标头,并且这些标头包含“no-cache”指令。
因此,如果您希望它们可缓存,则必须创建代理。您无需将脚本 src 指向 Google,而是将其指向您的服务器。然后,您的服务器会调用 Google 并将响应发送回客户端。
这样您就可以控制 HTTP 标头和缓存。您也可以对脚本内容进行缓存,以减少与 Google 的连接。
我不会建议有人在生产或关键任务网站上这样做。所有 Google API 都会经常更新,并且或多或少地绑定在一起。如果某些内容与其他内容不同步,那么您将很难追踪错误。
希望有帮助。
编辑:我听说您将脚本放在文档的
HEAD
部分中。也许这会损害您的“感知”页面加载时间。尝试将脚本的下载移至页面标记之前以及页面
onload
事件中的地图初始化之前。麦克风
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 theonload
event of the page.Mike
截至2016年,js返回时带有“Cache-Control: public, max-age=1800”标头,因此至少缓存半小时。
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.
恕我直言,你无法缓存它。 API 脚本调用 Google 服务器上的对象。最多,您可以捕获结果并将其缓存为图像(但随后您会失去交互性)。
如果可以缓存 GMap 结果,人们就会在本地缓存 Google 的整个数据库,我不认为这是用户协议的一部分;)。
如果您想要离线地图,则必须使用非交互式图像,或者购买地图服务器。
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.
不会让您违反 Google 服务条款的一个选项是使用 OpenLayers 脚本保留 OpenStreetMap 图块的本地缓存以供查看它们。基本上,使用免费数据创建您自己的地图服务器。
如果有对您的 Intranet 很重要的特定兴趣点,您可以确保它们位于 OpenStreetMap 中,并自行设置一个仅包含您需要的功能的渲染服务器。毕竟,将一个地区的每所学校覆盖在一些 png 上会比仅显示其中包含学校的 png 需要更多的工作。
如果您的地图服务器在周末获取/生成图块,而不是一直使用 Google 地图 API,那么在外部连接速度较慢时,延迟也会少很多。
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.