【发布时间】: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。