@@ -6,10 +6,26 @@ import gql from 'graphql-tag'
6
6
import { withRouter } from 'react-router'
7
7
import get from 'lodash/get'
8
8
import debounce from 'lodash/debounce'
9
+ import { ApolloClient } from 'apollo-client'
9
10
10
11
import { deslugify } from '../lib/helpers'
11
12
12
13
class Section extends Component {
14
+ onSectionChange = newId => {
15
+ this . viewedSection ( newId )
16
+ this . updateScrollPosition ( )
17
+ this . prefetchNextSection ( newId )
18
+ }
19
+
20
+ prefetchNextSection = currentId => {
21
+ this . props . client . query ( {
22
+ query : NEXT_SECTION_QUERY ,
23
+ variables : {
24
+ id : currentId
25
+ }
26
+ } )
27
+ }
28
+
13
29
viewedSection = id => {
14
30
if ( ! id ) {
15
31
return
@@ -30,8 +46,7 @@ class Section extends Component {
30
46
window . addEventListener ( 'scroll' , this . handleScroll )
31
47
32
48
if ( this . props . section ) {
33
- this . viewedSection ( this . props . section . id )
34
- this . updateScrollPosition ( )
49
+ this . onSectionChange ( this . props . section . id )
35
50
}
36
51
}
37
52
@@ -61,8 +76,7 @@ class Section extends Component {
61
76
const sectionChanged = get ( prevProps , 'section.id' ) !== id
62
77
63
78
if ( sectionChanged ) {
64
- this . viewedSection ( id )
65
- this . updateScrollPosition ( )
79
+ this . onSectionChange ( id )
66
80
}
67
81
}
68
82
@@ -134,9 +148,24 @@ Section.propTypes = {
134
148
} ) ,
135
149
loading : PropTypes . bool . isRequired ,
136
150
viewedSection : PropTypes . func . isRequired ,
137
- setScrollPosition : PropTypes . func . isRequired
151
+ setScrollPosition : PropTypes . func . isRequired ,
152
+ client : PropTypes . instanceOf ( ApolloClient ) . isRequired
138
153
}
139
154
155
+ const NEXT_SECTION_QUERY = gql `
156
+ query NextSection($id: String!) {
157
+ section(id: $id) {
158
+ id
159
+ next {
160
+ id
161
+ content
162
+ views
163
+ scrollY @client
164
+ }
165
+ }
166
+ }
167
+ `
168
+
140
169
export const SECTION_BY_ID_QUERY = gql `
141
170
query SectionContent($id: String!) {
142
171
section(id: $id) {
@@ -241,6 +270,7 @@ const SectionWithData = ({ location: { state, pathname } }) => {
241
270
{ setScrollPosition => (
242
271
< Section
243
272
{ ...createProps ( queryInfo ) }
273
+ client = { queryInfo . client }
244
274
viewedSection = { viewedSection }
245
275
setScrollPosition = { setScrollPosition }
246
276
/>
0 commit comments