8000 batch update head contents (#129) · JavaScriptExpert/next.js@037e2d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 037e2d9

Browse files
authored
batch update head contents (vercel#129)
* head-manager: batch update * side-effect: remove redundant rendering cycle checks
1 parent 93d8567 commit 037e2d9

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

client/head-manager.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,20 @@ import HTMLDOMPropertyConfig from 'react/lib/HTMLDOMPropertyConfig'
33
const DEFAULT_TITLE = ''
44

55
export default class HeadManager {
6+
constuctor () {
7+
this.requestId = null
8+
}
9+
610
updateHead (head) {
11+
// perform batch update
12+
window.cancelAnimationFrame(this.requestId)
13+
this.requestId = window.requestAnimationFrame(() => {
14+
this.requestId = null
15+
this.doUpdateHead(head)
16+
})
17+
}
18+
19+
doUpdateHead (head) {
720
const tags = {}
821
head.forEach((h) => {
922
const components = tags[h.type] || []

lib/head.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ class Head extends React.Component {
99
render () {
1010
return null
1111
}
12-
13-
componentWillUnmount () {
14-
this.context.headManager.updateHead([])
15-
}
1612
}
1713

1814
function reduceComponents (components) {

lib/side-effect.js

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
2424

2525
const mountedInstances = new Set()
2626
let state
27-
let shouldEmitChange = false
2827

2928
function emitChange (component) {
3029
state = reduceComponentsToState([...mountedInstances])
@@ -36,12 +35,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
3635
}
3736
}
3837

39-
function maybeEmitChange (component) {
40-
if (!shouldEmitChange) return
41-
shouldEmitChange = false
42-
emitChange(component)
43-
}
44-
4538
class SideEffect extends Component {
4639
// Try to use displayName of wrapped component
4740
static displayName = `SideEffect(${getDisplayName(WrappedComponent)})`
@@ -60,8 +53,6 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
6053
throw new Error('You may only call rewind() on the server. Call peek() to read the current state.')
6154
}
6255

63-
maybeEmitChange()
64-
6556
const recordedState = state
6657
state = undefined
6758
mountedInstances.clear()
@@ -70,24 +61,15 @@ export default function withSideEffect (reduceComponentsToState, handleStateChan
7061

7162
componentWillMount () {
7263
mountedInstances.add(this)
73-
shouldEmitChange = true
74-
}
75-
76-
componentDidMount () {
77-
maybeEmitChange(this)
78-
}
79-
80-
componentWillUpdate () {
81-
shouldEmitChange = true
64+
emitChange(this)
8265
}
8366

8467
componentDidUpdate () {
85-
maybeEmitChange(this)
68+
emitChange(this)
8669
}
8770

8871
componentWillUnmount () {
8972
mountedInstances.delete(this)
90-
shouldEmitChange = false
9173
emitChange(this)
9274
}
9375

0 commit comments

Comments
 (0)
0