動態

詳情 返回 返回

手動為 SAP Spartacus 添加 SSR 即服務器端渲染的步驟 - 動態 詳情

ng add @spartacus/schematics --ssr

在用 SAP Spartacus 開發的 store 裏,能看到 devDependencies 裏具有 @spartacus/schematics 的依賴:

這是 SAP Spartacus Schematics 的一部分:https://sap.github.io/spartacus-docs/schematics.

Spartacus schematics allow you to install Spartacus libraries in your project.

Spartacus Schematics 允許我們在自己的項目裏安裝 Spartacus 庫。SAP Spartacus Schematics 本身的幫助文檔:https://github.com/SAP/spartacus/blob/develop/projects/schematics/README.md

命令:ng add @spartacus/schematics@latest

支持的一些重要 option:

  • featureLevel sets the application feature level. The default value is the same as the version of the Spartacus packages you are working with. For example, the featureLevel for @spartacus/schematics@3.2.0 is 3.2.

featureLevel:設置應用的 feature level,默認值和 Spartacus package level 一致。

  • ssr includes the server-side rendering (SSR) configuration.

做兩件事情:

  1. Adds server-side rendering dependencies.
  2. Provides additional files that are required for SSR.

使用 option 的語法:

ng add @spartacus/schematics@latest --baseUrl https://spartacus-demo.eastus.cloudapp.azure.com:8443/ --baseSite=apparel-uk-spa,electronics-spa --currency=gbp,usd --language=uk,en --ssr

手動添加 SSR support 步驟

  1. package.json 裏添加依賴:
  • "@angular/platform-server": "~10.1.0",
  • "@nguniversal/express-engine": "^10.1.0"

https://github.com/angular/universal/tree/master/modules/express-engine

Express Engine 的作用是為了在服務器端運行 Angular 應用讓其支持服務器端渲染。

  • "@spartacus/setup": "^3.0.0-rc.2",

  • "express": "^4.15.2"

  1. 添加下列 devDependencies 到 package.json 裏:
  • "ts-loader": "^6.0.4",
  • "@nguniversal/builders": "^10.1.0",
  • "@types/express": "^4.17.0",
  1. 添加下列腳本到 package.json:
  • "e2e": "ng e2e",
  • "dev:ssr": "ng run <your-project-name>:serve-ssr" - 注意,不是 npm run,後者定義在 package.json 裏。

而這個 serve-ssr 定義在 angular.json 的 architect 區域裏:

  • "serve:ssr": "node dist/<your-project-name>/server/main.js",
  • "build:ssr": "ng build --prod && ng run <your-project-name>:server:production",
  • "prerender": "ng run <your-project-name>:prerender"

修改 src/main.ts:

之前的代碼:

 platformBrowserDynamic().bootstrapModule(AppModule);

修改之後的代碼:

 document.addEventListener("DOMContentLoaded", () => {
   platformBrowserDynamic().bootstrapModule(AppModule);
 });

這裏的 platformBrowserDynamic 是什麼意思?參考這篇知乎文章什麼是 Angular Platforms - 深入理解 Angular Platforms

Angular 框架的設計初衷是將其打造成一個獨立平台。這樣的設計思路確保了 Angular 應用可以正常地跨環境執行 - 無論是在瀏覽器,服務端,web-worker 甚至是在移動設備上。

當我們通過 Angular CLI 指令 ng new MyNewApplication 創建一個新的 Angular 應用時,應用的默認環境設置為瀏覽器環境。

platformBrowserDynamic 函數的返回結果是一個 PlatformRef。 PlatformRef 只是一個 Angular 服務,該服務知曉如何引導啓動 Angular 應用。

Angular 高度依賴於依賴注入系統。這也是為什麼 Angular 本身很大一部分內容被聲明為抽象服務的原因, 比如 Renderer2.

下圖中間藍色圓圈代表抽象類,上下的綠色和紫色,分別代表 Browser Platform 和 Server Platform 的具體實現。

DomAdapter:

  • BrowserDomAdapter - 瀏覽器平台
  • DominoAdapter - 服務器端平台,不與 DOM 進行交互,因為在服務端無法獲取 DOM.除此之外,其還會使用 domino library,在 node.js 環境中模擬 DOM.

app.module.ts:

BrowserModule.withServerTransition({ appId: 'spartacus-app' }),

Configures a browser-based app to transition from a server-rendered app, if one is present on the page.

@param params — An object containing an identifier for the app to transition. The ID must match between the client and server versions of the app.

BrowserTransferStateModule: NgModule to install on the client side while using the TransferState to transfer state from server to client.

https://angular.io/api/platform-browser/BrowserTransferStateM...

TransferState: A key value store that is transferred from the application on the server side to the application on the client side.

一個 key value 存儲結構,從服務器端應用轉移到客户端應用。

TransferState will be available as an injectable token. To use it import ServerTransferStateModule on the server and BrowserTransferStateModule on the client.

關閉 PWA

As soon as Spartacus is installed in PWA mode, a service worker is installed, and it serves a cached version of index.html, along with the js files. This results in SSR being completely skipped.

Spartacus 如果以 PWA 模式安裝,則 service worker 啓動,提供一個緩存後的 index.html, 以及相關的 JavaScript 文件。這會導致 SSR 完全被忽略掉。

在代碼裏關閉 PWA:

StorefrontModule.withConfig({
       backend: {
         occ: {
           baseUrl: 'https://[your_enpdoint],
         },
       },
       pwa: {
         enabled: false,
       },
 };
user avatar yelloxing 頭像 7mandy7 頭像 code500g 頭像 lintp 頭像 huaweichenai 頭像 jueqiangqingtongsan 頭像 beiyinglunkuo 頭像 baozouai 頭像 lindexi 頭像 gplml 頭像
點贊 10 用戶, 點贊了這篇動態!
點贊

Add a new 評論

Some HTML is okay.