Skip to content

Commit be7ad3b

Browse files
GatsbyJS BotLB
GatsbyJS Bot
and
LB
authored
Fix(gatsby-plugin-image): Render LayoutWrapper prior to loading of lazy-hydrate (#28841) (#28990)
* add layoutwrapper to intial render when lazy-hydrate hasn't yet run * calculate sizer string (cherry picked from commit 78e5e47) Co-authored-by: LB <[email protected]>
1 parent c762050 commit be7ad3b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
import { PlaceholderProps } from "./placeholder"
1818
import { MainImageProps } from "./main-image"
1919
import { Layout } from "../image-utils"
20+
import { getSizer } from "./layout-wrapper"
2021

2122
// eslint-disable-next-line @typescript-eslint/interface-name-prefix
2223
export interface GatsbyImageProps
@@ -193,6 +194,8 @@ export const GatsbyImageHydrator: FunctionComponent<GatsbyImageProps> = function
193194
props,
194195
])
195196

197+
const sizer = getSizer(layout, width, height)
198+
196199
return (
197200
<Type
198201
{...wrapperProps}
@@ -203,7 +206,9 @@ export const GatsbyImageHydrator: FunctionComponent<GatsbyImageProps> = function
203206
}}
204207
className={`${wClass}${className ? ` ${className}` : ``}`}
205208
ref={root}
206-
dangerouslySetInnerHTML={{ __html: `` }}
209+
dangerouslySetInnerHTML={{
210+
__html: sizer,
211+
}}
207212
suppressHydrationWarning
208213
/>
209214
)

packages/gatsby-plugin-image/src/components/layout-wrapper.tsx

+17-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ if (hasNativeLazyLoadSupport) {
3939
/>
4040
)
4141

42+
export function getSizer(
43+
layout: Layout,
44+
width: number,
45+
height: number
46+
): string {
47+
let sizer: string | null = null
48+
if (layout === `fluid`) {
49+
sizer = `<div aria-hidden="true" style="padding-top: ${
50+
(height / width) * 100
51+
}%;"></div>`
52+
}
53+
if (layout === `constrained`) {
54+
sizer = `<div style="max-width: ${width}px; display: block;"><img alt="" role="presentation" aria-hidden="true" src="data:image/svg+xml;charset=utf-8,%3Csvg height='${height}' width='${width}' xmlns='http://www.w3.org/2000/svg' version='1.1'%3E%3C/svg%3E" style="max-width: 100%; display: block; position: static;"></div>`
55+
}
56+
return sizer
57+
}
58+
4259
export const LayoutWrapper: FunctionComponent<ILayoutWrapperProps> = function LayoutWrapper({
4360
layout,
4461
width,
@@ -68,7 +85,6 @@ export const LayoutWrapper: FunctionComponent<ILayoutWrapperProps> = function La
6885
</div>
6986
)
7087
}
71-
7288
return (
7389
<Fragment>
7490
{sizer}

0 commit comments

Comments
 (0)