-
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
fix(gatsby-image): Fix fluid not respecting maxWidth/maxHeight #17006
Conversation
I'm not 100% sure what to do here. We have presentationWidth & presentationHeight which we can already use for this. Another thing is I'm unsure if baking this in gatsby-image is the right decision. Problems with this solution is that we need to update our fragments to make it universal to always include presentationWidth & presentationHeight inside fluid fragments, which is probably not a big deal. const Image = () => {
const data = useStaticQuery(graphql`
query {
placeholderImage: file(relativePath: { eq: "gatsby-astronaut.png" }) {
childImageSharp {
fluid(maxHeight: 150) {
presentationWidth,
presentationHeight,
...GatsbyImageSharpFluid
}
}
}
}
`)
return <Img fluid={data.placeholderImage.childImageSharp.fluid} style={{ width: data.placeholderImage.childImageSharp.fluid.presentationWidth, height: data.placeholderImage.childImageSharp.fluid.presentationHeight }} />
} |
@wardpeet you are right, I didn't even notice those were getting the same values. I can drop the 3 commits dealing with adding maxHeight/maxWidth (4ac71dd, a838a30, c34d5b4) and work from there using I did some quick testing, including |
c34d5b4
to
09f20cb
Compare
@wardpeet, I have pushed the changes we talked about. I think I updated the fragments correctly, but I had a heck of a time getting my test project to see / take those updates, so maybe there is somewhere else I'm not aware of that will need to be updated as well? I was getting some strange errors (didn't seem to be associated with my changes), but they seemed to go away after pulling and rebasing off master again, I think something was broken upstream for awhile, FYI in case they show back up. |
thanks @wardpeet 👍 |
This changes default behaviour and users will get different results for same code (breaking change) - some people rely on current behaviour. I think it's also easier to to set As a note - please check #10460 for PR that was pretty similar to this one. |
b5183ce
to
717b923
Compare
Thought I would give this one last go 😀 sorry @pieh This still might "break" people who are relying on the current behavior... Thought here is adding maxWidth and maxHeight to the node, to use in I see now that you say it, Testing this seems to function pretty well, but I understand it could throw everyone using this for a loop. We can just close it out and go with the documentation with the easy fix 👍 no big. |
e39d3ab
to
2738953
Compare
one thing I can't seem to get to work, is getting the update fragments in my project. so when testing I have to include even if all that happens out of this PR is I learn how to update fragments, I consider that a win :) |
I have created a gatsby test site for testing this PR. https://github.com/sbardian/my-default-starter. directions in README for anyone who needs them. |
Hey, don't worry about me ;) I know this is confusing and problematic (hence attempt to address it with this PR), only thing I wanted to convey is that we have to be careful about breaking changes. I don't have proper answer on how to best solve this. Adding documentation is "safest way out" (in that we avoid making breaking changes, and that's why we opted into this way in that previous PR), but this will still bite people that don't expect our current behaviour and it's not unreasonable to think that meaningful portion of users might never find our documentation for this problem |
Will wait and see what wardpeet or anyone else that might check it out thinks. Give it a test drive and let me know. Thanks! |
@pieh @wardpeet I was looking through the documentation on the manual "fix" for this. I don't think this will be a breaking change. I think people using the documented fix would just end up overriding the new maxHeight and/or maxWidth values I'm setting on the image, when they pass their |
@pieh @wardpeet looking at the documented fix, it will not always actually work.
you will get the following results (using my branch) or the same minus maxHeight/maxWidth using master branch:
So with the fix you would be setting the gatsby-image styles to maxHeight/maxWidth: 800, not the 2000 you requested in your query. |
caac636
to
c744698
Compare
@gillkyle have you had a chance to review? Thoughts? Anyone, else I should reach out to? |
c744698
to
59aaf86
Compare
Hey @sbardian, all your work on this looks awesome and your research into the problem is really helpful! I probably don't have as much context as @pieh and @wardpeet (who can probably make the final say better), but I do like the idea of documenting what you've found tricky with this better in the API doc page. I actually wasn't aware of the |
Hey @sbardian sorry for taking so long but could you make an example site which has a lot of different image resolutions and see if this actually fixes things for all cases. It's going to be easier to get this through. Thanks for your patience and sorry for taking so long. |
@wardpeet no problem. I think I fixed up the issue @smashercosmo caught. |
Is this coming any time soon? |
This looks great, I'm running some tests and hopefully, I can merge this today. |
@wardpeet you might still be working on this, if so ignore me. Pulled down the update and it seems to break the fix. Though I'm not able to run |
Hey @sbardian, what do you mean it doesn't work anymore? It seems to work fine for me but maybe I'm testing it the wrong way 😬 export const query = graphql`
query {
allImageSharp {
nodes {
wh: fluid(maxWidth: 800) {
...GatsbyImageSharpFluid_withWebp
...GatsbyImageSharpFluidLimitPresentationSize
}
hw: fluid(maxHeight: 500) {
...GatsbyImageSharpFluid_withWebp
...GatsbyImageSharpFluidLimitPresentationSize
}
}
}
} |
@wardpeet with your update to the fragments I was also not getting maxWidth/maxHeight coming across to my project. But I think that is only because I'm not sure how to get gatsby to update the fragments, when I add them specifically I get the values.
|
Hey sorry, i changed the PR a bit so we don't change behavior in gatsby-image v2. In v3 we should do stuff differently like we do now. I've added a new fragment called You'll have to do the following:
What do you think of this change? |
@wardpeet I see. Looks good. Trying to test it but it thinks the fragment doesn't exist. I'm not sure how to get gatsby to "refresh" the fragments. |
@@ -119,7 +119,7 @@ export const GatsbyImageSharpFluid = graphql` | |||
* Presentation sizes to make sure a fluid container does not overflow | |||
* @type {Fragment} | |||
*/ | |||
export const GatsbyImageSharpFluidLimitPresentationSize = ` | |||
export const GatsbyImageSharpFluidLimitPresentationSize = graphql` | |||
fragment GatsbyImageSharpFluidLimitPresentationSize on ImageSharpFluid { | |||
maxHeight: presentationHeight |
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.
@wardpeet if you are setting the maxHeight/maxWidth to the presentationHeight/presentationWidth I'm not sure this will end up being correct, see this comment of mine on the issue of using presentationHeight/Width as maxHeight/Width. comment. Basically the presentationHeight/Width max out at the images dimensions, so if your image is 800x800 and you set maxWidth to 1500, it will still only be maxWidth 800.
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.
and this comment also.
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 was finally able to get the fragments to update and working using:
childImageSharp {
fluid(maxWidth: 500) {
...GatsbyImageSharpFluid
...GatsbyImageSharpFluidLimitPresentationSize
}
}
and it does appear to be using presentationHeight/Width. So any maxHeight/width will max out at the image dimensions. This is why I had the logic in gatsby-plugin-sharp
let maxWidth
let maxHeight
if (options.maxHeight || options.maxWidth) {
maxWidth = options.maxWidth
? `${options.maxWidth}px`
: `${options.aspectRatio * options.maxHeight}px`
maxHeight = options.maxHeight
? `${options.maxHeight}px`
: `${options.aspectRatio * options.maxWidth}px`
}
and specifically returned maxHeight/maxWidth
it as part of the fluid return object.
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.
Setting it to presentationWidth/height feels more correct as your the container shouldn't be bigger than image. If you need that, you'll need a bigger image, else you'll end up with quality drop.
If you want to center the image or override the behavior you can still add a wrapper or set the maxWidth/maxHeight yourself on gatsby-image.
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.
If this makes more sense I'm good with it.
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 approve this message! Thanks @sbardian for the hard work on this and leading the way to get this merged!
Thanks @wardpeet ! |
@wardpeet sorry to be a downer. . . but it appears using
Instead of getting a skinny tall image cropped to your specifics, you end up with a 200x200 image. One way to fix this is to remove the documentation comment in the example here so no one expects a cropping behavior I guess? Thoughts? Correction: It may only be affecting one aspect of cropping. See the example "maxWidth: 200, maxHeight: 600. Crop Width" here |
The PR was published in:
The cropping problem might need to be looked at :( |
I will :) |
@wardpeet I think I have a fix for this by replacing
Also update
Basically remove first if condition, no point setting maxWidth if the user does not pass one, we will use the image dimensions. We get cropping back. The only issue is the image will grow to match a maxWidth or maxHeight query value if given. BUT it will not grow beyond the dimensions of the image itself. So if the image was 200x500 and they had maxHeight: 2000, it will max out at the 500 image height.
Anyway, not sure if you wanted a new PR to help fix this issue or not. Let me know if so. I have also deployed a new demo site here This demo site has updated images and the query descriptions towards the bottom. You can see the queries and demo code here |
@sbardian thanks for your thorough comment! It seems like calculating presentationWidth & height is a mistake, we should get it from the image itself. I've created #23905 to deal with that.
Sadly changing this default will be a breaking change, I do think in a next version we should make maxWidth or maxHeight mandatory. |
Description
An attempt at fixing gatsby-image fluid not respecting maxHeight #15167 issue. Instead of using the sizes string to pull out max-width/height, I decided to try to add to maxHeight/maxWidth fields to the node and use those in
gatsby-image
on thegatsby-image-wrapper
div. This might be way off base, and may presents other problems I'm not seeing at this point, but thought I would give it a try. This change touches many different pieces of code, and I am in no way confident this is a great/easiest/best way to fix this. :) My manual testing seems to fix the issue though.Related Issues
closes #15167