[英]Firebase Cloud Functions + Hosting - 404 Not Found
I'm building my web app on top of Firebase cloud functions, and hosting.我正在 Firebase 云功能和托管之上构建我的网络应用程序。 My client was built with create-react-app, and I have a small express server running in a cloud function that serves the app build.
我的客户端是用 create-react-app 构建的,我有一个小型快速服务器在云函数中运行,为应用程序构建提供服务。
Everything works fine in a normal browser window however, I am having an issue where if I go to any url that is not '/' in incognito mode or a mobile device I get a 404 and "Not Found"在普通浏览器窗口中一切正常,但是,我遇到了一个问题,如果我在隐身模式或移动设备中转到任何不是“/”的网址,我会收到 404 和“未找到”
ie: hitting " https://144blocks.com/week " directly will load fine in a normal browser window but throws the following error in incognito and mobile.即:直接点击“ https://144blocks.com/week ”将在普通浏览器窗口中正常加载,但在隐身和移动中会引发以下错误。
My firebase function for serving the site is:我为网站提供服务的 Firebase 功能是:
functions/index.js函数/index.js
const functions = require('firebase-functions');
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
app.get('/*', (req, res) => {
const fullPath = path.normalize(__dirname + '/../react-ui/build/index.html');
res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
res.sendFile(fullPath);
});
exports.app = functions.https.onRequest(app);
In my client my firebase.json file:在我的客户端中,我的 firebase.json 文件:
firebase.json firebase.json
{
"hosting": {
"public": "react-ui/build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"function": "app"
}
]
}
}
I'm not sure why I can't hit any route directly aside from '/' in an incognito window or a mobile device.我不知道为什么我不能在隐身窗口或移动设备中直接点击除“/”之外的任何路线。 Ideally, I want all my routes to flow through the one
app.get(/*
on my express server and have my client handle the routing.理想情况下,我希望我的所有路由都通过我的 Express 服务器上的一个
app.get(/*
并让我的客户端处理路由。
I am still not 100% sure as to why this is working however I fixed the issue by: 1. In my functions/index.js
file, updating the fullPath
because I believe that the __dirname + '/../react-ui/build/index.html'
wasn't resolving.我仍然不能 100% 确定为什么会这样,但是我通过以下方式解决了这个问题: 1. 在我的
functions/index.js
文件中,更新fullPath
因为我相信__dirname + '/../react-ui/build/index.html'
没有解决。
My name functions/index.js
file:我的名字
functions/index.js
文件:
const functions = require('firebase-functions');
const express = require('express');
const path = require('path');
const app = express();
app.get('/*', (req, res) => {
const fullPath = path.normalize(__dirname + '/build/index.html');
res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
res.sendFile(fullPath);
});
exports.app = functions.https.onRequest(app);
functions
and react-ui
is identical:functions
和react-ui
中的构建目录相同: If anyone has more insight into why this is exactly working I would appreciate additional information.如果有人更深入地了解为什么这确实有效,我将不胜感激。
Thanks!谢谢!
Try running firebase login
.尝试运行
firebase login
。 I had this exact issue and it was because my login session had expired.我遇到了这个确切的问题,这是因为我的登录会话已过期。
Alternatively, try firebase login --no-localhost
if you're having trouble with the standard login.或者,如果您在使用标准登录时遇到问题,请尝试
firebase login --no-localhost
。
If you're using ci/cd, use firebase login:ci --no-localhost
, save your ci token to your environment under FIREBASE_TOKEN
and deploy with firebase deploy --only functions --token $FIREBASE_TOKEN
.如果您使用 ci/cd,请使用
firebase login:ci --no-localhost
,将您的 ci 令牌保存到FIREBASE_TOKEN
下的环境中,并使用FIREBASE_TOKEN
firebase deploy --only functions --token $FIREBASE_TOKEN
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.