日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

手动为 SAP Spartacus 添加 SSR 支持的步骤

發布時間:2023/12/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 手动为 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.

做兩件事情:

  • Adds server-side rendering dependencies.
  • 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 步驟

  • 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”

  • 添加下列 devDependencies 到 package.json 里:
    • “ts-loader”: “^6.0.4”,
    • “@nguniversal/builders”: “^10.1.0”,
    • “@types/express”: “^4.17.0”,
  • 添加下列腳本到 package.json:
    • “e2e”: “ng e2e”,
    • “dev:ssr”: “ng run :serve-ssr” - 注意,不是 npm run,后者定義在 package.json 里。

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

    • “serve:ssr”: “node dist//server/main.js”,
    • “build:ssr”: “ng build --prod && ng run :server:production”,
    • “prerender”: “ng run :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/BrowserTransferStateModule

    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的原創文章,盡在:“汪子熙”:

    總結

    以上是生活随笔為你收集整理的手动为 SAP Spartacus 添加 SSR 支持的步骤的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。