-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby-image): don't fadein image when already loaded "browser-cache" #12468
feat(gatsby-image): don't fadein image when already loaded "browser-cache" #12468
Conversation
noscript, removed transition and transition-delay, dropped width/height props in "fixed" image sectionnoscript version migrated sometime ago via this PR(states due to errors but no citation of what those were, edit: here it is and direct link to issue/workaround comment, As of late 2018 it is fixed, but only for React 16.5+, if you use 16.4, you don't get the error but lose lazy loading.. ). So Previously it used the The code for "Fixed" images , also passes in width/height, this is taken from the CSS Transition on placeholder unintended?The CSS transition duration for opacity was removed from the default Transition delay also seems to have varied. While it's |
Well this failure is confusing. I had run the tests locally and updated them noting in the relevant commit that it seemed odd the error hadn't been raised before this PR, and now the CI is unhappy with the snapshot(while local is fine with the exact same code).
// `display: inline`, oddly doesn't show up in the snapshot either?
const setup = (fluid = false, onLoad = () => {}, onError = () => {}) => {
const { container } = render(
<Img
backgroundColor
className={`fixedImage`}
style={{ display: `inline` }}
title={`Title for the image`}
alt={`Alt text for the image`}
{...fluid && { fluid: fluidShapeMock }}
{...!fluid && { fixed: fixedShapeMock }}
onLoad={onLoad}
onError={onError}
itemProp={`item-prop-for-the-image`}
placeholderStyle={{ color: `red` }}
placeholderClassName={`placeholder`}
/>
)
return container
}
// Img component has the styles that are now in the snapshot and CI should recognize the last 3 lines of style
const Img = React.forwardRef((props, ref) => {
const { sizes, srcSet, src, style, onLoad, onError, ...otherProps } = props
return (
<img
sizes={sizes}
srcSet={srcSet}
src={src}
{...otherProps}
onLoad={onLoad}
onError={onError}
ref={ref}
style={{
position: `absolute`,
top: 0,
left: 0,
width: `100%`,
height: `100%`,
objectFit: `cover`,
objectPosition: `center`,
...style,
}}
/>
)
}) My snapshot was updated by running |
4d616b2
to
0b9cd16
Compare
EDIT: Done.
EDIT: Fixed once #12719 merges, otherwise tests can be reverted. |
0ddd4c8
to
3d9a3cf
Compare
## Description Certain CSS styles have been failing to appear in Jest tests due to an outdated JSDom(and some it's dependencies, one being updated with bugfix in Feb 2019). This was causing false positives for `gatsby-image` tests for a while. Nothing major, just some CSS props weren't appearing in tests when they technically should have been. ## Details [My PR](#12468) is failing in the CI but was passing tests locally. I had been running tests on the `gatsby-image` package on it's own, which uses `react-testing-library: ^5.0.0`, and it failed against the snapshot test. Updated snapshots failed when my PR ran on CircleCI. The main repo root yarn.lock file has `react-testing-library: ^5.2.1` locked down(retrieves 5.2.1 instead of 5.9.0, although is a v6 release now). Correcting this didn't help for passing the test suite. [`cssstyle: 1.2.0`](jsdom/cssstyle#74) fixes the [problems with certain styles](jsdom/jsdom#1965 (comment)) being [ignored](testing-library/react-testing-library#214. `transitionDelay`, and certain color values for `backgroundColor`, "red" works, but not "lightgray"). That however [needs `jsdom: ^12.0.0`](jsdom/jsdom@ed11465#diff-b9cfc7f2cdf78a7f4b91a753d10865a2), but [Jest refuses to update as support for Node <8 is dropped](jestjs/jest#8016 (comment)).. I've added a workaround until Jest supports a newer major version of JSDom. Using [`jest-environment-jsdom-fourteen`](https://github.com/ianschmitz/jest-environment-jsdom-fourteen) is the [advised approach](jestjs/jest#7122 (comment)) for the meantime, it'll only impact users running Jest test suite on Node 6, that should be fine for Gatsby? (Node 6 CI test seems to pass) Only `gatsby-image` tests needed updates. [Node 6 will be EoL in May](https://github.com/nodejs/Release#release-schedule), no idea when Jest will bump JSDom from then, but we could wait a few months if you rather avoid the workaround? ## Related Issues #12468 (Spotted it while working on this)
Once isVisible is true, imageRef becomes accessible. imgLoaded is moved into the 2nd setState call to avoid initiating unnecessary animation frames.
Missed including style change for the bgcolor placeholder in fixed image section.
Width and Height for fixed image are overwritten by the spread syntax of image(which is where they source the value anyway). transition and transition-delay CSS properties have no use, JS isn't going to change these, nor are they ever passed into this method in the first place(it's from a prior version of Img element, which was used for noscript support before this string approach.
My changes should not have introduced many of the changes in the new snapshot, although the changes appear to be what should have been here (why weren't they?)
Moved `imageStyle` above placeholder styles. Keeps the "should" vars co-located with `imageStyle` while `imagePlaceholderStyle` can use `shouldFadeIn` too and be relocated next to `placeholderImageProps`.
For clarity of actual element imported(as there is both `Img` and `Image` elements in `gatsby-image`
Establishes a clear link between the two for the fadeIn transition.
3d9a3cf
to
83bafb2
Compare
@wardpeet Would you be able to review/merge this please? TL;DR version is: Addresses both linked issues:
Three main changes(each commit provides more details):
|
looking at this right now! thanks for you patience and great comments! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great! Such a great improvement. I love this and so will our users.
Just a few questions but not blocking!
packages/gatsby-image/src/index.js
Outdated
@@ -250,24 +258,30 @@ class Image extends React.Component { | |||
itemProp, | |||
} = convertProps(this.props) | |||
|
|||
const shouldDisplay = this.state.imgLoaded || this.state.fadeIn === false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure but maybe shouldLazyLoad is a better name? not sure if lazyload is the right term here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the condition for that boolean gives the context/clarity what it is for, it was just more appropriate to summarize it as a should
variable where it was used so the code for the style was still easy to read when the logic would otherwise get a bit long/messy.
If shouldDisplay
doesn't feel right, how about shouldReveal
? The variable is describing when the image is ready to display by toggling the opacity, a shouldLazyLoad
seems to describe the wrong relationship, you could try isLazyLoaded
or isReady
(since ultimately it's a true value you're after, there isn't really a distinction to lazy-loading or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 for shouldReveal! let me change that and merge afterwards!
No longer relies on using git-blame to reference the related commit for more details.
The Windows unit test is failing unrelated to the contents of this PR:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again! this is awesome!
Published in [email protected] |
Description
Placeholder transition CSS continues to work as normal, only when the image exists in the browser cache are the transition styles removed. This is mainly to address/reduce the undesirable transition for transparent images as #12254 describes.
Additionally:
0.25s
and0.35s
respectively have been replaced with the0.5s
duration of the fadein transition.See my comment in the referenced issue for some considerations/variations or further reasoning for the changes made.
imgLoaded
andimgCached
could be renamed toisImgLoaded
isImgCached
?delayHide
maybe should bedelayHideStyle
?Related Issues
Fixes: #12254, Fixes #10937