如何使用以下项目结构将next.js应用托管到Firebase?

时间:2019-06-13 14:13:51

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

这是项目结构:

project structure

app folder structure

我的网站写在next.js上。为了使用next-i18next和发送电子邮件,有src / app / server / server.js。

const express = require("express");
const next = require("next");
const nextI18NextMiddleware = require("next-i18next/middleware");
var bodyParser = require("body-parser");
const nextI18next = require("../i18n");

const port = process.env.PORT || 3000;
const nextApp = next({ dev: process.env.NODE_ENV !== "production" });
const handle = nextApp.getRequestHandler();

nextApp.prepare().then(() => {
  const server = express();

  // parse application/json
  server.use(bodyParser.json());

  const sendMail = require("./sendMail");
  server.use("/api/v1/communicate", sendMail);

  server.use(nextI18NextMiddleware(nextI18next));

  server.get("*", (req, res) => handle(req, res));

  server.listen(port);
  console.log(`> Ready on http://localhost:${port}`);
});

要在Firebase上托管应用程序,我在src / functions / index.js中使用了firebase函数:

const functions = require("firebase-functions");
const next = require("next");

var dev = process.env.NODE_ENV !== "production";
var app = next({ dev, conf: { distDir: "next" } });
var handle = app.getRequestHandler();

exports.next = functions.https.onRequest((req, res) => {
  console.log("File: " + req.originalUrl); // log the page.js file that is being requested
  return app.prepare().then(() => handle(req, res));
});

在项目的根目录中,我有package.json:

{
  "name": "src",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "install": "yarn build-all",
    "next": "yarn build-firebase && cd \"src/app\" && yarn && yarn dev",
    "preserve": "yarn build-all",
    "serve": "firebase serve",
    "predeploy": "yarn build-all",
    "deploy": "firebase deploy",
    "build-all": "yarn build-next && yarn build-firebase",
    "build-next": "cd \"src/app\" && yarn && yarn build",
    "build-firebase": "cd \"src/functions\" && yarn"
  },
  "engines": {
    "node": "10"
  }
}

当我运行命令

yarn build-all

我有以下输出:

command output1

当我运行命令时

firebase deploy --only functions:next

我有这个输出:

command output2

当前,我的问题是我现在不怎么在Firebase托管上部署该应用程序。 不幸的是,我找不到任何可以帮助我理解问题的东西。我很高兴听到至少一些假设,这些假设可以帮助我理解问题。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

问题已经很老了,但是如果您仍然想知道如何解决此问题,请尝试添加以下yarn脚本:

"ignore-engine": "yarn config set ignore-engines true"

并更新您的所有内置cmd:

"build-all": "yarn ignore-engine && yarn build-next && yarn build-firebase"