Skip to content

Commit

Permalink
fix(ConfigProvider): side effects of constructor on SSR, close #2920
Browse files Browse the repository at this point in the history
  • Loading branch information
youluna committed Apr 25, 2021
1 parent 63c445b commit 69985d6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 4 additions & 3 deletions site/zh-cn/contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,19 @@ git checkout -b fix-issue-100
## 开发注意事项

1. Fusion Next 作为前端组件库支持 SSR 因此需要做到:
- 尽量避免使用 window 等客户端变量 ( server端没有window对象,如果使用需要从window开始逐级判断是否存在 )
- 客户端端对象的判断用typeof
- 尽量避免在 `constructor` `getDerivedStateFromProps` `componentWillMount (deprecated)` 这些生命周期中,使用 `window` `localStorage` `sessionStorage` `document` `navigator` 等客户端变量;
- 必须使用的话,客户端端对象的判断用typeof
```
if(window && window.autoScroll)
=>
if(typeof window != undefined && window.autoScroll) )
```
- 避免往window等全局对象挂载定时器 (可能内存泄漏)
- 避免往window等全局对象挂载定时器
- 避免random()等不确定性输出(输出结果可预期,不依赖于环境等)
2. sass 颜色变量计算的结果,需要以 `$color-calcualte-` 开头,写到组件的 variable.scss 中(不能写到main.scss中),参考`Search`组件,[#1029](https://github.com/alibaba-fusion/next/issues/1029)
3. 所有sass计算需要被calc包裹
> 《Tips for server-side rendering with React》 https://itnext.io/tips-for-server-side-rendering-with-react-e42b1b7acd57
## 发布周期
Expand Down
6 changes: 5 additions & 1 deletion src/config-provider/cache.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Cache {
constructor() {
this._root = null;
this._store = new Map();
this._store = new WeakMap();
}

empty() {
Expand Down Expand Up @@ -41,6 +41,10 @@ class Cache {
}
}

clear() {
this._store = new WeakMap();
}

root() {
return this._store.get(this._root);
}
Expand Down
4 changes: 4 additions & 0 deletions src/config-provider/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ class ConfigProvider extends Component {
return getContextProps(props, childContextCache.root() || {}, displayName);
};

static clearCache = () => {
childContextCache.clear();
};

static initLocales = initLocales;
static setLanguage = setLanguage;
static setLocale = setLocale;
Expand Down

0 comments on commit 69985d6

Please sign in to comment.