【发布时间】: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