<?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>Forem: anisapatel</title>
    <description>The latest articles on Forem by anisapatel (@anisapatel).</description>
    <link>https://forem.com/anisapatel</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%2F408999%2Fbb86fcb0-8526-4a5b-b569-f6e6f55f87fb.png</url>
      <title>Forem: anisapatel</title>
      <link>https://forem.com/anisapatel</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/anisapatel"/>
    <language>en</language>
    <item>
      <title>Optimizing Images with Gatsby Image</title>
      <dc:creator>anisapatel</dc:creator>
      <pubDate>Fri, 28 Aug 2020 20:30:44 +0000</pubDate>
      <link>https://forem.com/anisapatel/optimizing-images-with-gatsby-image-3afh</link>
      <guid>https://forem.com/anisapatel/optimizing-images-with-gatsby-image-3afh</guid>
      <description>&lt;p&gt;Gatsby Image is a react component designed to optimize image loading times for static sites. It lazy-loads the images with a blur-up effect, which speeds up initial page loading times and holds the image position.&lt;/p&gt;

&lt;p&gt;1) Install the gatsby default boilerplate using the Gatsby Cli, containing all the main configuration files to get you up and running quickly and install the following packages which allow you to process the images and use GraphQL queries to access images.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install --save gatsby-cli
gatsby new my-default-starter https://github.com/gatsbyjs
/gatsby-starter default
npm install --save gatsby-image
npm install --save gatsby-transformer-sharp gatsby-plugin-sharp gatsby-source-filesystem
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;2) The starter will have generated a gatsby config file. Here you must add the plugins if they are not there already. The gatsby source filesystem will by default contain a path to the images folder which can be changed if you move the images locally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;plugins: [ 
{
resolve: `gatsby-source-filesystem`,
options: { name: `images`,    
path: `${__dirname}/src/images`,
},    
},    
`gatsby-transformer-sharp`,    
`gatsby-plugin-sharp`
, ...additionalplugins 
]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;3) Add some images to the /src/images folder to test. Fire up the development server &lt;a href="http://localhost:8000/"&gt;http://localhost:8000/&lt;/a&gt; and the GraphiQL playground &lt;a href="http://localhost:8000/___graphql"&gt;http://localhost:8000/___graphql&lt;/a&gt; to write your query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;gatsby develop
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Example query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;query MyQuery {
 file(relativePath: {eq: "blossoms1.jpg"}
 childImageSharp {
 fluid {
   src
   }
  }
 }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;4) Copy the query and for this example we'll use it in the src/pages/index.js homepage. Replace fluid/src with the query fragment ...GatsbyImageSharpFluid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const data = graphql`  
query MyQuery {    
file(relativePath: { eq: "flora.jpg" }) {      
childImageSharp {        
fluid(maxWidth: 1000) {          
...GatsbyImageSharpFluid       
}}}}` 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;5) Import img from gatsby-image and add the img tag as below. You can destructure the query from props.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Img fluid={props.data.file.childImageSharp.fluid} /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Full Component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react"
import { graphql } from "gatsby"
import Layout from "../components/layout"
import SEO from "../components/seo"
import Img from "gatsby-image"
import "./index.css"

const IndexPage = props =&amp;gt; (  
&amp;lt;Layout&amp;gt;    
&amp;lt;SEO title="Home" /&amp;gt;   
&amp;lt;Img fluid={props.data.file.childImageSharp.fluid} /&amp;gt;    
&amp;lt;article&amp;gt;      
&amp;lt;h1&amp;gt;Fleurs De Majorelle&amp;lt;/h1&amp;gt;      
&amp;lt;p&amp;gt;Designers of high quality, bespoke floral creations, traditional or        contemporary in design, arranged to suit your budget.&amp;lt;/p&amp;gt;    
&amp;lt;/article&amp;gt;  
&amp;lt;/Layout&amp;gt;
)
export default IndexPage

export const data = graphql`  
query MyQuery {    
file(relativePath: { eq: "flora.jpg" }) {      
childImageSharp {        
fluid(maxWidth: 1000) {          
...GatsbyImageSharpFluid        
}}}}` 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>gatsby</category>
      <category>react</category>
      <category>graphql</category>
    </item>
  </channel>
</rss>
