Because the post Gatsby descrete notes is too long, I write a separate post just for images in Gatsby.
- Static images (icon, logo, favicon): just
importand useimg.
svgcannot be used withchildImageSharp, just use<img>with itspublicURL.
png,jpgcan be used with<Img>andchildImageSharp.
- Data can be used:
jsonoryaml, cannot usejsfor images.
- List of images are get up to
edges. Afteredges, there are a loop ofnodes.
querycomponents must placed in<StaticQuery />.
- Different screen solution: use
fixed.
- Different screen size: use
fluid.
- Use http://localhost:8000/___graphql to check the
query.
- Use
{condition && condition && output}instead of usingif..else...
- The relative path of images is compared with the path of data file where the urls are indicated.
Read this post from CSS-Tricks to know which images should be processed, which ones should not.
- No processing required: "static images", icons and logos, favicons 👉 We can
importand use<img>to directly import them.
- Process required: PNG, JPG files, gallery, ...
Install stuffs following this doc
So that we can have the lazing loading / resizing / blur-up (traced placeholder) effects.
Below technique is just for single photos.
👉 Most of tutorials like this or this don't talk about
StaticQuery (Gatsby v2). I followed them but it didn't work, we have to use StaticQuery to make things work! 👉 The reason is that the (old) page query can only be added to page components (in my try, I add in Header.js component). StaticQuery can be used as a replacement of page query, it can be added to any component. (ref)👉 There are 2 types:
fixed: has a set width and height and is for supporting different screen resolutions. It acceptswidthandheight.
fluid: has a max-width and sometimes a max-height, and will create multiple images for supporting different screen sizes. It acceptsmaxWidthandmaxHeight.
You need to use them with fragments like
GatsbyImageSharpFixed_tracedSVG or GatsbyImageSharpFluid (more).If you want to query to an image in
/src/images/example.png, you can use,gatsby-plugin-sharp / gatsby-image doesn't handle SVGs or GIFs. If you want to use your svg you, e.g. could import / use it like import yourSVG from './logo_large.svg'. (ref)Put
favicon.png in src/images and then change gatsby-config.jsYou need to restart
gastby to see the result!Suppose that we store some images in
/src/images/ and we indicate them in a json file like this,Images (
github.svg, linkedin.svg, math2it.png) are stored in the same folder of your json file, that why we put ./ before them!❓ There are both
png and svg files in the list. We cannot use childImageSharp for svg files. How can we "loop" through each item and consider differently svg and png files?💡 Read this tutorial to read data from a json file.
- Having a json file, in this example, it's
/data/AccountItem.json(you can name it whatwever you want but its name is really important for using later!!!)
- Install
- Inside
/gatsby-config.js, add following (direction inpathis the direction to the folder containing your json file),
- Suppose that we wanna load images in a component
/src/components/ListAccount.js. Put in it, (ref)
Explaination:
- We have to put
extensionandpublicURLinsideiconbecause there aresvgfiles!
- For more intuition, look at http://localhost:8000/___graphql.
- In
edges, there are manynodes, each of them is relating to the item insideAccountItem.json. That's why the input data for<ListAccount />must bedata.allAccountItemsJson.edges.
- For each
accountinaccounts, we have to passaccount.nodebefore each attributename,icon.
{condition && condition && output}is a trick for not usingif...else.
👉 In the case you store images in a different folder, let's say
/src/images/accounts/, you have to indicate it in /data/AccountItems.json:Suppose our
yaml file,The same technique as in the case of
json file. In this case, we need to install gatsby-transformer-yamlIn your
gatsby-config.jsIn the previous step,
query is considered inside the component file. What if you wanna query data and then pass it in a JSX components? (check index.js for an example)There is a data file
/data/MostProudOfItems.yaml.👉 This is also a way you use the
name indicate in gatsby-config.js.Suppose that you wanna show all images in
/sketches/. (ref)💢 Above method is only works with
/sketches (folder locates at the root of site). It doesn't work with /src/images/sketches, for example. I don't know why!👉 If you want to get all images from a folder (without using
sourceInstanceName) you can use relativeDirectory in the query. Suppose that we have 2 folders with the same name sketches, one is in /content/sketches, one is in /src/images/sketches. The following code will load all images in these two folders!💢 Make sure the name of your folder is unique if you don't want to load images from a wrong location.
- A comprehensive guide to images in Gatsby by James Allardice.