简体   繁体   English

Angular 通用生产构建过程

[英]Angular Universal Production Build Process

I'm using Angular Universal 10 and I want to prerender my site.我正在使用 Angular Universal 10,我想预渲染我的网站。

I'm confused on the proper build process for a production environment.我对生产环境的正确构建过程感到困惑。

Angular gives these two commands: Angular 给出了这两个命令:

"build:ssr": "ng build --prod && ng run my-app.spa:server:production",
"prerender": "ng run my-app.spa:prerender"

Is it enough to just do this to get a production ready output?这样做是否足以让 output 做好生产准备?

npm run prerender

Or do I need to run both of these?还是我需要同时运行这两个?

npm run build:ssr
npm run prerender

What's the difference?有什么不同?

To understand the difference between these two commands, run one and look at the compiled output.要了解这两个命令之间的区别,请运行一个并查看已编译的 output。

The first command npm run build:ssr will compile your site into a file that can be served using a NodeJS Express app, as you see in server.ts .第一个命令npm run build:ssr会将您的站点编译为可以使用 NodeJS Express 应用程序提供的文件,如您在server.ts中所见。 This way you will still only have one index.html file, but depending on your route, universal will deliver a rendered html file for that route to the browser.这样,您仍然只有一个 index.html 文件,但根据您的路线,universal 将为该路线提供一个渲染的 html 文件到浏览器。 So, you are rendering on the server, server-side rendering .所以,你在服务器上渲染,服务器端渲染

The second command npm run prerender will use the server.ts to "pre-render" each of the routes with their own individual index.html file, all placed in a directory structure according to your routes.第二个命令npm run prerender将使用server.ts来“预渲染”每个带有自己单独索引的路由。html 文件,所有这些都根据您的路由放置在目录结构中。 The compiled code looks like a traditional static website.编译后的代码看起来像一个传统的 static 网站。 So, you are rendering before you deploy, pre-rendering .因此,您在部署之前进行渲染,预渲染.

So which is best for you?那么哪个最适合您?

build:ssr

  • advantage: You don't need to redeploy if content is created dynamically优点:如果内容是动态创建的,则不需要重新部署
  • disadvantage: server has to be running a reliable and fast NodeJS process at all times.缺点:服务器必须始终运行可靠且快速的 NodeJS 进程。 The good news is there are lots of ways to do this, eg, serverless functions, app engine, etc.好消息是有很多方法可以做到这一点,例如,无服务器功能、应用程序引擎等。

prerender

  • advantage: your app can be hosted on a regular http server优势:您的应用程序可以托管在常规 http 服务器上
  • disadvantage: you will probably need to redeploy when new content pages are added缺点:添加新内容页面时可能需要重新部署

This post on Medium has a pretty good overview with more detailed information. 这篇关于 Medium 的帖子有一个很好的概述,其中包含更详细的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM