【问题标题】:React app not working with firebase hostingReact 应用程序不适用于 Firebase 托管
【发布时间】:2021-09-19 02:36:09
【问题描述】:

我的 react 应用程序在本地运行良好......但在部署到 firebase 托管后就不行了。

Chrome 的检查器的网络选项卡显示 https://another-netflix-clone.web.app/ 被添加到每个 api 调用...https://another-netflix-clone.web.app/api.themoviedb.org/3/discover/movie?api_key=key&with_genres=27

这是个问题。不知道为什么会这样。

firebase.json

{
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

主机设置:

=== Hosting Setup

Your public directory is the folder (relative to your project directory) that
will contain Hosting assets to be uploaded with firebase deploy. If you
have a build process for your assets, use your build's output directory.

? What do you want to use as your public directory? build
? Configure as a single-page app (rewrite all urls to /index.html)? Yes
? Set up automatic builds and deploys with GitHub? No
✔  Wrote build/index.html

i  Writing configuration info to firebase.json...
i  Writing project information to .firebaserc...

✔  Firebase initialization complete!
chaselwander@W-MD-ML-CHASEL chad_neflix_clone (master)*$ npm run build

> chad_neflix_clone@0.1.0 build /Users/chaselwander/projects/react_netflix_clone/chad_neflix_clone
> react-scripts build

Creating an optimized production build...
Compiled successfully.

应用程序:

https://another-netflix-clone.web.app/

API调用代码片段:

/* example fetchUrl value: https://image.tmdb.org/t/p/original/trending/all/week?api_key=${APIKEY}&language=en-US */

function Row({ title, fetchUrl, isLargeRow }) {
  const [movies, setMovies] = useState([]);
  const [trailerUrl, setTrailerUrl] = useState("");

  useEffect(() => {
    async function fetchData() {
      const request = await axios.get(fetchUrl);
      setMovies(request.data.results);
      return request;
    }
    fetchData();
  }, [fetchUrl]);

我在本地使用 build 文件夹运行了应用程序......它工作得很好。

serve -s build

更新了问题描述:

我真的只需要帮助弄清楚为什么这个 firebase 会生成应用程序 url:

https://another-netflix-clone.web.app/

在每个 api 调用之前添加:

Request URL: https://another-netflix-clone.web.app/api.themoviedb.org/3/discover/tv?api_key=2ce4f6136730519d5877eb7a94b4e21e&with_networks=213

当我运行时在本地:

serve -s build

api调用正确,返回数据:

https://api.themoviedb.org/3/discover/tv?api_key=2ce4f6136730519d5877eb7a94b4e21e&with_networks=213

【问题讨论】:

  • 显示 API 调用的代码。这与 firebase 无关。
  • 您的应用程序不工作,检查控制台日志,有很多错误,部署工作正常,但您的代码已损坏。在本地尝试 npm run build,从 build 文件夹运行 index.html 并查看它的行为。
  • 谢谢大家!它当然可能是我的代码......但是在本地运行构建文件夹可以正常工作。我用更多信息更新了我的帖子。
  • 能否请console.log(fetchUrl) 分享输出截图?我想在控制台中查看 url。

标签: reactjs firebase


【解决方案1】:

在当前版本中,当前的错误在这里:

 console.log(movies);
  // install Quokka to print results to editor
  return (
    <div className="row">
      <h2>{title}</h2>
      <div className="row__posters">
        {movies.map((movie) => (
          <img
            key={movie.id}
            onClick={() => handleClick(movie)}
            className={`row__poster ${isLargeRow && "row__posterLarge"}`}
            src={`${base_url}${
              isLargeRow ? movie.poster_path : movie.backdrop_path
            }`}
            alt={movie.name}
          />
        ))}
      </div>
      {trailerUrl && <YouTube videoId={trailerUrl} opts={opts} />}
    </div>
  );

尝试将其重写为:

 console.log(movies);
  // install Quokka to print results to editor
  return (
    <div className="row">
      <h2>{title}</h2>
      <div className="row__posters">
        {movies && movies.map((movie) => (
          <img
            key={movie.id}
            onClick={() => handleClick(movie)}
            className={`row__poster ${isLargeRow && "row__posterLarge"}`}
            src={`${base_url}${
              isLargeRow ? movie.poster_path : movie.backdrop_path
            }`}
            alt={movie.name}
          />
        ))}
      </div>
      {trailerUrl && <YouTube videoId={trailerUrl} opts={opts} />}
    </div>
  );

是的,这与 Firebase 托管无关。

还将您的 axios.js 文件更新为:

import axios from "axios";

const instance = axios.create({
  baseURL: "https://api.themoviedb.org/3",
  headers: {
    "Content-Type": "application/json;charset=UTF-8",
  },
});

export default instance;

【讨论】:

  • 谢谢塔里克!应用程序现在加载...但没有从 api 调用返回数据...端点 url 仍以应用程序 url 为前缀:https://chad-netflix-clone-a9f63.web.app 这可以在网络选项卡中看到。我对此感到非常困惑。
  • @Hase134 我不认为我们可以调试你的整个代码,直到它工作。 stackoverflow 上的每个问题都应该与单个错误/主题有关。我们需要保持清洁和结构化。否则没有人可以在这里找到任何答案。
  • Tarik 我同意...感谢您提供的帮助。但是我要问的问题仍然存在......我更新了描述以更清楚地了解正在发生的事情。谢谢!
  • 您能分享一些我们可以在我们这边运行的最小项目或代码吗?您能否按您所说的serve -build 显示屏幕截图。对我来说,这看起来像是一个可能的 CORS 问题,但我不知道为什么没有显示任何错误。
  • 当然,Tarik... 这是代码:https://github.com/CH34MKE10/react_netflix_clone Firebase 托管上的代码:https://another-netflix-clone.web.app/ 网络选项卡下的预览和响应选项卡说:你需要启用 JavaScript 来运行这个应用程序。 但是我可以在 chrome 的控制台中运行 javascript,所以 idk?
猜你喜欢
  • 2019-11-20
  • 2020-11-25
  • 2018-10-20
  • 2018-12-09
  • 2021-09-09
  • 2019-05-26
  • 1970-01-01
  • 1970-01-01
  • 2021-05-09
相关资源
最近更新 更多