gatsby - 使用 Gatsby 和 Netlify 构建站点时,Google Maps InvalidKeyMapError

标签 gatsby netlify

我构建了一个 Gatsby 站点,该站点使用我从 npm 包“google-maps-react”中获取的 google maps 组件。在我的本地环境中一切正常,但是当我部署到 Netlify 时,我收到“Google Maps JavaScript API 错误:InvalidKeyMapError”。

我完成了确保我的 API key 已正确注册和激活的所有步骤。我确保在 Netlify UI 中将 API key 声明为环境变量,并在我的组件中使用“process.env.GOOGLE_API_KEY”访问它。

import React from "react"
import { Map, InfoWindow, Marker, GoogleApiWrapper } from "google-maps-react"

export class MapContainer extends React.Component {


  render() {

    return (
      <Map google={this.props.google} zoom={14} initialCenter={{lat:37.769461, lng:-122.251831}}>
        <Marker onClick={this.onMarkerClick} name={"Current location"} />
        <InfoWindow onClose={this.onInfoWindowClose}>
          <div>
            ...some code
          </div>
        </InfoWindow>
      </Map>
    )
  }
}

export default GoogleApiWrapper({
    apiKey: (`${process.env.GOOGLE_API_KEY}`)
  })(MapContainer)

从我读过的内容来看,我需要在 Netlify UI 中声明 GOOGLE_API_KEY 环境变量才能访问它,但我显然错过了一些东西。任何帮助表示赞赏,谢谢

最佳答案

环境变量需要以GATSBY_开头在客户端javascript中,如docs所示.

使用 GATSBY_GOOGLE_API_KEYprocess.env.GATSBY_GOOGLE_API_KEY以便在构建期间访问它们并捆绑到您的 Gatsby 客户端代码中。

关于gatsby - 使用 Gatsby 和 Netlify 构建站点时,Google Maps InvalidKeyMapError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56741636/

相关文章:

reactjs - React Hook 在 gatsby 生产模式下的第一次渲染上无法正常工作

reactjs - 滚动深度 GTM - Gatsby

typography - Gatsby .js : Load Google Fonts for Typography themes without Render-Blocking

reactjs - 如何通过react-fontawesome使用图层

gatsby - 如何使用Gatsby将任意文件添加到/public?

reactjs - google 域上 netlify 的 DNS 设置是什么?

gatsby - 在 Netlify 上的构建之间保留 gatsby 图像

graphql - 在 Gatsby 和 Contentful 中按日期过滤

javascript - 使用 JavaScript (Netlify Identity) 控制内容 : Content flashes in slow connections, 如何仅在登录检查后加载它?

pdf - 使用 github/netlify 上的 hugo 从 markdown 自动构建 PDF