简体   繁体   English

Firebase 云功能 + 托管 - 404 Not Found

[英]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(/*并让我的客户端处理路由。

There is currently a problem with Firebase Hosting, which appears to be affecting a number of sites, including two of my own. Firebase 托管目前存在一个问题,它似乎影响了许多站点,包括我自己的两个站点。 You can stay up-to-date with their status updates via this link .您可以通过此链接了解他们的状态更新。

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);
  1. On my build process, I'm moving the build directory into the functions folder to get deployed to Firebase with the functions so my project directory when I'm getting ready to deploy looks like where the build directory in functions and react-ui is identical:在我的构建过程中,我将构建目录移动到函数文件夹中,以便将函数部署到 Firebase,因此当我准备部署时,我的项目目录看起来就像functionsreact-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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM