Skip to content

Commit

Permalink
chore: 使用pathname作为namespace来隔离组件的首次render的props获取防止属性污染
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Mar 27, 2020
1 parent dcb939c commit 28cbc14
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion example/ssr-with-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ssr-with-js",
"version": "2.3.0",
"version": "2.3.1",
"dependencies": {
"egg": "^2.21.0",
"egg-scripts": "^2.11.0",
Expand Down
2 changes: 1 addition & 1 deletion example/ssr-with-js/web/layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const Layout = (props) => {
<div id='app'>{ commonNode(props) }</div>
{
serverData && <script dangerouslySetInnerHTML={{
__html: `window.__USE_SSR__=true; window.__INITIAL_DATA__ =${serialize(serverData)}`
__html: `window.__USE_SSR__=true; window.__INITIAL_DATA__ ={"${props.layoutData.req.url}":${serialize(serverData)}}` // 使用pathname作为组件初始化数据的隔离,防止props污染
}} />
}
<div dangerouslySetInnerHTML={{
Expand Down
2 changes: 1 addition & 1 deletion example/ssr-with-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ssr-with-ts",
"version": "2.3.7",
"version": "2.3.8",
"description": "ykfe",
"dependencies": {
"egg-scripts": "^2.10.0",
Expand Down
2 changes: 1 addition & 1 deletion example/ssr-with-ts/web/layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const Layout: SFC<LayoutProps> = (props: LayoutProps): JSX.Element | null => {
<div id='app'>{commonNode(props)}</div>
{
serverData && <script dangerouslySetInnerHTML={{
__html: `window.__USE_SSR__=true; window.__INITIAL_DATA__ =${serialize(serverData)}`
__html: `window.__USE_SSR__=true; window.__INITIAL_DATA__ ={"${props.layoutData!.req.url}":${serialize(serverData)}}` // tslint:disable-line
}} />
}
<div dangerouslySetInnerHTML={{
Expand Down
2 changes: 1 addition & 1 deletion packages/ykfe-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ykfe-utils",
"version": "3.5.9",
"version": "3.5.10",
"description": "utils for ykfe",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
14 changes: 7 additions & 7 deletions packages/ykfe-utils/src/components/getinitialProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const popStateFn = () => {
}

interface IState {
getProps: boolean,
initialProps: any
extraProps: Object
}

Expand All @@ -21,17 +21,18 @@ function GetInitialProps (WrappedComponent: FC): React.ComponentClass {
super(props)
this.state = {
extraProps: {},
getProps: false
initialProps: window.__INITIAL_DATA__ && window.__INITIAL_DATA__[location.pathname] // 使用pathname来防止不同页面的初始props冲突
}
}

async componentDidMount () {
const props = this.props
if (window.__USE_SSR__) {
_this = this // 修正_this指向,保证_this指向当前渲染的页面组件
window.addEventListener('popstate', popStateFn)
window.addEventListener('popstate', popStateFn) // history.pushState和history.replaceState方法并不会触发popstate事件。因此需要另外判断当前action是否为push/replace
}
const getProps = !window.__USE_SSR__ || (props.history && props.history.action === 'PUSH')
const getProps = !window.__USE_SSR__ || props.history && props.history.action === ('PUSH' || 'REPLACE')

if (getProps) {
await this.getInitialProps()
}
Expand All @@ -46,14 +47,13 @@ function GetInitialProps (WrappedComponent: FC): React.ComponentClass {
}
const extraProps = WrappedComponent.getInitialProps ? await WrappedComponent.getInitialProps(props) : {}
this.setState({
extraProps,
getProps: true
extraProps
})
}

render () {
// 只有在首次进入页面需要将window.__INITIAL_DATA__作为props,路由切换时不需要
return <WrappedComponent {...Object.assign({}, this.props, this.state.getProps ? {} : window.__INITIAL_DATA__, this.state.extraProps)} />
return <WrappedComponent {...Object.assign({}, this.props, window.__INITIAL_DATA__, this.state.initialProps, this.state.extraProps)} />
}
}
return withRouter(GetInitialPropsClass)
Expand Down

0 comments on commit 28cbc14

Please sign in to comment.