Skip to content

Commit 881daea

Browse files
committed
Persist the cache
1 parent cf1723a commit 881daea

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/components/App.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Component } from 'react'
22
import { Switch, Route, Redirect } from 'react-router'
33
import { Link } from 'react-router-dom'
4+
import { persistCache } from 'apollo-cache-persist'
45

56
import logo from '../logo.svg'
67
import StarCount from './StarCount'
@@ -10,6 +11,7 @@ import CurrentUser from './CurrentUser'
1011
import Profile from './Profile'
1112
import Reviews from './Reviews'
1213
import CurrentTemperature from './CurrentTemperature'
14+
import { cache } from '../lib/apollo'
1315

1416
const Book = ({ user }) => (
1517
<div>
@@ -21,8 +23,31 @@ const Book = ({ user }) => (
2123
</div>
2224
)
2325

26+
const cacheHasBeenSaved = !!localStorage.getItem('apollo-cache-persist')
27+
2428
class App extends Component {
29+
state = {
30+
loading: cacheHasBeenSaved
31+
}
32+
33+
async componentDidMount() {
34+
await persistCache({
35+
cache,
36+
storage: window.localStorage,
37+
maxSize: 4500000, // little less than 5 MB
38+
debug: true
39+
})
40+
41+
this.setState({
42+
loading: false
43+
})
44+
}
45+
2546
render() {
47+
if (this.state.loading) {
48+
return null
49+
}
50+
2651
return (
2752
<div className="App">
2853
<header className="App-header">

src/lib/apollo.js

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

53-
const cache = new InMemoryCache({
53+
export const cache = new InMemoryCache({
5454
cacheRedirects: {
5555
Query: {
5656
section: (_, { id }, { getCacheKey }) =>

0 commit comments

Comments
 (0)