天天看點

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

使用伺服器端渲染,我們可以保證搜尋引擎,與浏覽器的Javascript禁用,或沒有JavaScript的浏覽器仍然可以通路我們的網站内容。 https://b2bspastore.cg79x9wuu9-eccommerc1-p5-public.model-t.myhybris.cloud/powertools-spa/en/USD/ 文檔: https://sap.github.io/spartacus-docs/server-side-rendering-in-spartacus/#adding-ssr-support-using-schematics-recommended package.json裡添加如下依賴:

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

  • @angular/platform-server

允許我們在伺服器上運作Angular應用程式的技術, 在Angular文檔中稱為Angular Universal.

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

Angular Universal通過稱為伺服器端渲染(SSR)的過程在伺服器上生成靜态應用程式頁面。

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

根AppModule

打開檔案src/app/app.module.ts并在NgModule中繼資料中找到BrowserModule導入。

将該導入替換為此:

BrowserModule.withServerTransition({appId : ‘your App-ID’ });

如下圖所示:

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

使用以下AppServerModule代碼在src/app/目錄中建立一個app.server.module.ts檔案

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

使用以下代碼在src目錄中建立一個main.server.ts檔案:

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

Angular在服務端渲染方面提供一套前後端同構解決方案,它就是 Angular Universal(統一平台),一項在服務端運作 Angular 應用的技術。

标準的 Angular 應用會執行在浏覽器中,它會在 DOM 中渲染頁面,以響應使用者的操作。

而 Angular Universal 會在服務端通過一個被稱為服務端渲染(server-side rendering - SSR)的過程生成靜态的應用頁面。

它可以生成這些頁面,并在浏覽器請求時直接用它們給出響應。 它也可以把頁面預先生成為 HTML 檔案,然後把它們作為靜态檔案供服務端使用。

工作原理

要制作一個 Universal 應用,就要安裝 platform-server 包。 platform-server 包提供了服務端的 DOM 實作、XMLHttpRequest 和其它底層特性,但不再依賴浏覽器。

你要使用 platform-server 子產品而不是 platform-browser 子產品來編譯這個用戶端應用,并且在一個 Web 伺服器上運作這個 Universal 應用。

伺服器(下面的示例中使用的是 Node Express 伺服器)會把用戶端對應用頁面的請求傳給 renderModuleFactory 函數。SEO

網絡爬蟲不會像人類那樣導航到具有高度互動性的 Spartacus Angular 應用,并為其建立索引。

Angular Universal 可以為你生成應用的靜态版本,它易搜尋、可連結,浏覽時也不必借助 JavaScript。它也讓站點可以被預覽,因為每個 URL 傳回的都是一個完全渲染好的頁面。

package.json裡添加dev dependency:

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。
  • 用于對服務端應用進行轉譯。

這些檔案均需要手動修改:

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

1. AppServerModule
SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。
參考連結: https://www.cnblogs.com/laixiangran/p/8664480.html

服務端應用子產品(習慣上叫作 AppServerModule)是一個 Angular 子產品,它包裝了應用的根子產品 AppModule,以便 Universal 可以在你的應用和伺服器之間進行協調。 AppServerModule 還會告訴 Angular 在把你的應用以 Universal 方式運作時,該如何引導它。

最後build的dist目錄:

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

client side render(CSR,用戶端渲染)模式下,第一個HTTP請求,傳回的HTML頁面裡,cx-storefront标簽是空的:

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

SSR模式下,第一個HTTP請求裡,包含了伺服器端渲染好的HTML頁面:

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。

也可以參考這個文檔,進行SAP Spartacus SSR的調試:

How to Debug a Server–Side Rendered Storefront

另一個具體開啟 SSR 的步驟

create fresh angular app $ ng new

enter the directory

install spartacus with ssr v3.1.0 using schematics $ng add @spartacus/[email protected] --ssr

set backend baseUrl in app.module of the fresh app

disable ssr optimizations by passing null as a second argument of const ngExpressEngine = NgExpressEngineDecorator.get(engine, null); in the file server.ts of your fresh app

if your test backend doesn’t have proper SSL cert, please add at the top of the file server.ts the line:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = ‘0’;

copy i18n JSON assets to your app:

cp ./node_modules/@spartacus/assets/i18n-assets ./src/assets -r

in app.module set i18n.debug to true and i18n.backend.loadPath to ‘assets/i18n-assets/{{lng}}/{{ns}}.json’,:

i18n: {

chunks: translationChunksConfig,

backend: {

loadPath: ‘assets/i18n-assets/{{lng}}/{{ns}}.json’,

},

  1. debug: true,
  2. disable Javascript in your browser
  3. run $ yarn dev:ssr
  4. open in the browser http://localhost:4200/

暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法
SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟1. AppServerModule參考連結:https://www.cnblogs.com/laixiangran/p/8664480.html暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法傳一個 null 進去即可。
傳一個 null 進去即可。

繼續閱讀