Skip to content

Commit 676b5db

Browse files
committed
Purge cache on store reset
1 parent b1a6e16 commit 676b5db

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/components/App.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +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'
4+
import { CachePersistor } from 'apollo-cache-persist'
55

66
import logo from '../logo.svg'
77
import StarCount from './StarCount'
@@ -11,7 +11,7 @@ import CurrentUser from './CurrentUser'
1111
import Profile from './Profile'
1212
import Reviews from './Reviews'
1313
import CurrentTemperature from './CurrentTemperature'
14-
import { cache } from '../lib/apollo'
14+
import { cache, apollo } from '../lib/apollo'
1515

1616
const Book = ({ user }) => (
1717
<div>
@@ -23,6 +23,15 @@ const Book = ({ user }) => (
2323
</div>
2424
)
2525

26+
const persistor = new CachePersistor({
27+
cache,
28+
storage: window.localStorage,
29+
maxSize: 4500000, // little less than 5 MB
30+
debug: true
31+
})
32+
33+
apollo.onResetStore(() => persistor.purge())
34+
2635
const cacheHasBeenSaved = !!localStorage.getItem('apollo-cache-persist')
2736

2837
class App extends Component {
@@ -31,12 +40,7 @@ class App extends Component {
3140
}
3241

3342
async componentDidMount() {
34-
await persistCache({
35-
cache,
36-
storage: window.localStorage,
37-
maxSize: 4500000, // little less than 5 MB
38-
debug: true
39-
})
43+
await persistor.restore()
4044

4145
this.setState({
4246
loading: false

0 commit comments

Comments
 (0)