<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Shrey Sachdeva</title>
    <description>The latest articles on DEV Community by Shrey Sachdeva (@shreysachdeva2000).</description>
    <link>https://dev.to/shreysachdeva2000</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F278884%2Fd5358136-7ffa-44bb-9166-50232a9a203e.jpeg</url>
      <title>DEV Community: Shrey Sachdeva</title>
      <link>https://dev.to/shreysachdeva2000</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shreysachdeva2000"/>
    <language>en</language>
    <item>
      <title>Create a Medium like Lazy Image Loading Effect Using Gatsby.js in 5 minutes</title>
      <dc:creator>Shrey Sachdeva</dc:creator>
      <pubDate>Tue, 11 Aug 2020 07:56:03 +0000</pubDate>
      <link>https://dev.to/srmkzilla/create-a-medium-like-lazy-image-loading-effect-using-gatsby-js-in-5-minutes-9d</link>
      <guid>https://dev.to/srmkzilla/create-a-medium-like-lazy-image-loading-effect-using-gatsby-js-in-5-minutes-9d</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Ensuring that users get the best experience with images on the web can be a tedious task. However, with Gatsby, we can leverage the power of &lt;a href="https://www.gatsbyjs.org/packages/gatsby-image/"&gt;gatsby-image&lt;/a&gt; to get these speedy, optimised images with a little setup and a rich toolset.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You probably already saw this effect here on Medium, the blurry images with a nice “fade in” animation being changed by their respective originals. This effect is called Progressive/Lazy Image Loading.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;What is Lazy Image Loading?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Lazy Image Loading is a practice that’s been around for a decade and for good reason: images are usually the heaviest downloads on a given webpage and avoiding unloading images that are never seen saves the user bandwidth. With this technique, you can sharply load the images on your website when required, and use blurred placeholders while the original image is being lazy-loaded. And when it finishes loading, you can do a smooth transition to the original image. It’ll help you shave-off hours of work, use fewer resources, improve performance and build a meaningful experience for the user.&lt;/p&gt;

&lt;p&gt;Gatsby does a great job optimizing images. With plugins and some configuration, you can setup image preloading and the blur-up effect in just a matter of minutes. This helps with a smoother user experience that is faster and way, way better!&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s code right in!
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Step 1: Install Dependencies
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;If you used the CLI, you’ll have to install these packages withyarn. If you used npm install, then continue to use npm so you don't mix the package installers. I'll be using npm for the rest of this demo.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To install &lt;a href="https://www.gatsbyjs.org/packages/gatsby-image/"&gt;gatsby-image &lt;/a&gt;with npm run:&lt;/p&gt;

&lt;p&gt;npm install gatsby-image&lt;/p&gt;

&lt;p&gt;We’ll also need to install two more npm packages that are required to process the images.&lt;/p&gt;

&lt;p&gt;npm install gatsby-transformer-sharp gatsby-plugin-sharp&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can read more about how these packages work, &lt;a href="https://www.gatsbyjs.org/packages/gatsby-transformer-sharp/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 2: Configure Gatsby
&lt;/h3&gt;

&lt;p&gt;Once these packages are installed we need to include them manually in our gatsby-config.js file.&lt;/p&gt;

&lt;p&gt;Include the following snippet to the file:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  siteMetadata: {
    title: 'Gatsby Default Starter'
  },
  plugins: [
    **{
      resolve: `gatsby-source-filesystem`,
      options: {
        path: `${__dirname}/src/images`,
        name: 'images'
      }
    },**
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `pages`,
        path: `${__dirname}/src/pages`,
      }
     },

    **'gatsby-transformer-sharp',
    'gatsby-plugin-sharp',
 **],
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This snippet will also allow us to create file nodes from our images that can be used later to query the /images folder inside the /src directory using GraphQL.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Writing queries in GraphQL
&lt;/h3&gt;

&lt;p&gt;You can navigate to GraphiQL which is an integrated development environment (IDE). It’s a powerful (and all-around awesome) tool you’ll use often while building Gatsby websites.&lt;/p&gt;

&lt;p&gt;You can access it when your site’s development server is running — normally at &lt;a href="http://GraphiQL%20is%20the%20GraphQL%20integrated%20development%20environment%20(IDE).%20It%E2%80%99s%20a%20powerful%20(and%20all-around%20awesome)%20tool%20you%E2%80%99ll%20use%20often%20while%20building%20Gatsby%20websites.%20%20You%20can%20access%20it%20when%20your%20site%E2%80%99s%20development%20server%20is%20running%E2%80%94normally%20at%20http://localhost:8000/___graphql"&gt;http://localhost:8000/___graphql&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I have added 4 &lt;a href="https://www.dropbox.com/sh/xmvkmcronshzis0/AABanbmtca-8e9Ef0LcghAz-a?dl=0"&gt;images&lt;/a&gt; inside my /src/images directory and I will write the following query to get the contents of the /images folder.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  images: allFile {
    edges {
      node {
        relativePath
        name
        childImageSharp {
          fluid {
            srcSet
            src
            srcWebp
            base64
            originalImg
            originalName
            presentationHeight
            presentationWidth
            sizes
            srcSetWebp
            tracedSVG
            aspectRatio
          }
        }
      }
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Writing this query will return a string that you can see in the response column. This essentially means that Gatsby is able to find your image and process it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2VqJDWrn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3840/1%2AZCNyJCaecfk-203XjIf0dA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2VqJDWrn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/3840/1%2AZCNyJCaecfk-203XjIf0dA.png" alt="http://localhost:8000/__graphql"&gt;&lt;/a&gt;&lt;em&gt;&lt;a href="http://localhost:8000/__graphql"&gt;http://localhost:8000/__graphql&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can read more about how Gastby uses GraphQL &lt;a href="https://www.gatsbyjs.org/docs/graphql/"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Step 4: Creating Components
&lt;/h3&gt;

&lt;p&gt;Now that we have the query string, we can use it in our component. I will create a image.js component inside /src/components directory.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here, we need to import &lt;em&gt;StaticQuery&lt;/em&gt; as well as &lt;em&gt;graphql&lt;/em&gt; from &lt;em&gt;gatsby&lt;/em&gt; and &lt;em&gt;Img *from *gatsby-image&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { StaticQuery, graphql } from "gatsby"
import Img from "gatsby-image"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;We’ll now add the query that we made earlier to this component. Our React component should return a  tag, which has a query property and a render property.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;StaticQuery 
  query={
    graphql`{
      query {        
        images: allFile {
          edges {
            node {
              relativePath
              name
              childImageSharp {
                fluid {
                  ...GatsbyImageSharpFluid
                }
              }
            }
          }
        }
      }
  `} 
  render={(data) =&amp;gt; (
    &amp;lt;&amp;gt;
      {*/* our JSX will go in here */*}
    &amp;lt;/&amp;gt;
  )}
/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;Notice how we are using &lt;a href="https://www.gatsbyjs.org/docs/gatsby-image/#fluid-images"&gt;...GatsbyImageSharpFluid&lt;/a&gt; here, which is a fragment that replaces all the fields inside the fluid property.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, we need to include our image inside the render property which has to be passed down two props — a fluid and an alt prop.&lt;/p&gt;

&lt;p&gt;The final result looks like this:&lt;/p&gt;

&lt;h2&gt;
  
  
  We’re almost done!
&lt;/h2&gt;

&lt;p&gt;Let’s create a new page that will accept these props to load the images.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: Putting it together
&lt;/h3&gt;

&lt;p&gt;I’ll name this pagegallery.js and place it in the/src/pages folder. And import the Image component we created earlier.&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Image from "../components/Image"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Let’s pass down the props and here’s how our Gallery page should look like:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*const* Gallery = () *=&amp;gt;* (
  &amp;lt;div&amp;gt;  
     &amp;lt;*Image* alt="image_1" filename="image_1.jpg" /&amp;gt;
     &amp;lt;*Image* alt="image_2" filename="image_2.jpg" /&amp;gt;
     &amp;lt;*Image* alt="image_3" filename="image_3.jpg" /&amp;gt;
     &amp;lt;*Image* alt="image_4" filename="image_4.jpg" /&amp;gt;
  &amp;lt;/div&amp;gt;
)

export Gallery
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;It’s all done! Open up &lt;a href="http://localhost:8000/gallery/"&gt;http://localhost:8000/gallery/&lt;/a&gt; after running gatsby develop on your command line and…&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ii32Zbwu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AtOV6k1mvm1IJkGS67jY04w.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ii32Zbwu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/2000/1%2AtOV6k1mvm1IJkGS67jY04w.gif" alt="[https://gist.github.com/shrey-sachdeva2000/07a6b8ef822f2b385a15f186c933fcd6](https://gist.github.com/shrey-sachdeva2000/07a6b8ef822f2b385a15f186c933fcd6)"&gt;&lt;/a&gt;&lt;em&gt;&lt;a href="https://gist.github.com/shrey-sachdeva2000/07a6b8ef822f2b385a15f186c933fcd6"&gt;https://gist.github.com/shrey-sachdeva2000/07a6b8ef822f2b385a15f186c933fcd6&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;&lt;em&gt;Voila!&lt;/em&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;There you have it. A really cool Lazy Loading Image Effect. Oh, by the way, I really wish I could lazy-load the image of the black hole too if only we had it in better pixels(a sincere astrophilia).&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Gatsby does a great job at processing and handling images and by leveraging the power of Gatsby, optimizing images is easy as pie. You can also try out generating a traced SVG of your image while it processes by applying a traceSVG argument and the appropriate &lt;a href="https://www.gatsbyjs.org/packages/gatsby-image/#fragments"&gt;fragment&lt;/a&gt;. Let me know how did that go for you!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you liked this article, don’t forget to give it a 👏 and share it with your friends! And subscribe for more geeky and cool articles in the future.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>gatsby</category>
      <category>react</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
