SAP Spartacus 3.3.0 版本服务器端渲染的优化
https://sap.github.io/spartacus-docs/server-side-rendering-optimization/
為什么要引入服務器端渲染優化?
Without SSR optimization, it is possible for the following to occur:
- Pages do not render quickly enough, which leads to SSR using too much memory, and eventually it fails.
如果頁面渲染不夠快,可能會導致 SSR 使用了太多內存,最終導致渲染失敗;
- A response is not received in time, and an HTTP 500 error is returned.
無法按時接收到響應,只得返回 HTTP 500 錯誤給客戶端。
Memory leaks are often the result of issues with the SSR implementation, while failover can happen when the SSR response is not cached.
SSR 實現中的 bug 通常會導致內存泄漏,而 SSR 響應如果不緩存下來,可能會導致故障。
啟用 SSR 優化引擎的方式:
To enable the SSR optimization engine, use the NgExpressEngineDecorator decorator in your server.ts file. The following is an example:
import { ngExpressEngine as engine } from '@nguniversal/express-engine'; import { NgExpressEngineDecorator } from '@spartacus/setup/ssr';[...]const ngExpressEngine = NgExpressEngineDecorator.get(engine);[...]// In the app() functionserver.engine('html',ngExpressEngine({bootstrap: AppServerModule,}) );SSR 優化引擎的參數
timeout
a number that indicates the amount of time (in milliseconds) during which the SSR server tries to render a page, before falling back to CSR.
該配置的單位是毫秒,代表 SSR 服務器在退化到使用客戶端渲染之前,能夠消耗用于渲染頁面的時間。如果過了這個時間間隔,還未完成服務器端渲染,則返回 CSR 的 index.html 給客戶端。該 index.html 不包含任何渲染好的 markup.
The CSR app (index.html) is served with a Cache-Control:no-store header.
這個 index.html 返回時帶有 Cache-Control:no-store 頭部。
As a result, it is not stored by the cache layer.
因此,index.html 不會存儲在 Cache layer.
SSR pages do not contain this header because it is preferable to cache SSR pages.
服務器端頁面不包含 Cache-Control:no-store 頭部字段,因為 SSR 渲染好的頁面需要被緩存。
In the background, the SSR server continues to render the SSR version of the page. Once this rendering finishes, the page is placed in a local cache to be returned the next time it is requested.
SSR 在后臺持續渲染頁面的服務器端版本。渲染結束后,存儲到本地緩存里,下次被請求時,直接從緩存里返回。
By default, the server clears the page from its cache after returning it for the first time. It is assumed that you are using an additional layer to cache pages externally.
總結
以上是生活随笔為你收集整理的SAP Spartacus 3.3.0 版本服务器端渲染的优化的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 限售期限是什么意思
- 下一篇: SAP Spartacus 服务器端渲染