简体   繁体   English

Angular Universal/Prerendering 会加载动态加载的内容吗?

[英]Will Angular Universal/Prerendering Load Content That Is Dynamically Loaded?

I have an angular web app that loads data (products) from Firebase Firestore in the constructor of components.我有一个 angular web 应用程序,它在组件的构造函数中从 Firebase Firestore 加载数据(产品)。 Then it displays the list of products on the page.然后它会在页面上显示产品列表。 While the data is dynamic, it does not change very often.虽然数据是动态的,但它不会经常改变。 For example, the price may change but the products themselves stay there.例如,价格可能会发生变化,但产品本身会保持不变。

I would like to implement Angular Universal and Prerendering but I want to confirm that the list of products loaded in the constructor would be included in the prerendered static pages.我想实现 Angular Universal 和 Prerendering,但我想确认构造函数中加载的产品列表将包含在预渲染的静态页面中。

Is that true?真的吗?

Also, is it possible to write a function to "re-prerender" maybe nightly or when the DB is updated?另外,是否可以在每晚或更新数据库时编写一个函数来“重新预渲染”?

Static pre-rendering is possible with Angular Universal as well. Angular Universal 也可以进行静态预渲染。 Any requests will then be made during the build time.然后将在构建期间提出任何请求。

The quickest ways to try this out is to use the schematic via the command:最快的尝试方法是通过以下命令使用原理图

ng add @ng-toolkit/universal

Take a look at the created prerender.ts and the changes in your scripts within the package.json .查看创建的prerender.ts以及package.json scripts的更改。

In the file static.paths.ts you can define the routes that should be pre-rendered:在文件static.paths.ts您可以定义应该预渲染的路由:

export const ROUTES = [
  '/commits/yanxch'
];

When you then run npm run build:prerender you should see a new static folder within the dist folder.当您运行npm run build:prerender您应该会在dist文件夹中看到一个新的static文件夹。

Basically the response state of the requests during build time is put into the index.html file under <script id="serverApp-state">..</script>基本上,构建时请求的响应状态被放入<script id="serverApp-state">..</script>下的index.html文件中

This solution uses the so called ServerTransferStateModule and the TransferHttpCacheModule on the browser side to avoid that requests are made twice (on the server during build and when the client side application is started).该解决方案在浏览器端使用所谓的ServerTransferStateModuleTransferHttpCacheModule来避免两次请求(在构建期间和客户端应用程序启动时在服务器上)。

You can trigger this during a nightly build and then deploy the newly built application but that procedure depends more on the needs and the setup of your build pipeline.您可以在每晚构建期间触发此操作,然后部署新构建的应用程序,但该过程更多地取决于构建管道的需求和设置。 So yes, a build script that does a DB-Query and then executes a new prerender build is possible as well.所以是的,一个执行 DB-Query 然后执行新的预渲染构建的构建脚本也是可能的。

You can also take look at my example .你也可以看看我的例子

Angular Universal would allow you to load the content (in your case, product list) in the page before the page is served. Angular Universal 允许您在提供页面之前加载页面中的内容(在您的情况下,产品列表)。 However, it does not output static pages ahead of time, and usually you'll find yourself conditionally running certain code server-side and certain code client-side (using isPlatformServer and isPlatformBrowser).但是,它不会提前输出静态页面,通常您会发现自己有条件地运行某些服务器端代码和某些客户端代码(使用 isPlatformServer 和 isPlatformBrowser)。 Also worth noting that you will need to disable, clean, or substitute DOM references and other browser dependent code for it to run on the server (Using Angular references, such as ViewChildren, will behave properly in Universal/server-side).另外值得注意的是,您需要禁用、清理或替换 DOM 引用和其他浏览器相关代码才能在服务器上运行(使用 Angular 引用,例如 ViewChildren,将在通用/服务器端正常运行)。

As for regular prerendering, Universal is designed to deliver the application/page on demand, and does not output pages ahead of time.对于常规预渲染,Universal 旨在按需交付应用程序/页面,并且不会提前输出页面。 If you're looking to reduce load, I'd recommend a combination of URL params and caching (such as with a CDN).如果您想减少负载,我建议结合使用 URL 参数和缓存(例如 CDN)。

Once you've got it set up, and embrace that the code needs to support a browser-less environment, Angular Universal is really neat and allows you to do some unique things.一旦你设置好它,并接受代码需要支持无浏览器的环境,Angular Universal 真的很简洁,允许你做一些独特的事情。 I consider it more of a server-render, client-render hybrid.我认为它更像是服务器-渲染、客户端-渲染的混合体。 However, it isn't a "plug and play" solution to prerendering a page.但是,它不是预渲染页面的“即插即用”解决方案。

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

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