Image display is a common task in web development, how to use it in Gatsby application may seem confused, this article described the answer to it.
Gatsby has many features to win the developers' love and prevails over other mainstream static site generators. One of those features is gatsby-image component. It solved the image optimization problem with the specified dimension scope you defined, progressively and responsively presenting to the webpage gives user a comfortable browsing experience.
Like the official doc said, there are three steps needed to implement a gatsby image in Gatsby website:
In the step 2, you tell the Gatsby build tool, where to find the `root` directory of all the image files. Just like the doc example, the root directory here is `src/images/` of current Gatsby project:
{
resolve: `gatsby-source-filesystem`,
options: {
name: `images`,
path: path.join(__dirname, `src`, `images`),
},
},
In the step 3, you tell gatsby-image component instance where to get the file: graphql query result.
export const query = graphql`
query {
file(relativePath: { eq: "blog/avatars/kyle-mathews.jpeg" }) {
childImageSharp {
# Specify the image processing specifications right in the query.
# Makes it trivial to update as your page's design changes.
fixed(width: 125, height: 125) {
...GatsbyImageSharpFixed
}
}
}
}
`
After seeing this graphql code snippet, you may wonder where's the blog/avatars/kyle-mathews.jpeg? Is it under project root or src/images? let's take a test from a blank gatsby project.
$ gatsby new using-image-in-gatsby
Note: Don't forget the 'data' property in IndexPage properties.
Now, we can start the website without the need to modify other stuff:
$ gatsby develop
Visit the `http://localhost:8000/` in your browser, you'll see the correct result.
This experiment verified our speculation, that is:
gatsy-image query(graphql) using relative path under the path defined in gatsby-source-filesystem as query condition!
Above example use static value blog/avatars/kyle-mathews.jpeg to query in graphql expression. Then, what if the query condition is from markdown file meta data?
Official doc about [Working with image in Markdown posts and pages told us the placing the featuredImage in the same position of markdown file:
example-post.md:
---
title: My Amazing Post
featuredImage: ./awesome-image.png
---
Content goes here!
In this solution, it works though, but, if you have many posts which include many images in each post, the directory structure will grow big dramatically and end up with chaos.
How to restructure the images path in a reasonable way?
plugins: [
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/content`,
name: `content`,
},
},
...
]
separate image files and .md files to different folder
example-post.md:
---
title: My Amazing Post
featuredImage: ../assets/awesome-image.png
---
Business intro is missing...
In a normal web application, html img element can use relative or absolute path to assign src property. We use image in a native and static way like the official doc said, place image under static folder, display image in webpage as we expected. But, when we build and deploy the gatsby site the GitHub Pages, visit it under an URL pattern like this:
https://username.github.io/your-gatsby-website/
The images declared by img tag broken all!
Although all the gatsby-images built by `$ gatsby build --prefx-paths` works, but those native imgs don't work.
What's the solution?
For example:
<img src="/ueofcweb/images/project-home.jpg" className="img-fluid" alt="ultronele screenshot" />
ueofcweb is the github project name, images/project-home.jpg is actually under ueofcweb/static.
If you're refactoring a traditional website which use a large amount of img tags, the second option would be a cheap option. My production official site is migrated from bootstrap/jquey stack, while I was adding it to the Gatsby showcase, I spend hours to figure out that the second approach is the best way to me.
Last note I can give in this post is:
Do not add folders under `static` to gatsby source filesystem, it's a BAD practice ending up with build error `childImageSharp' of null.
Gatsby image is the best responsive image solution which I have met. It frees you from the tedious optimization work in the build phase, add good performance and excellent user experience to your website. It deserves you to spend time to delve into and use proficiently.