手動爲 SAP Spartacus 添加 SSR 即服務器端渲染的步驟

ng add @spartacus/schematics --ssrhtml

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

這是 SAP Spartacus Schematics 的一部分:https://sap.github.io/spartac...git

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

Spartacus Schematics 容許咱們在本身的項目裏安裝 Spartacus 庫。SAP Spartacus Schematics 自己的幫助文檔:https://github.com/SAP/sparta...github

命令:ng add @spartacus/schematics@latestweb

支持的一些重要 option:typescript

  • 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 一致。express

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

作兩件事情:npm

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

使用 option 的語法:json

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

手動添加 SSR support 步驟

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

https://github.com/angular/un...

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/platfo...

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,
       },
 };

更多Jerry的原創文章,盡在:"汪子熙":

相關文章
相關標籤/搜索