question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ImageSharp doesn't work for images in siteMetadata

See original GitHub issue

I am trying to apply ImageSharp queries to images included as siteMetadata. For some reason, the nodes with the image path are then defined as String instead of File. Subsequently they aren’t transformed to ImageSharp by gatsby-transformer-sharp:

GraphQL Error Field "photo" must not have a selection since type "String" has no subfields.

  file: src/pages/team.js

   1 | 
   2 |   query TeamQuery {
   3 | 
   4 |     site {
   5 |       siteMetadata {
   6 |         team {
   7 |           teamMembers {
   8 |             experience
   9 |             name
> 10 |             photo {
     |                   ^
  11 |               childImageSharp {
  12 |                resolutions(width: 160, height: 200, quality: 90) {
  13 |                  ...GatsbyImageSharpResolutions
  14 |                }
  15 |               }
  16 |             }
  17 |             position
  18 |             children {
  19 |               name
  20 |               year

I could make the same work for image fields defined in frontmatter (as discussed in #2054). But I couldn’t make it work with siteMetadata.

Here the relevant part of my gatsby-config.js:

const yaml = require('js-yaml')
const fs = require('fs')
const loadYaml = (fname) =>
  yaml.safeLoad(fs.readFileSync(fname, 'utf8'))

module.exports = {
  siteMetadata: {
    ...siteMetadata,
    team: loadYaml('./data/team.yml'),
  },
  plugins: [
    {
      resolve: 'gatsby-source-filesystem',
      options: {
        path: `${__dirname}/src/pages`,
        name: 'pages'
      }
    },
    'gatsby-plugin-sharp',
    'gatsby-transformer-sharp',

And team.yml:

title: Team
teamMembers:
- name: Karin
  photo: img/karin.png
- name: Michael
  photo: img/michael-2.jpg

In order for the photo nodes to become File the relative path must be correct so that the file’s existence can be confirmed (at least this appears to be the case for image fields in frontmatter). But I tried all the possibilities I could think of for the path and couldn’t make it work.

Is there any fundamental difference between having image fields in frontmatter and injecting them as siteMetadata?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
oolothcommented, Dec 1, 2018

I keep running into this issue since it would be nice to define the default site image (for SEO purposes) in the same siteMetadata object as the rest of the site-wide metadata.

It would be good to add support for this e.g. hard code inside Gatsby that siteMetadata lives at the root of the site.

👆

Does anyone know how to do this? I’d be happy to create the PR if someone can summarize how to do it.

2reactions
ilyabocommented, Jan 16, 2018

@MaralS Not really. I am just not using images in siteMetadata. Instead I add collections of images as fields within pages directly. If you are using Netlify CMS, that also requires this workaround to work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stuck on GraphQl query in Gatsby using imageSharp
I am trying to get the following background prop set to a image in my ... site { siteMetadata { title desc }...
Read more >
Part 7: Add Dynamic Images from Data - Gatsby
Introduction In Part 3 , you used gatsby-plugin-image to add static images to your home page. Now that you've worked a bit more...
Read more >
Image Processing in Gatsby - Anvil
Learn how Gatsby's image processing works under the hood, from compressing images and setting cache-control headers to enabling responsive ...
Read more >
Getting Started - Six Labors Documentation.
ImageSharp provides several options for loading and saving images to cover ... The library automatically detects the source image format upon load and...
Read more >
GatsbyJS - Add Dynamic Images from Data - codersnack.com
Now that you've worked a bit more with Gatsby's data layer, ... text for screen readers or in case the image doesn't load...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found