Skip to content

Commit 12da978

Browse files
wardpeetLaurie
andauthored
fix(gatsby-plugin-image): do not show placeholder if image already lo… (#28868)
* fix(gatsby-plugin-image): do not show placeholder if image already loaded * remove console.log Co-authored-by: Laurie <[email protected]>
1 parent a8c2f39 commit 12da978

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

packages/gatsby-plugin-image/src/components/gatsby-image.browser.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,14 @@ export const GatsbyImageHydrator: FunctionComponent<GatsbyImageProps> = function
109109
if (hasSSRHtml.complete) {
110110
customOnLoad?.()
111111
storeImageloaded(JSON.stringify(images))
112-
}
113-
hasSSRHtml.addEventListener(`load`, function onLoad() {
114-
hasSSRHtml.removeEventListener(`load`, onLoad)
112+
} else {
113+
hasSSRHtml.addEventListener(`load`, function onLoad() {
114+
hasSSRHtml.removeEventListener(`load`, onLoad)
115115

116-
customOnLoad?.()
117-
storeImageloaded(JSON.stringify(images))
118-
})
116+
customOnLoad?.()
117+
storeImageloaded(JSON.stringify(images))
118+
})
119+
}
119120
return undefined
120121
}
121122

@@ -157,11 +158,6 @@ export const GatsbyImageHydrator: FunctionComponent<GatsbyImageProps> = function
157158
return
158159
}
159160

160-
// When no ssrHtml is found (develop) we should force render instead of hydrate
161-
if (!hasSSRHtml) {
162-
hydrated.current = true
163-
}
164-
165161
import(`./lazy-hydrate`).then(({ lazyHydrate }) => {
166162
lazyHydrator.current = lazyHydrate(
167163
{

packages/gatsby-plugin-image/src/components/lazy-hydrate.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ type LazyHydrateProps = Omit<GatsbyImageProps, "as" | "style" | "className"> & {
1919
ref: MutableRefObject<HTMLImageElement | undefined>
2020
}
2121

22+
const IS_DEV = process.env.NODE_ENV === `development`
23+
2224
export function lazyHydrate(
2325
{
2426
image,
@@ -97,7 +99,8 @@ export function lazyHydrate(
9799
</LayoutWrapper>
98100
)
99101

100-
const doRender = hydrated.current ? render : hydrate
102+
// Force render to mitigate "Expected server HTML to contain a matching" in develop
103+
const doRender = hydrated.current || IS_DEV ? render : hydrate
101104
doRender(component, root.current)
102105
hydrated.current = true
103106

0 commit comments

Comments
 (0)