SAP Spartacus一个客户项目无法启用SSR服务器端渲染的实际例子和分析方法

简介: SAP Spartacus一个客户项目无法启用SSR服务器端渲染的实际例子和分析方法

Customer support - SSR not working

Main problem

In the codebase I found multiple instances of access to properties inside this.windowRef.nativeWindow.


This works correctly on client side, but on server side (SSR) nativeWindow is undefined.


Recommendation

Always validate if the code is executing in browser or on the server.


Best practice

Use PLATFORM_ID with utility isPlatformBrowser to detect if you can safely access nativeWindow. Alternatively check if nativeWindow is defined.


Example:

image.pngFiles to fix

Completely breaking SSR:


digital-data.adapter.ts

custom-occ-cms-page-normalizer.ts

Additionally search for any usage of nativeWindow and check if it is accessed safely.


How to speed up debugging?

Fix script dev:ssr.


Add to architect in angular.json

image.pngOther issues

There are other kind of access to browser API without using WindowRef.nativeWindow. Eg. sessionStorage, screen, localStorage. Same rule mentioned above applies here. It’s always better to check if you can execute such code (not doing it in SSR).


What we will improve in Spartacus

To make it more clear to everyone that nativeWindow is not always available we will fix TS types for nativeWindow to indicate that it can be undefined along with TSDoc comment documenting that it can be undefined and how to check if you can use it.


也可以参考这个文档,进行SAP Spartacus SSR的调试:


How to Debug a Server–Side Rendered Storefront


2021-1-29 4:16PM 更新

We found out that the problem was caused by using this.windowAdapter.getWindow().sessionStorage.* without previously checking if the sessionStorage is actually available. In SSR it was undefined.


If you wrap all the calls in an if (this.windowAdapter.getWindow().sessionStorage) {...} the PLP pages are being SSRed correctly.


As an additional, browser’s storage (sessionStorage, localStorage) API is not available in SSR, therefore code defensively.


相关文章
SAP 电商云 Spartacus UI 客户端模式下从服务器端渲染模式接收到的状态转移
SAP 电商云 Spartacus UI 客户端模式下从服务器端渲染模式接收到的状态转移
|
资源调度
SAP 电商云 Spartacus UI 修改代码后,重新构建基于 SSR 版本的程序报错
SAP 电商云 Spartacus UI 修改代码后,重新构建基于 SSR 版本的程序报错
|
Web App开发 API
SAP 电商云 Spartacus UI SSR 模式下发起的 CMS 请求讨论
SAP 电商云 Spartacus UI SSR 模式下发起的 CMS 请求讨论
127 0
SAP 电商云 Spartacus UI SSR 模式下发起的 CMS 请求讨论
SAP 电商云 Spartacus UI 3.4.5 版本引入的对并发 SSR 请求的支持
SAP 电商云 Spartacus UI 3.4.5 版本引入的对并发 SSR 请求的支持
SAP 电商云 Spartacus UI 3.4.5 版本引入的对并发 SSR 请求的支持
SAP Spartacus 服务器端渲染的三种可能情形
SAP Spartacus 服务器端渲染的三种可能情形
SAP Spartacus 服务器端渲染的三种可能情形
SAP Spartacus 3.3.0 版本服务器端渲染的优化
SAP Spartacus 3.3.0 版本服务器端渲染的优化
111 0
SAP Spartacus 3.3.0 版本服务器端渲染的优化
|
存储 缓存
SAP Spartacus 服务器端渲染优化引擎的参数 SsrOptimizationOptions
SAP Spartacus 服务器端渲染优化引擎的参数 SsrOptimizationOptions
119 0
SAP Spartacus 服务器端渲染优化引擎的参数 SsrOptimizationOptions
试图安装客户出现SSR问题的Spartacus Storefront时遇到的错误消息
试图安装客户出现SSR问题的Spartacus Storefront时遇到的错误消息
试图安装客户出现SSR问题的Spartacus Storefront时遇到的错误消息
|
存储 缓存 CDN
SAP Spartacus 服务器端渲染处理内存泄漏的准则
开启服务器端渲染(SSR) 的 SAP Spartacus 实例,运行在 CCV2 Pod 里,当下列情况出现时,可能会造成 pod 的重启:
SAP Spartacus 服务器端渲染处理内存泄漏的准则
以非生产模式的方式启用SAP Spartacus的服务器端渲染SSR
以非生产模式的方式启用SAP Spartacus的服务器端渲染SSR
104 0
以非生产模式的方式启用SAP Spartacus的服务器端渲染SSR
下一篇
无影云桌面