【问题标题】:React - firebase hosting not workingReact - firebase 托管不工作
【发布时间】:2018-08-29 01:40:09
【问题描述】:

我正在使用 react 和 firebase 创建 api。

但是没有托管。 Chrome 浏览器需要清空缓存和强烈刷新才能获得 404 页面。

网址是https://jp-db-2018.firebaseapp.com/api/getChannels

总之,我想使用 react 和 firebase 进行托管。

这里是项目设置。

感谢您的建议。

firebase.json

{
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "functions": {
    "predeploy": [
         "npm --prefix \"$RESOURCE_DIR\" run lint"
    ],
    "source": "functions"
  },
  "hosting": {
    "public": "build",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/api/**",
        "function": "api"
      }
    ],
    "headers": [ {
      "source" : "**/*.@(eot|otf|ttf|ttc|woff|font.css)",
      "headers" : [ {
        "key" : "Access-Control-Allow-Origin",
        "value" : "*"
      } ]
      }, {
     "source" : "**/*.@(jpg|jpeg|gif|png)",
      "headers" : [ {
        "key" : "Cache-Control",
        "value" : "max-age=7200"
      } ]
      }, {
      "source" : "404.html",
      "headers" : [ {
        "key" : "Cache-Control",
        "value" : "max-age=300"
      } ]
    } ],
    "cleanUrls": true,
    "trailingSlash": false
  }
}

/src/index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import * as firebase from 'firebase';

const config = {
  apiKey: "xxxxxxxx",
  authDomain: "xxxxxxxx",
  databaseURL: "xxxxxxxx",
  projectId: "xxxxxxxx",
  storageBucket: "xxxxxxxx",
  messagingSenderId: "xxxxxxxx"
};
firebase.initializeApp(config);

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();

/functions/index.js

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://jp-db-2018.firebaseio.com"
});
const express = require('express');
const app = express();
const db = admin.firestore();

app.get('/getChannels', (req, res) => {
  res.json({code: 200});
});
exports.api = functions.https.onRequest(app);

树视图

【问题讨论】:

    标签: javascript reactjs firebase google-cloud-functions firebase-hosting


    【解决方案1】:

    似乎工作正常。您指定您希望它在您的 firebase 函数的 json 响应中说 code:200。你期待别的吗?

    【讨论】:

    • 我不知道为什么,但它的工作。但是问题中的源已重新部署,因此无法正常工作。我不知道为什么。
    • 我不明白。托管有时可能需要一些时间。此外,如果您没有看到我看到的内容,您可能需要刷新浏览器缓存。
    • 感谢您的回答。缓存似乎是正确的。
    • 我不明白。你说它不工作?你修好了吗?我不清楚现在的问题是什么。
    • 当我创建项目时,我在 rewrites 选项中得到 yes 并且它已被修复。
    猜你喜欢
    • 1970-01-01
    • 2017-09-16
    • 2019-01-16
    • 2017-03-28
    • 2018-02-24
    • 2019-01-22
    • 1970-01-01
    • 2021-05-07
    • 2020-08-13
    相关资源
    最近更新 更多