Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

yashim/fix: LCP for eager loading Images #2472

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions src/components/elements/query-image.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import styled from 'styled-components'
import styled, { css } from 'styled-components'
import { GatsbyImage, getImage, ImageDataLike } from 'gatsby-plugin-image'

type QueryImageProps = {
Expand All @@ -15,28 +15,39 @@ type ImageWrapperProps = {
width: string
height: string
className?: string
loading: 'eager' | 'lazy'
}

export const ImageWrapper = styled.div<ImageWrapperProps>`
& .gatsby-image-wrapper {
width: ${(props) => props.width || '100%'};
height: ${(props) => props.height};
}
.gatsby-image-wrapper [data-main-image] {
${(props) => {
if (props.loading === 'eager') {
return css`
transition: none;
opacity: 1;
`
}
}}
}
`

const QueryImage = ({
alt,
className,
data,
height,
loading,
loading = 'lazy',
width,
...props
}: QueryImageProps) => {
const image = getImage(data)
if (data) {
return (
<ImageWrapper width={width} height={height} className={className}>
<ImageWrapper loading={loading} width={width} height={height} className={className}>
<GatsbyImage image={image} alt={alt} loading={loading} {...props} />
</ImageWrapper>
)
Expand Down