如何使用 Nextjs 设置 Firebase 托管多站点

How can I setup Firebase hosting multisites with Nextjs

我正在尝试将 nextjs 应用程序部署到 firebase 托管并希望支持多个站点。当我部署它时,所有应用程序都会呈现默认应用程序。我该如何解决这个问题?

我正在学习使用 AngularJs 将多个站点部署到 firebase 的教程 here,我尝试用 NextJs

替换 Angularjs

package.json

中的脚本
{
  "scripts": {
    "accounts": "next \"src/apps/accounts/\"",
    "accounts-build": "next build \"src/apps/accounts/\"",

    "admin": "next \"src/apps/admin/\"",
    "admin-build": "next build \"src/apps/admin/\"",
    "preserve": "npm run build-public && npm run build-funcs && npm run admin-build && npm run copy-deps && npm run install-deps",
    "serve": "cross-env NODE_ENV=production firebase serve",
    "predeploy": "npm run build-public && npm run build-funcs && npm run accounts-build && npm run admin-build && npm run copy-deps",
    "deploy": "firebase deploy",
    "clean": "rimraf \"dist/functions/**\" && rimraf \"dist/public\"",
    "build-public": "cpx \"src/public/**/*.*\" \"dist/public\" -C",
    "build-funcs": "babel \"src/functions\" --out-dir \"dist/functions\"",
    "copy-deps": "cpx \"*{package.json,package-lock.json,yarn.lock}\" \"dist/functions\" -C",
    "install-deps": "cd \"dist/functions\" && npm i"
  }
}

firebase.json

{
  "hosting": [
    {
      "target": "accounts",
      "public": "dist/public",
      "rewrites": [
        {
          "source": "**/**",
          "function": "accounts"
        }
      ]
    },
    {
      "target": "admin",
      "public": "dist/public",
      "rewrites": [
        {
          "source": "**/**",
          "function": "admin"
        }
      ]
    }
  ],
  "functions": {
    "source": "dist/functions"
  }
}

.firebaserc

{
  "projects": {
    "default": "example-app"
  },
  "targets": {
    "example-app": {
      "hosting": {
        "accounts": [
          "example-accounts"
        ],
        "admin": [
          "example-admin"
        ]
      }
    }
  }
}

index.js 在函数中

import {https} from 'firebase-functions'

const dev = process.env.NODE_ENV !== 'production'
const app = require('next')({dev, conf: {distDir: 'next'}})
const handle = app.getRequestHandler()

const accounts = https.onRequest((req, res) =>
    app.prepare().then(() => handle(req, res)))

const admin = https.onRequest((req, res) =>
    app.prepare().then(() => handle(req, res)))

export {accounts, admin}

next.config.js 移动到根文件夹

module.exports = {
  distDir: '../../../dist/functions/next'
}

所有应用程序都呈现默认的帐户应用程序。希望管理员呈现管理应用程序。

我在 package.json 中复制了 build-public 并相应地更新了 firebase.json 并且它起作用了。

package.json

"scripts": {
   "accounts": "next \"src/apps/accounts/\"",
   "accounts-build": "next build \"src/apps/accounts/\"",
   "accounts-build-public": "cpx \"src/public/**/*.*\" \"dist/accounts/public\" -C",

   "admin": "next \"src/apps/admin/\"",
   "admin-build": "next build \"src/apps/admin/\"",
   "admin-build-public": "cpx \"src/public/**/*.*\" \"dist/admin/public\" -C", 
   ...

firebase.json

已将 "public": "dist/public" 更新为 "public": "dist/admin/public""public": "dist/accounts/public"

由于您的应用程序目录中有不同的文件夹,如 admin 和 accounts,因此当您为每个文件夹构建时 next.js 始终考虑您正在构建的最后一个构建并提供上一个构建的页面所以为了克服这个问题,我们必须在不同的目录中为管理员创建构建目录将是

dist/functions/admin/next

对于客户,目录将是

dist/functions/accounts/next

您必须为此定义不同的 distDir,以便在提供页面时它会在指定目录中查找,因此请更改您的

next.config.js 管理员

module.exports = {
  distDir: '../../../dist/functions/admin/next'
}

next.config.js 帐户

module.exports = {
  distDir: '../../../dist/functions/accounts/next'
}

将此 next.config.js 保留在您的 src/apps/adminsrc/apps/customer 中 目录 分别 并更改您的

index.js 在函数中

import {https} from 'firebase-functions'

const dev = process.env.NODE_ENV !== 'production'
const appAdmin = require('next')({dev, conf: {distDir: 'admin/next'}});

const appAccounts= require('next')({dev, conf: {distDir: 'accounts/next'}});

const handle = app.getRequestHandler()

const accounts = https.onRequest((req, res) =>
    appAccounts.prepare().then(() => handle(req, res)))

const admin = https.onRequest((req, res) =>
    appAdmin.prepare().then(() => handle(req, res)))

export {accounts, admin}