Skip to content

Commit

Permalink
fixed image cache information store
Browse files Browse the repository at this point in the history
provide cache information in the onStartLoad handler
  • Loading branch information
Kalle Ott committed Sep 3, 2018
1 parent ce75b87 commit b7854d8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/gatsby-image/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface GatsbyImageProps {
position?: string
backgroundColor?: string | boolean
onLoad?: () => void
onStartLoad?: () => void
onStartLoad?: (param: { wasCached: boolean }) => void
Tag?: string
}

Expand Down
13 changes: 5 additions & 8 deletions packages/gatsby-image/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ const inImageCache = props => {
? convertedProps.fluid.src
: convertedProps.fixed.src

if (imageCache[src]) {
return true
} else {
imageCache[src] = true
return false
}
return imageCache[src] || false
}

let io
Expand Down Expand Up @@ -172,7 +167,7 @@ class Image extends React.Component {

componentDidMount() {
if (this.state.isVisible && typeof this.props.onStartLoad === `function`) {
this.props.onStartLoad()
this.props.onStartLoad({ wasCached: inImageCache(this.props) })
}
}

Expand All @@ -183,7 +178,7 @@ class Image extends React.Component {
!this.state.isVisible &&
typeof this.props.onStartLoad === `function`
) {
this.props.onStartLoad();
this.props.onStartLoad({ wasCached: inImageCache(this.props) })
}

this.setState({ isVisible: true, imgLoaded: false })
Expand Down Expand Up @@ -309,6 +304,7 @@ class Image extends React.Component {
sizes={image.sizes}
style={imageStyle}
onLoad={() => {
imageCache[image.src] = true
this.state.IOSupported && this.setState({ imgLoaded: true })
this.props.onLoad && this.props.onLoad()
}}
Expand Down Expand Up @@ -410,6 +406,7 @@ class Image extends React.Component {
style={imageStyle}
onLoad={() => {
this.setState({ imgLoaded: true })
imageCache[image.src] = true
this.props.onLoad && this.props.onLoad()
}}
onError={this.props.onError}
Expand Down

0 comments on commit b7854d8

Please sign in to comment.