Skip to content

Commit

Permalink
Add basic caching of preview thumbnails
Browse files Browse the repository at this point in the history
Fixes #927
  • Loading branch information
cookpete committed Jun 11, 2020
1 parent f90c756 commit dba324a
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React, { Component } from 'react'

const ICON_SIZE = '64px'

const cache = {}

export default class Preview extends Component {
mounted = false
state = {
Expand Down Expand Up @@ -29,13 +31,18 @@ export default class Preview extends Component {
this.setState({ image: light })
return
}
if (cache[url]) {
this.setState({ image: cache[url] })
return
}
this.setState({ image: null })
return window.fetch(`https://noembed.com/embed?url=${url}`)
.then(response => response.json())
.then(data => {
if (data.thumbnail_url && this.mounted) {
const image = data.thumbnail_url.replace('height=100', 'height=480')
this.setState({ image })
cache[url] = image
}
})
}
Expand Down

0 comments on commit dba324a

Please sign in to comment.