简体   繁体   English

在服务器端渲染Google Maps

[英]Rendering Google Maps on Server Side

I have a npm package that exports a class for a google map (based on this article: http://cuneyt.aliustaoglu.biz/en/using-google-maps-in-react-without-custom-libraries/ ). 我有一个npm软件包,可为Google地图导出类(基于本文: http : //cuneyt.aliustaoglu.biz/en/using-google-maps-in-react-without-custom-libraries/ )。 When I import this package to a server-rendering react app, I get this error that window is not defined. 当我将此包导入服务器渲染的React应用程序时,出现此错误,即未定义窗口。 I believe this is happening b/c window doesn't exist in node. 我相信这正在发生b / c窗口在节点中不存在。 However, I only reference window in componentDidMount lifecycle hook in the map pkg and I believe the server-side doesn't render that lifecycle hook. 但是,我仅在映射pkg中的componentDidMount生命周期挂钩中引用window ,并且我相信服务器端不会呈现该生命周期挂钩。

What's the best way to render the map component on the server-side? 在服务器端呈现地图组件的最佳方法是什么?

I had same question before. 我之前也有同样的问题。 And found out the Google Map does not support rendering tiles on server side: https://github.com/google-map-react/google-map-react/issues/302#issuecomment-279984745 并发现Google Map不支持服务器端的渲染图块: https//github.com/google-map-react/google-map-react/issues/302#issuecomment-279984745

If you really want to render the map on server side for some special purposes. 如果您真的想出于某些特殊目的在服务器端渲染地图。 You can use headless browser to render and cache the result. 您可以使用无头浏览器来呈现和缓存结果。 Just like something the Prerender.io is doing. 就像Prerender.io所做的一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM