首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下一个js -react google-map/api在Vercel中部署时出错:发生了客户端异常。

下一个js -react google-map/api在Vercel中部署时出错:发生了客户端异常。
EN

Stack Overflow用户
提问于 2022-06-21 11:48:44
回答 1查看 382关注 0票数 1

我的下一个js应用程序在开发和构建中运行良好,但是当我试图在Vercel中部署它时,当我试图调用调用我的mapView组件的页面时,我会得到这个错误:

应用程序错误:发生了客户端异常(有关更多信息,请参见浏览器控制台).

这是vercel应用程序中的完整日志:

这是我的地图组件:

代码语言:javascript
复制
import { GoogleMap, Marker, MarkerClusterer, useJsApiLoader } from '@react-google-maps/api'
import { useCallback, useState } from 'react'
import SpotCard from './spots/SpotCard'

export const MapView = ({ data }) => {
  const [postCardData, setPostCardData] = useState(null)
  const defaultProps = {
    center: {
      lat: -21.115141,
      lng: 55.536384,
    },
    containerStyle: {
      maxWidth: '800px',
      height: '600px',
      margin: '0 auto',
      borderRadius: '10px',
      opacity: 1,
    },
    zoom: 10,
  }

  const getImage = (category) => {
    let source
    if (category == 'Bivouac') source = `/static/icons/Bivouac.png`
    if (category == 'Camping') source = `/static/icons/Camping.png`
    if (category == 'Gîtes') source = `/static/icons/Gîtes.png`
    if (category == 'Insolite') source = `/static/icons/Insolite.png`

    return source
  }

  const { isLoaded } = useJsApiLoader({
    id: 'google-map-script',
    googleMapsApiKey: 'XXXXXXXXX',
  })

  const [map, setMap] = useState(null)

  const onLoad = useCallback(function callback(map) {
    const bounds = new window.google.maps.LatLngBounds(defaultProps.center)
    map.fitBounds(bounds)
    setMap(map)
  }, [])

  const onUnmount = useCallback(function callback(map) {
    setMap(null)
  }, [])
  return (
    isLoaded ? (
      <div>
        <div className=" my-16">
          <GoogleMap
            mapContainerStyle={defaultProps.containerStyle}
            center={defaultProps.center}
            zoom={10}
            onLoad={onLoad}
            onUnmount={onUnmount}
          >
            <MarkerClusterer minimumClusterSize={4}>
              {(clusterer) =>
                data.length > 1 ? (
                  data?.map((marker, i) => (
                    <Marker
                      key={i}
                      position={{
                        lat: Number(marker.attributes.lat),
                        lng: Number(marker.attributes.lon),
                      }}
                      icon={getImage(marker.attributes.category.data.attributes.name)}
                      onClick={() => setPostCardData(marker)}
                      clusterer={clusterer}
                    />
                  ))
                ) : (
                  <Marker
                    position={{
                      lat: Number(data.attributes.lat),
                      lng: Number(data.attributes.lon),
                    }}
                    icon={getImage(data.attributes.category.data.attributes.name)}
                    onClick={() => setPostCardData(data)}
                  />
                )
              }
            </MarkerClusterer>
            {postCardData !== null && (
              <div className=" z-10 flex h-full items-center justify-center">
                <div className=" max-w-xs">
                  <div className="flex justify-end">
                    <button
                      className="absolute top-16 z-20 inline-flex items-center justify-center rounded-full bg-white p-2 text-green-600 opacity-95 hover:bg-gray-100 hover:text-green-800 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-green-500"
                      onClick={() => setPostCardData(null)}
                    >
                      <span className="sr-only">Close menu</span>

                      <svg
                        className="h-6 w-6"
                        xmlns="http://www.w3.org/2000/svg"
                        fill="none"
                        viewBox="0 0 24 24"
                        stroke="currentColor"
                        aria-hidden="true"
                      >
                        <path
                          strokeLinecap="round"
                          strokeLinejoin="round"
                          strokeWidth="2"
                          d="M6 18L18 6M6 6l12 12"
                        />
                      </svg>
                    </button>
                  </div>
                  <SpotCard post={postCardData} />
                </div>
              </div>
            )}
          </GoogleMap>
        </div>
      </div>
    ) : (
      <></>
    )
  )
}

我试图找到一个解决办法,但从昨天至今还没有找到,所以我想知道是否有人已经有相同的问题,可以帮助我。

谢谢

编辑:这也可能是ContentSecurityPolicy的一个问题,我在这里让我的配置:

代码语言:javascript
复制
const ContentSecurityPolicy = `
  default-src 'self';
  script-src 'self' 'unsafe-eval' 'unsafe-inline' giscus.app maps.googleapis.com;
  style-src 'self' 'unsafe-inline' fonts.googleapis.com;
  img-src * blob: data: maps.gstatic.com googleapis.com ggpht.com;
  media-src 'none';
  connect-src * googleapis.com google.com gstatic.com data: blob:;
  font-src 'self' fonts.gstatic.com;
  frame-src giscus.app google.com

或者来自eslint config:

代码语言:javascript
复制
module.exports = {
  root: true,
  env: {
    browser: true,
    amd: true,
    node: true,
    es6: true,
    window: true,
  },
  globals: {
    window: true,
    document: true,
    google: true,
  },
  extends: [
    'eslint:recommended',
    'plugin:prettier/recommended',
    'next',
    'next/core-web-vitals',
    'google',
  ],
  rules: {
    'prettier/prettier': ['error', { endOfLine: 'auto' }],
    'react/react-in-jsx-scope': 'off',
    'react/prop-types': 0,
    'no-unused-vars': 0,
    'react/no-unescaped-entities': 0,
  },
}

EN

回答 1

Stack Overflow用户

发布于 2022-09-05 18:13:59

我发现一篇文章帮助我解决了控制台中的生产错误。以下是本文:https://medium.com/web-dev-survey-from-kyoto/3-gotchas-of-google-maps-api-when-used-with-next-js-and-eslint-dba627c9657d

我在useEffect中加载了地图,我认为这是主要问题。希望能帮上忙!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72700233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档