【问题标题】:How can I setup Firebase hosting multisites with Nextjs如何使用 Nextjs 设置 Firebase 托管多站点
【发布时间】:2019-07-03 04:00:53
【问题描述】:

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

我正在按照教程 here 使用 AngularJs 将多个站点部署到 firebase 并尝试用 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'
}

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

【问题讨论】:

    标签: javascript node.js reactjs firebase-hosting next.js


    【解决方案1】:

    由于您在您的应用程序目录中拥有不同的文件夹作为管理员和帐户,因此当您为它们中的每一个进行构建时,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}
    

    【讨论】:

      【解决方案2】:

      我在 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"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-01
        • 2019-02-08
        • 1970-01-01
        • 1970-01-01
        • 2020-03-31
        • 2012-05-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多