天天看點

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟BrowserModule.withServerTransition({appId : ‘your App-ID’ });

使用伺服器端渲染,我們可以保證搜尋引擎,與浏覽器的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裡添加如下依賴:

 "@angular/platform-server": "~10.1.0",

"@nguniversal/express-engine": "^10.1.0",

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

"express": "^4.15.2"

  • @angular/platform-server

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

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟BrowserModule.withServerTransition({appId : ‘your App-ID’ });

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

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟BrowserModule.withServerTransition({appId : ‘your App-ID’ });

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

将該導入替換為此:

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

如下圖所示:

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟BrowserModule.withServerTransition({appId : ‘your App-ID’ });

import { NgModule } from '@angular/core';

import {

 ServerModule,

 ServerTransferStateModule,

} from '@angular/platform-server';

import { StorefrontComponent } from '@spartacus/storefront';

import { AppModule } from './app.module';

@NgModule({

 imports: [

   // The AppServerModule should import your AppModule followed

   // by the ServerModule from @angular/platform-server.

   AppModule,

   ServerModule,

   ServerTransferStateModule,

 ],

 // Since the bootstrapped component is not inherited from your

 // imported AppModule, it needs to be repeated here.

 bootstrap: [StorefrontComponent],

})

export class AppServerModule {}

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟BrowserModule.withServerTransition({appId : ‘your App-ID’ });

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:

"ts-loader": "^6.0.4",

"@nguniversal/builders": "^10.1.0",

"@types/express": "^4.17.0",

1

2

3

用于對服務端應用進行轉譯。

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

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟BrowserModule.withServerTransition({appId : ‘your App-ID’ });
SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟BrowserModule.withServerTransition({appId : ‘your App-ID’ });

參考連結:

https://www.cnblogs.com/laixiangran/p/8664480.html

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

最後build的dist目錄:

SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟BrowserModule.withServerTransition({appId : ‘your App-ID’ });
SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟BrowserModule.withServerTransition({appId : ‘your App-ID’ });
SAP Spartacus 手動開啟伺服器端渲染 (SSR) 所必須的步驟BrowserModule.withServerTransition({appId : ‘your App-ID’ });

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’,

},

debug: true,

disable Javascript in your browser

run $ yarn dev:ssr

open in the browser http://localhost:4200/

暫時禁用 SAP Spartacus 伺服器端渲染引擎的方法

disable SSR Optimizations by passing null as a second parameter to:

NgExpressEngineDecorator.get(engine, null)

傳一個 null 進去即可。

繼續閱讀