Cache articles in memory for speed
This commit is contained in:
parent
6764bf0d6d
commit
187c6b8110
|
@ -1,5 +1,6 @@
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom';
|
import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom';
|
||||||
|
import localForage from 'localforage';
|
||||||
import './Style-light.css';
|
import './Style-light.css';
|
||||||
import './Style-dark.css';
|
import './Style-dark.css';
|
||||||
import './fonts/Fonts.css';
|
import './fonts/Fonts.css';
|
||||||
|
@ -17,6 +18,12 @@ class App extends React.Component {
|
||||||
this.state = {
|
this.state = {
|
||||||
theme: localStorage.getItem('theme') || '',
|
theme: localStorage.getItem('theme') || '',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.cache = {};
|
||||||
|
}
|
||||||
|
|
||||||
|
updateCache = (key, value) => {
|
||||||
|
this.cache[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
light() {
|
light() {
|
||||||
|
@ -29,6 +36,15 @@ class App extends React.Component {
|
||||||
localStorage.setItem('theme', 'dark');
|
localStorage.setItem('theme', 'dark');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
if (!this.cache.length) {
|
||||||
|
localForage.iterate((value, key) => {
|
||||||
|
this.updateCache(key, value);
|
||||||
|
});
|
||||||
|
console.log('loaded cache from localforage');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const theme = this.state.theme;
|
const theme = this.state.theme;
|
||||||
document.body.style.backgroundColor = theme === 'dark' ? '#000' : '#eeeeee';
|
document.body.style.backgroundColor = theme === 'dark' ? '#000' : '#eeeeee';
|
||||||
|
@ -46,12 +62,12 @@ class App extends React.Component {
|
||||||
<Route path='/(|search)' component={Search} />
|
<Route path='/(|search)' component={Search} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Route path='/' exact component={Feed} />
|
<Route path='/' exact render={(props) => <Feed {...props} updateCache={this.updateCache} />} />
|
||||||
<Switch>
|
<Switch>
|
||||||
<Route path='/search' component={Results} />
|
<Route path='/search' component={Results} />
|
||||||
<Route path='/:id' exact component={Article} />
|
<Route path='/:id' exact render={(props) => <Article {...props} cache={this.cache} />} />
|
||||||
</Switch>
|
</Switch>
|
||||||
<Route path='/:id/c' exact component={Comments} />
|
<Route path='/:id/c' exact render={(props) => <Comments {...props} cache={this.cache} />} />
|
||||||
|
|
||||||
<ScrollToTop />
|
<ScrollToTop />
|
||||||
</Router>
|
</Router>
|
||||||
|
|
|
@ -8,8 +8,13 @@ class Article extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
|
const id = this.props.match.params.id;
|
||||||
|
const cache = this.props.cache;
|
||||||
|
|
||||||
|
if (id in cache) console.log('cache hit');
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
story: false,
|
story: cache[id] || false,
|
||||||
error: false,
|
error: false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,12 +10,17 @@ class Article extends React.Component {
|
||||||
constructor(props) {
|
constructor(props) {
|
||||||
super(props);
|
super(props);
|
||||||
|
|
||||||
|
const id = this.props.match.params.id;
|
||||||
|
const cache = this.props.cache;
|
||||||
|
|
||||||
|
if (id in cache) console.log('cache hit');
|
||||||
|
|
||||||
this.state = {
|
this.state = {
|
||||||
story: false,
|
story: cache[id] || false,
|
||||||
error: false,
|
error: false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
componentDidMount() {
|
componentDidMount() {
|
||||||
const id = this.props.match.params.id;
|
const id = this.props.match.params.id;
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ class Feed extends React.Component {
|
||||||
.then(result => {
|
.then(result => {
|
||||||
localForage.setItem(x.id, result.story)
|
localForage.setItem(x.id, result.story)
|
||||||
.then(console.log('preloaded', x.id, x.title));
|
.then(console.log('preloaded', x.id, x.title));
|
||||||
|
this.props.updateCache(x.id, result.story);
|
||||||
}, error => {}
|
}, error => {}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user