Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent Gatsby image from blurring a logo with every reload?

Tags:

reactjs

gatsby

I've read through the gatsby-image docs but can't figure out how to turn something off. By default, it seems gatsby-image loads a thumbnail of an image, then loads the image progressively. Basically a nice placeholder effect. But I find that my logo keeps blurring everytime I change pages. Here's my code:

 <StaticQuery
            query={graphql`
              query {
                file(relativePath: { eq: "appendto_logo.png" }) {
                  childImageSharp {
                    # Specify the image processing specifications right in the query.
                    # Makes it trivial to update as your page's design changes.
                    fixed(width: 150) {
                      ...GatsbyImageSharpFixed
                    }
                  }
                }
              }
            `}
            render={data => <Img fixed={data.file.childImageSharp.fixed} />}
          />

Any thoughts? How to prevent that blurring effect? Or the thumbnail loading effect...

like image 285
Kyle Pennell Avatar asked Apr 24 '19 23:04

Kyle Pennell


People also ask

How does Gatsby image work?

gatsby-image is a React component specially designed to work seamlessly with Gatsby's GraphQL queries. It combines Gatsby's native image processing capabilities with advanced image loading techniques to easily and completely optimize image loading for your sites.

How was The Great Gatsby Picture installed?

Setting up Gatsby Image To start working with Gatsby Image, install the gatsby-image package along with necessary plugins gatsby-transformer-sharp and gatsby-plugin-sharp . Reference the packages in your gatsby-config. js file. You can also provide additional options to gatsby-plugin-sharp in your config file.


3 Answers

Switching to GatsbyImageSharpFixed_noBase64 solved it (instead of just ...GatsbyImageSharpFixed)

like image 123
Kyle Pennell Avatar answered Nov 09 '22 16:11

Kyle Pennell


Update

critical is now deprecated in favor of loading:

 - <Img critical fixed={ ... } />
 + <Img loading="eager" fixed={ ... } />

Also use _noBase64 sharp fragments to completely remove the blur effect, as @epsilon42's comment & Kyle's answer suggested.

And finally, to persist a component (navigation bar, etc.) between page load, you can wrap the pages in a layout with that component.


Original answer

I think you can pass in a critical prop to Gatsby Image, like this: <Img critical fixed={ ... }> It will always load the image immediately. You might want to add fadeIn in there as well:

Images marked as critical will start loading immediately as the DOM is parsed, but unless fadeIn is set to false, the transition from placeholder to final image will not occur until after the component is mounted.

Gatsby Image docs

like image 44
Derek Nguyen Avatar answered Nov 09 '22 18:11

Derek Nguyen


To avoid the blurring effect you could just hide the placeholder using the placeholderStyle attribute:

<Img fixed={data.logoImage.childImageSharp.fixed} placeholderStyle={{ visibility: "hidden" }}/>

like image 21
flogy Avatar answered Nov 09 '22 17:11

flogy