使用ReactJs将Firebase部署到服务器上的子目录

时间:2020-07-22 04:02:04

标签: javascript reactjs firebase firebase-cloud-messaging

我正在尝试将firebase连接到我的React项目以实现推送通知,这是我的firebase.js文件:

import firebase from "firebase";

const firebaseConfig = {
    apiKey: "API_KEY",
    authDomain: "AUTH_DOMAIN",
    databaseURL: "DATABASE_URL",
    projectId: "PROJECT_ID",
    storageBucket: "STORAGE",
    messagingSenderId: "MSGID",
    appId: "APPID",
    measurementId: "MEASUREID"
};

firebase.initializeApp(firebaseConfig);

export default firebase;

这是我的firebase-messaging-sw.js文件:

const { registration } = require("../src/Components/loginPage/Reducers/RegistrationReducer");

importScripts('https://www.gstatic.com/firebasejs/7.16.1/firebase-app.js');
importScripts('https://www.gstatic.com/firebasejs/7.16.1/firebase-messaging.js');

firebase.initializeApp({
    messagingSenderId: "1061462994754",
})

const initMessaging = firebase.messaging().useServiceWorker(registration)

我想要做的是将Firebase部署到服务器上的子目录,而她的修改方式是修改firebase.json以使其正常工作:

{
  "hosting": {
    "public": "public",
    "headers": [
      {
        "source": "/serviceWorker.js",
        "headers": [
          {
            "key": "Cache-Control",
            "value": "no-cache"
          }
        ]
      }
    ],
    "redirects": [
      {
        "source": "/frontend/saif/**", // The subdirectory on the server 
        "destination": "https://testing-server.com/",
        "type": 302
      }
    ],
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ],
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ]
  }
}

但是,每当我尝试控制台记录令牌时,这就是我得到的: enter image description here

我认为我需要将我的子目录添加到/firebase-messaging-sw.js之前的请求URL中,这可能可以解决此问题,但是我不确定。有什么帮助吗?

0 个答案:

没有答案