diff --git a/packages/gatsby-plugin-image/src/components/gatsby-image.browser.tsx b/packages/gatsby-plugin-image/src/components/gatsby-image.browser.tsx index f3b0ed01343ea..30d09b237d74d 100644 --- a/packages/gatsby-plugin-image/src/components/gatsby-image.browser.tsx +++ b/packages/gatsby-plugin-image/src/components/gatsby-image.browser.tsx @@ -17,6 +17,7 @@ import { import { PlaceholderProps } from "./placeholder" import { MainImageProps } from "./main-image" import { Layout } from "../image-utils" +import { getSizer } from "./layout-wrapper" // eslint-disable-next-line @typescript-eslint/interface-name-prefix export interface GatsbyImageProps @@ -193,6 +194,8 @@ export const GatsbyImageHydrator: FunctionComponent = function props, ]) + const sizer = getSizer(layout, width, height) + return ( = function }} className={`${wClass}${className ? ` ${className}` : ``}`} ref={root} - dangerouslySetInnerHTML={{ __html: `` }} + dangerouslySetInnerHTML={{ + __html: sizer, + }} suppressHydrationWarning /> ) diff --git a/packages/gatsby-plugin-image/src/components/layout-wrapper.tsx b/packages/gatsby-plugin-image/src/components/layout-wrapper.tsx index c4e9a728b55f9..2e66946a59430 100644 --- a/packages/gatsby-plugin-image/src/components/layout-wrapper.tsx +++ b/packages/gatsby-plugin-image/src/components/layout-wrapper.tsx @@ -39,6 +39,23 @@ if (hasNativeLazyLoadSupport) { /> ) +export function getSizer( + layout: Layout, + width: number, + height: number +): string { + let sizer: string | null = null + if (layout === `fluid`) { + sizer = `` + } + if (layout === `constrained`) { + sizer = `
` + } + return sizer +} + export const LayoutWrapper: FunctionComponent = function LayoutWrapper({ layout, width, @@ -68,7 +85,6 @@ export const LayoutWrapper: FunctionComponent = function La ) } - return ( {sizer}