Skip to content

Commit

Permalink
fix(gatsby-plugin-image): flickering when state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
wardpeet committed Oct 29, 2021
1 parent 42c4e10 commit 3b8f538
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,12 @@ class GatsbyImageHydrator extends Component<
}

return import(`./lazy-hydrate`).then(({ lazyHydrate }) => {
const cacheKey = JSON.stringify(this.props.image.images)
this.lazyHydrator = lazyHydrate(
{
image: props.image.images,
isLoading: state.isLoading,
isLoaded: state.isLoaded,
isLoading: state.isLoading || hasImageLoaded(cacheKey),
isLoaded: state.isLoaded || hasImageLoaded(cacheKey),
toggleIsLoaded: () => {
props.onLoad?.()

Expand Down Expand Up @@ -138,6 +139,7 @@ class GatsbyImageHydrator extends Component<

shouldComponentUpdate(nextProps, nextState): boolean {
let hasChanged = false
console.log(this.props, nextProps)
if (!this.state.isLoading && nextState.isLoading && !nextState.isLoaded) {
// Props have changed between SSR and hydration, so we need to force render instead of hydrate
this.forceRender.current = true
Expand Down Expand Up @@ -169,13 +171,15 @@ class GatsbyImageHydrator extends Component<
}

if (this.root.current && !hasChanged) {
console.log({ hasChanged })
this._lazyHydrate(nextProps, nextState)
}

return false
}

componentDidMount(): void {
console.log(`mount`)
if (this.root.current) {
const ssrElement = this.root.current.querySelector(
`[data-gatsby-image-ssr]`
Expand Down Expand Up @@ -291,6 +295,7 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> =
classSafe,
backgroundColor,
])
console.log({ propsKey })
return <GatsbyImageHydrator key={propsKey} {...props} />
}

Expand Down

0 comments on commit 3b8f538

Please sign in to comment.