我最近将Angular 8应用程序升级为通用应用程序(SSR)。在将它作为SSR应用程序之前,我已经将其部署到Firebase,但是后来我发现,使用常规Firebase托管无法在Firebase上部署SSR应用程序。我做了一些研究,发现我必须使用Firebase Cloud Functions。
我使用以下方法创建了SSR应用程序:
ng add @nguniversal/express-engine --clientProject PROJECT_NAME
[PROJECT_NAME
可以在angular.json
部分下面的"projects"
文件中找到。
有人可以帮我吗?
重要说明:此解决方案取自有关Udemy(here)的Angular课程的“问答”部分。我尝试了一下,并进行了一些修改使其得以工作。
因此,首先通过运行npm run build:ssr
和npm run serve:ssr
确保SSR确实有效。
然后安装Firebase Tools并初始化项目:
npm install -g firebase-tools
firebase login
,如果需要,提供您的Firebase凭据(电子邮件/密码)。firebase init
回答一些问题...
“您准备好继续了吗?”
键入y
并按ENTER。
“您要设置哪些Firebase CLI功能?”
选择...
(*) Functions
(*) Hosting
...,使用空格键选择两者,然后按Enter。
“为此目录选择默认的Firebase项目吗?”
使用箭头键选择一个,然后按Enter。
“您想使用哪种语言编写Cloud Functions?”
用箭头键选择TypeScript
,然后按ENTER。
“您要使用TSLint吗?”
键入y
并按ENTER。
“您现在要使用npm安装依赖项吗?”
键入y
并按ENTER。
“您想使用什么作为公用目录?”
键入dist/browser
并按ENTER(请注意:这与部署不具有Universal的应用程序不同!)。]
“配置为单页应用程序吗?”
键入y
并按ENTER。
File index.html已经存在。覆盖?
键入n
(重要!),然后按ENTER键。
修改一些文件...
在firebase.json
中,将"destination": "/index.html"
替换为"function": "ssr"
([ssr
指向此export const ssr = functions.https.onRequest(universal);
变量,您将在下面找到它)。
在server.ts
export
添加到app
初始化中:export const app = express();
而不是const app = express();
在server.ts
app.listen(...)
)或将其替换为以下内容:// If we're not in the Cloud Functions environment, spin up a Node server if (!process.env.FUNCTION_NAME) { // Start up the Node server app.listen(PORT, () => { console.log(`Node Express server listening on http://localhost:${PORT}`); }); }
您可以在将它们部署到Firebase时将其删除,但在运行
npm run serve:ssr
时需要它们才能将应用程序托管在localhost
上。
output
:output: { // Puts the output at the root of the dist folder path: path.join(__dirname, 'dist'), // Export a UMD of the webpacked server.ts & dependencies for rendering in Cloud Functions library: 'app', libraryTarget: 'umd', filename: '[name].js', },
像这样修改
externals
:
错误:externals: [ // Firebase has some troubles being webpacked when it's in the Node environment, so we will skip it. /^firebase/ ],
这将修复
找不到模块'require(“ ./ server / main”)'
运行
npm run serve:ssr
或firebase serve
命令时。
通过运行npm run build:ssr
重建应用程序。
使用终端移动到功能文件夹:cd functions
安装用于文件系统访问的npm软件包:npm i fs-extra
在[[functions
const fs = require('fs-extra');
fs.copy('../dist', './dist').then(() => {
// We should remove the original "index.html" since Firebase will use it when SSR is enabled (instead of calling SSR functions),
// and because of that, SSR won't work for the initial page.
fs.remove('../dist/browser/index.html').catch(e => console.error('REMOVE ERROR: ', e));
}).catch(err => {
console.error('COPY ERROR: ', err)
});
此初始页面未作为SSR加载(代替显示初始页面的内容,它仍显示fixes
<app-root></app-root>
)。注意:
由于我们删除了index.html
文件,因此除非您首先重新构建应用程序(通过运行npm run serve:ssr
->这将重新创建npm run build:ssr
文件),否则运行index.html
将不起作用。 。"build": "node copy-angular-app && tsc",
import * as functions from 'firebase-functions';
const universal = require(`${process.cwd()}/dist/server`).app;
export const ssr = functions.https.onRequest(universal);
npm run build
将dist
文件夹复制到functions
文件夹。附加说明:为简化Firebase的构建,您可以在主项目的package.json
文件中创建脚本:
"build:ssr": "npm run build:client-and-server-bundles && npm run compile:server", // this one should already exist
"build:ssr-firebase": "npm run build:ssr && npm --prefix functions/ run build",
此脚本将首先构建您的Angular SSR应用程序(部署您的应用程序...npm run build:ssr
),然后将在functions
文件夹中运行npm run build
(这样它将把项目的dist
文件夹复制到您的functions] >dist
文件夹,将删除项目的index.html
文件。
firebase serve
(如果需要)在localhost:5000
firebase deploy
来部署应用程序,并通过终端中显示的URL对其进行访问。通过这种方式,我设法在Firebase上部署了Angular SSR应用程序。
希望这有帮助...