Skip to content

Commit cf1723a

Browse files
committed
Prefetch with cache redirect
1 parent e1c09b2 commit cf1723a

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/components/Section.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,26 @@ import gql from 'graphql-tag'
66
import { withRouter } from 'react-router'
77
import get from 'lodash/get'
88
import debounce from 'lodash/debounce'
9+
import { ApolloClient } from 'apollo-client'
910

1011
import { deslugify } from '../lib/helpers'
1112

1213
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+
1329
viewedSection = id => {
1430
if (!id) {
1531
return
@@ -30,8 +46,7 @@ class Section extends Component {
3046
window.addEventListener('scroll', this.handleScroll)
3147

3248
if (this.props.section) {
33-
this.viewedSection(this.props.section.id)
34-
this.updateScrollPosition()
49+
this.onSectionChange(this.props.section.id)
3550
}
3651
}
3752

@@ -61,8 +76,7 @@ class Section extends Component {
6176
const sectionChanged = get(prevProps, 'section.id') !== id
6277

6378
if (sectionChanged) {
64-
this.viewedSection(id)
65-
this.updateScrollPosition()
79+
this.onSectionChange(id)
6680
}
6781
}
6882

@@ -134,9 +148,24 @@ Section.propTypes = {
134148
}),
135149
loading: PropTypes.bool.isRequired,
136150
viewedSection: PropTypes.func.isRequired,
137-
setScrollPosition: PropTypes.func.isRequired
151+
setScrollPosition: PropTypes.func.isRequired,
152+
client: PropTypes.instanceOf(ApolloClient).isRequired
138153
}
139154

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+
140169
export const SECTION_BY_ID_QUERY = gql`
141170
query SectionContent($id: String!) {
142171
section(id: $id) {
@@ -241,6 +270,7 @@ const SectionWithData = ({ location: { state, pathname } }) => {
241270
{setScrollPosition => (
242271
<Section
243272
{...createProps(queryInfo)}
273+
client={queryInfo.client}
244274
viewedSection={viewedSection}
245275
setScrollPosition={setScrollPosition}
246276
/>

src/lib/apollo.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ const networkLink = split(
5050
authedHttpLink
5151
)
5252

53-
const cache = new InMemoryCache()
53+
const cache = new InMemoryCache({
54+
cacheRedirects: {
55+
Query: {
56+
section: (_, { id }, { getCacheKey }) =>
57+
getCacheKey({ __typename: 'Section', id })
58+
}
59+
}
60+
})
5461

5562
const stateLink = withClientState({
5663
cache,

0 commit comments

Comments
 (0)