-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compatibility with Gatsby 3 & workaround (☂️ Umbrella Issue) #141
Comments
Hi Yannick, wished I were a team, but I'm flying solo on Best, Tim. |
Hi @YannickMol, just looked into it & have to say: Uffz. Quite another paradigm of handling images oO. Best, Tim. |
Hi @YannickMol, again. As written above, I just worked on a converter function. It isn't completely finished & I'm gonna try adding it to import React from 'react'
import { graphql, useStaticQuery } from 'gatsby'
import { GatsbyImage, getImage } from "gatsby-plugin-image"
import BackgroundImage from "gatsby-background-image"
// ------------------ COPY FROM HERE -------------------------------------------
// Helper functions.
const getBgImageType = imageData => imageData.layout === 'fixed' ? 'fixed' : 'fluid'
const getAspectRatio = imageData => imageData.width / imageData.height
const getPlaceholder = imageData => {
if (imageData.placeholder) {
return imageData.placeholder.fallback.includes(`base64`) ?
{ base64: imageData.placeholder.fallback }
: { tracedSvg: imageData.placeholder.fallback }
}
return {}
}
/**
* Tries to Backport the new `gatsbyImageData` type to the classic `fluid` / `fixed` form.
*
* @param imageData {object} The image data to convert.
* @returns {{}}
*/
const convertToBgImage = imageData => {
if (imageData && imageData.layout) {
const returnBgObject = {}
const bgType = getBgImageType(imageData)
const aspectRatio = getAspectRatio(imageData)
const placeholder = getPlaceholder(imageData)
returnBgObject[bgType] = {
...imageData.images.fallback,
...placeholder,
aspectRatio,
}
return returnBgObject
}
return {}
}
// ------------------ TO HERE --------------------------------------------------
const GpiTest = () => {
const {placeholderImage, oldImage} = useStaticQuery(
graphql`
query {
placeholderImage: file(relativePath: { eq: "gatsby-astronaut.png" }) {
childImageSharp {
gatsbyImageData(
width: 200
placeholder: BLURRED
formats: [AUTO, WEBP, AVIF]
)
}
}
}
`
)
const image = getImage(placeholderImage)
// Use like this:
const bgImage = convertToBgImage(image)
return (
<BackgroundImage
Tag="section"
// Spread bgImage into BackgroundImage:
{...bgImage}
preserveStackingContext
>
<div style={{minHeight: 1000, minWidth: 1000}}>
<GatsbyImage image={image} alt={"testimage"}/>
</div>
</BackgroundImage>
)
}
export default GpiTest
WebP (& AVIF) handling is currently missing (& for the latter I have to adapt Best, Tim. P.S.: With this function you could use the new type with the "old" |
Hi there! |
I have created a simple wrapper that can be used as a 1:1 replacement for // Helper functions.
import BackgroundImage, { IBackgroundImageProps, IFixedObject, IFluidObject } from 'gatsby-background-image'
import { getImage } from 'gatsby-plugin-image'
import { IGatsbyImageData } from 'gatsby-plugin-image/dist/src/components/gatsby-image.browser'
import React, { CSSProperties } from 'react'
import { FunctionComponent } from 'react'
const getBgImageType = (imageData: IGatsbyImageData) => (imageData.layout === 'fixed' ? 'fixed' : 'fluid')
const getAspectRatio = (imageData: IGatsbyImageData) => imageData.width / imageData.height
const getPlaceholder = (imageData: IGatsbyImageData) => {
if (imageData.placeholder) {
return imageData.placeholder!.fallback?.includes(`base64`)
? { base64: imageData.placeholder!.fallback }
: { tracedSVG: imageData.placeholder!.fallback }
}
return {}
}
/**
* Tries to Backport the new `gatsbyImageData` type to the classic `fluid` / `fixed` form.
*
* @param imageData {object} The image data to convert.
* @returns {{}}
*/
export function convertToBgImage(imageData: IGatsbyImageData): Partial<IBackgroundImageProps> {
if (imageData && imageData.layout) {
const returnBgObject: Partial<IBackgroundImageProps> = {}
const bgType = getBgImageType(imageData)
const aspectRatio = getAspectRatio(imageData)
const placeholder = getPlaceholder(imageData)
// @ts-ignore
returnBgObject[bgType] = {
...imageData.images.fallback,
...placeholder,
aspectRatio,
}
return returnBgObject
}
return {}
}
export interface IBgImageProps {
fluid?: IGatsbyImageData
className?: string
onClick?: (e: Event) => void
tabIndex?: number
fadeIn?: boolean
id?: string
style?: CSSProperties
role?: string
preserveStackingContext?: boolean
}
/**
* This is a temporary stopgap solution until `<BackgroundImage>` natively supports `gatsby-plugin-image`,
* see [https://github.com/timhagn/gatsby-background-image/issues/141](https://github.com/timhagn/gatsby-background-image/issues/141).
* @param {React.PropsWithChildren<IBgImageProps>} props
* @return {JSX.Element}
* @constructor
*/
export const BgImage: FunctionComponent<IBgImageProps> = (props) => {
const { fluid, children, ...args } = props
if (fluid) {
const image = getImage(fluid)
const bgImage = image && convertToBgImage(image)
return (
<BackgroundImage {...bgImage} {...args}>
{children}
</BackgroundImage>
)
} else {
return <div>{children}</div>
}
} Depending on which other features you need, you might need to add them to |
Currently, this workaround is very basic. I'm planning to extend this to support @timhagn @rburgst, I'm trying to also support |
Hi again @ALL, if you head over to the gbimage-bridge branch & package, you may see the proceedings of the new adapter and may of course add your knowledge and code : )! Best, Tim. |
And once more with feeling: Hi @ALL! Just published the first version of
Have a look at gbimage-bridge, grab Best, Tim. P.S.: The resulting image from the conversion could even be used with classic |
@timhagn It's a bit weird you decided to publish it as a separated package. I thought there will be a new major release of the original one. |
Hi @matthewkwong2, it was way faster to write the bridge & gain more experience with the new format than going through all the code and every important moving part to rip our Next step (apart from trying to fix more issues % ) is a rewrite, as the current code has become quite a behemoth through "organic growth" that everything should be revisited. Especially looking at the LCP-Score & suchlike ; ). Best, Tim. |
Hi @timhagn, thanks a lot, it works, but there is one major issue, the images all seem to load twice. Even in your demo repository you can see it in the network: https://github.com/timhagn/g3bitest Any ideas? |
I notice the same thing. While this issue also happens on |
hmmz Would have to invest a little more, but I guess this has to do with #125.
we might perhaps give them a shout, or what do you think? |
Already done it in gatsbyjs/gatsby#30272 |
@timhagn I know you are the only person working on this, but do you have any ETA when this will be available without the |
This issue seems to be fixed with the latest release of |
Hi Team,
As the new version of gatsby-image is coming up in the form of the gatsby-plugin-image plugin, I was wondering whether this will be reflected in a new version of the gatsby-background-image plugin. When using the new API and gatsbyImageData in a BackgroundImage component, it gives the following error:
Warning: Failed prop type: Invalid prop "fluid" supplied to "BackgroundImage".
.Thanks,
Yannick
The text was updated successfully, but these errors were encountered: