Skip to content
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

[gatsby-image] Fixed image with media query are not resized #16763

Closed
squelix opened this issue Aug 20, 2019 · 31 comments · Fixed by #26097
Closed

[gatsby-image] Fixed image with media query are not resized #16763

squelix opened this issue Aug 20, 2019 · 31 comments · Fixed by #26097
Assignees
Labels
topic: media Related to gatsby-plugin-image, or general image/media processing topics

Comments

@squelix
Copy link

squelix commented Aug 20, 2019

Description

I defined two fixed Image:

  • for desktop with a media query
  • for mobile without media query

When my screen resized it downloads the smallest image but it doesn't resize the gatsby-image-wrapper :/

Steps to reproduce

I define the Graphql queries like this:

export const query = graphql`
  query {
    imageDesktop: file(relativePath: { eq: "home/partners/image.png" }) {
      childImageSharp {
        fixed(width: 161) {
          ...GatsbyImageSharpFixed
        }
      }
    }
    imageMobile: file(relativePath: { eq: "home/partners/image.png" }) {
      childImageSharp {
        fixed(width: 111) {
          ...GatsbyImageSharpFixed
        }
      }
    }
  }
`;

Then I define the sources like this:

const sources = [
  data.imageMobile.childImageSharp.fixed,
  {
    ...data.imageDesktop.childImageSharp.fixed,
    media: `(min-width: 768px)`,
  },
]

And I use the sources:

<Img
  alt="Alt for image"
  fixed={sources}
></Img>

Expected result

What should happen?

Actual result

The size of the Image is 161px even after the breakpoint of 768px, but id downloads the 111px Image.

Environment

  System:
    OS: macOS 10.14.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 10.15.2 - /usr/local/opt/node@10/bin/node
    Yarn: 1.17.3 - /usr/local/Cellar/yvm/3.6.1/shim/yarn
    npm: 6.4.1 - /usr/local/opt/node@10/bin/npm
  Languages:
    Python: 2.7.15 - /usr/local/bin/python
  Browsers:
    Chrome: 76.0.3809.100
    Firefox: 67.0.4
    Safari: 12.1.2
  npmPackages:
    gatsby: ^2.5.5 => 2.13.59
    gatsby-image: ^2.1.0 => 2.2.8
    gatsby-plugin-env-variables: ^1.0.1 => 1.0.1
    gatsby-plugin-manifest: ^2.1.1 => 2.2.5
    gatsby-plugin-nprogress: ^2.0.10 => 2.1.2
    gatsby-plugin-offline: ^2.1.1 => 2.2.5
    gatsby-plugin-postcss: ^2.0.7 => 2.1.2
    gatsby-plugin-react-helmet: ^3.0.12 => 3.1.3
    gatsby-plugin-react-svg: ^2.1.1 => 2.1.2
    gatsby-plugin-robots-txt: ^1.4.0 => 1.5.0
    gatsby-plugin-sharp: ^2.1.3 => 2.2.11
    gatsby-plugin-sitemap: ^2.1.0 => 2.2.6
    gatsby-source-filesystem: ^2.0.36 => 2.1.9
    gatsby-transformer-sharp: ^2.1.21 => 2.2.6
@squelix squelix changed the title Fixed image with media query are not resized [gatsby-image] Fixed image with media query are not resized Aug 20, 2019
@lannonbr lannonbr added the topic: media Related to gatsby-plugin-image, or general image/media processing topics label Aug 20, 2019
@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Sep 10, 2019
@gatsbot
Copy link

gatsbot bot commented Sep 10, 2019

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@kimbaudi
Copy link
Contributor

kimbaudi commented Sep 10, 2019

@squelix - I think I'm also facing this same issue as well. I will try to create a minimal reproducible demo project, but at least wanted to comment that this issue shouldn't be stale.

BTW, I think you made a typo. In your graphql query, you name your files lovysDesktop and lovysMobile, but in your sources, you reference the files as data.imageDesktop and data.imageMobile.

@taj
Copy link

taj commented Sep 18, 2019

facing the same issue as well

@squelix
Copy link
Author

squelix commented Sep 19, 2019

@squelix - I think I'm also facing this same issue as well. I will try to create a minimal reproducible demo project, but at least wanted to comment that this issue shouldn't be stale.

BTW, I think you made a typo. In your graphql query, you name your files lovysDesktop and lovysMobile, but in your sources, you reference the files as data.imageDesktop and data.imageMobile.

I fixed my message, thanks :)

@gatsbot
Copy link

gatsbot bot commented Sep 30, 2019

Hey again!

It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it.

Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.

As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks again for being part of the Gatsby community!

@gatsbot gatsbot bot closed this as completed Sep 30, 2019
@yertech
Copy link

yertech commented Nov 27, 2019

Same issue for me with fixed Img. It always takes the first element in the array

@squelix
Copy link
Author

squelix commented Dec 17, 2019

yes :/

@zeeshanaligold
Copy link

@squelix did u get fixed?

@squelix
Copy link
Author

squelix commented Jan 7, 2020

No... I don't have time to do it by myself, sorry :(

@qramilq
Copy link

qramilq commented Feb 26, 2020

@squelix if I understood correctly, you can try to set media-query to mobile image too.
In your case yo can try to use (max-width: 767px)

const sources = [
  {
    ...data.imageMobile.childImageSharp.fixed,
   media: `(max-width: 767px)`
  },
  {
    ...data.imageDesktop.childImageSharp.fixed,
    media: `(min-width: 768px)`,
  },
]

Gatsby image use window.matchMedia to get current image. Maybe it helps

@LeeRob97
Copy link

LeeRob97 commented Mar 9, 2020

@qramilq this worked for me!

@qramilq
Copy link

qramilq commented Mar 10, 2020

@LeeRob97 you are welcome)

@LeeRob97
Copy link

@qramilq I am running into a strange issue with the media queries, when I am in development mode things work fine, but as soon as I create my production build the first element in the array is always loaded in, regardless of the media query. I have to resize my window in order for things to start working correctly.

@qramilq
Copy link

qramilq commented Mar 16, 2020

@LeeRob97 did you tried to cleat .cache and public folders before run build? Cause I don't think, that production version makes so seriously changes instead of development version.
Also, you can look to the gatsby-image source code, especially, at this line https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-image/src/index.js#L87 to understand, how it works)

@meros
Copy link

meros commented Apr 1, 2020

I encountered this as well - the right image is loaded again when I resize the window over one of the media query boundaries... Development works fine, production suffers from the problem.

@meros
Copy link

meros commented Apr 1, 2020

Here is a repro:
https://github.com/meros/gatsby-bug-repro-image-media-queries
So it turns out that the I do get right image, but it's layouting is not right at first load.

To trigger the bug, do:
yarn && yarn build
npx serve public

open chrome http://localhost:5000 and resize the window. Try reloading - the image is size weirdly until you again resize window to a point where you hit one of the media query boundaries.

@LeeRob97
Copy link

@qramilq I switched my images from type fixed to fluid and that works.

@crperez5
Copy link

There's definitely a bug here.

Gatsby will pick a different image depending on the environment.

  • If development, it will pick the correct image based on the media property.
  • If production, it will pick the first image on the list.

Would be nice if someone could have a look at it.

Thanks.

@comoser
Copy link
Contributor

comoser commented Jun 24, 2020

@gatsbyjs/core how is this closed when people are still having this problem nowadays?
PS I'm having the exact same issues, so maybe we can reopen the ticket 👍

@KosGrillis
Copy link

I'm experiencing the same problem, are we able to reopen this one?

I query for my images as follows:

imgLg: img {
  fixed(width: 734) {
    src
    srcSet
    width
    height
  }
}
imgMd: img {
  fixed(width: 568) {
    src
    srcSet
    width
    height
  }
}
imgSm: img {
  fixed(width: 375) {
    src
    srcSet
    width
    height
  }
}

I set up my sources as follows:

const imgSources = [
    {
      ...imageContent.imgSm.fixed,
      media: theme.breakpoints.sm,
    },
    {
      ...imageContent.imgSm.fixed,
      media: theme.breakpoints.md,
    },
    {
      ...imageContent.imgSm.fixed,
      media: theme.breakpoints.lg,
    },
]

In development this works fine, but in production the first source in that array is matched, until the screen is resized. Is it something to do with SSR?

@siffogh
Copy link

siffogh commented Jul 15, 2020

Exactly the same issue, works on development but not in the production build.

@pvdz
Copy link
Contributor

pvdz commented Jul 15, 2020

Anybody willing to put up a PR to fix whatever the bug is?

@siffogh
Copy link

siffogh commented Jul 15, 2020

I fixed it by using directly gatsby-plugin-sharp and not relying on gatsby-image:

export default function Home() {
  const { mobileImage, desktopImage } = getImageSources();

  return (
    <div>
         ...
        <picture>
          <source media="(min-width: 480px)" srcSet={desktopImage.srcSet} />
          <img
            src={mobileImage.src}
            srcSet={mobileImage.srcSet}
            alt="home"
            loading="lazy"
          />
        </picture>
      ....
    </div>
  );
}

// P.S. You can also just grab src and srcSet instead of using `GatsbyImageSharpFixed` fragment
function getImageSources() {
  const data = useStaticQuery(graphql`
    query {
      mobileImage: file(relativePath: { eq: "home.png" }) {
        childImageSharp {
          fixed(width: 500) {
            ...GatsbyImageSharpFixed
          }
        }
      }
      desktopImage: file(relativePath: { eq: "home.png" }) {
        childImageSharp {
          fixed(width: 900) {
            ...GatsbyImageSharpFixed
          }
        }
      }
    }
  `);

  return {
    mobileImage: data.mobileImage.childImageSharp.fixed,
    desktopImage: data.desktopImage.childImageSharp.fixed
  };
}

@kaushalyap
Copy link

@KyleAMathews Please reopen this issue.

@polarathene
Copy link
Contributor

This issue has been resolved with the following PR #24811

Please subscribe to that and await it being reviewed and merged.

@pvdz
Copy link
Contributor

pvdz commented Jul 28, 2020

Reopening as there is a PR for it

@pvdz pvdz reopened this Jul 28, 2020
@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Jul 29, 2020
@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Aug 18, 2020
@polarathene
Copy link
Contributor

Not stale, awaiting feedback on how to proceed from core team.

@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Aug 18, 2020
@github-actions
Copy link

github-actions bot commented Sep 7, 2020

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here.
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Sep 7, 2020
@polarathene
Copy link
Contributor

Not stale, awaiting feedback on how to proceed from core team.

@github-actions github-actions bot removed the stale? Issue that may be closed soon due to the original author not responding any more. label Sep 8, 2020
@iheathers
Copy link

iheathers commented Jan 25, 2021

@squelix

const sources = [
{ ...data.imageMobile.childImageSharp.fixed,
media: (max-width: 768px),},

{
...data.imageDesktop.childImageSharp.fixed,
media: (max-width: 1920px),
},
]

I did something like this to get it to work. I would love a mobile-first approach but this was the only way I got it to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: media Related to gatsby-plugin-image, or general image/media processing topics
Projects
None yet