Skip to content

Commit

Permalink
fix(gatsby-plugin-image): do not show placeholder if image already lo… (
Browse files Browse the repository at this point in the history
#28868)

* fix(gatsby-plugin-image): do not show placeholder if image already loaded

* remove console.log

Co-authored-by: Laurie <[email protected]>
  • Loading branch information
wardpeet and Laurie authored Jan 12, 2021
1 parent a8c2f39 commit 12da978
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ export const GatsbyImageHydrator: FunctionComponent<GatsbyImageProps> = function
if (hasSSRHtml.complete) {
customOnLoad?.()
storeImageloaded(JSON.stringify(images))
}
hasSSRHtml.addEventListener(`load`, function onLoad() {
hasSSRHtml.removeEventListener(`load`, onLoad)
} else {
hasSSRHtml.addEventListener(`load`, function onLoad() {
hasSSRHtml.removeEventListener(`load`, onLoad)

customOnLoad?.()
storeImageloaded(JSON.stringify(images))
})
customOnLoad?.()
storeImageloaded(JSON.stringify(images))
})
}
return undefined
}

Expand Down Expand Up @@ -157,11 +158,6 @@ export const GatsbyImageHydrator: FunctionComponent<GatsbyImageProps> = function
return
}

// When no ssrHtml is found (develop) we should force render instead of hydrate
if (!hasSSRHtml) {
hydrated.current = true
}

import(`./lazy-hydrate`).then(({ lazyHydrate }) => {
lazyHydrator.current = lazyHydrate(
{
Expand Down
5 changes: 4 additions & 1 deletion packages/gatsby-plugin-image/src/components/lazy-hydrate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type LazyHydrateProps = Omit<GatsbyImageProps, "as" | "style" | "className"> & {
ref: MutableRefObject<HTMLImageElement | undefined>
}

const IS_DEV = process.env.NODE_ENV === `development`

export function lazyHydrate(
{
image,
Expand Down Expand Up @@ -97,7 +99,8 @@ export function lazyHydrate(
</LayoutWrapper>
)

const doRender = hydrated.current ? render : hydrate
// Force render to mitigate "Expected server HTML to contain a matching" in develop
const doRender = hydrated.current || IS_DEV ? render : hydrate
doRender(component, root.current)
hydrated.current = true

Expand Down

0 comments on commit 12da978

Please sign in to comment.