Watch a live product demo 👀 See how Sanity powers richer commerce experiences

GatsbyImage - GraphQL TypeError: Cannot Read Property 'Split' of Undefined

12 replies
Last updated: Mar 25, 2021
How do you get GatsbyImage to work with gatsby-sanity-source?I have implemented this (
https://github.com/sanity-io/gatsby-source-sanity#using-images ) exactly as shown in the example, but cannot get it to work as shown.I have formatted my query exactly as shown (notice just the “asset”, without the “fluid, src, or gatsbyImage:

query vehicleQuery($slug: String) {
  sanityVehicle(id: {eq: $slug}) {
    id
    _id
    modellbetegnelse
    manufacturer {
      label
      logoimage {
        asset 
      }
    }
However, this gives me an error on build:

GraphQLError: Field "asset" of type "SanityImageAsset" must have a selection of subfields. Did you mean "asset { ... }"?


If I add fluid, src, and even gatsbyImageData to the query:

query vehicleQuery($slug: String) {
  sanityVehicle(id: {eq: $slug}) {
    id
    _id
    modellbetegnelse
    manufacturer {
      label
      logoimage {
        asset {
          fluid {
            src
          }
          gatsbyImageData
        }
      }
    }
I get a different error:

WebpackError: TypeError: Cannot read property 'split' of undefined


failed Building static HTML for pages - 0.243s

 ERROR #95313 

Building static HTML failed for path "/vehicles/-d9d34e74-0ffb-5ae8-b103-7895bfbd02c7"

See our docs page for more info on this error: <https://gatsby.dev/debug-html>

  161 |     var hasOriginalRatio = desiredAspectRatio === dimensions.aspectRatio;
  162 |     var outputHeight = Math.round(height ? height : width / desiredAspectRatio);
&gt; 163 |     var imgUrl = isOriginalSize(width, outputHeight) ||
      |                                                 ^
  164 |         (hasOriginalRatio &amp;&amp; width &gt; dimensions.width &amp;&amp; outputHeight &gt; dimensions.height)
  165 |         ? url
  166 |         : url + "?w=" + width + "&amp;h=" + outputHeight + "&amp;fit=crop";


  WebpackError: TypeError: Cannot read property 'split' of undefined
  
  - getGatsbyImageProps.ts:163 
    gatsby-starter-hello-world/src/images/getGatsbyImageProps.ts:163:49
  
  - getGatsbyImageProps.ts:393 
    gatsby-starter-hello-world/src/images/getGatsbyImageProps.ts:393:21
  
  - vehicledetail.js:14 
    gatsby-starter-hello-world/src/templates/vehicledetail.js:14:36
  
  - extends.js:8 
    [gatsby-starter-hello-world]/[@babel]/runtime/helpers/extends.js:8:1
  
  - extends.js:14 
    [gatsby-starter-hello-world]/[@babel]/runtime/helpers/extends.js:14:1
  
  - extends.js:13 
    [gatsby-starter-hello-world]/[@babel]/runtime/helpers/extends.js:13:1
  
  - static-entry.js:263 
    gatsby-starter-hello-world/.cache/static-entry.js:263:20
  
  - history.js:26 
    [gatsby-starter-hello-world]/[@gatsbyjs]/reach-router/es/lib/history.js:26:1
A bit lost here… if I skip GatsbyImage altogether, and just use fluid src with &lt;img src.., the site builds fine, including showing images.


System:
    OS: macOS 11.2.3
    CPU: (8) x64 Intel(R) Core(TM) i7-7920HQ CPU @ 3.10GHz
    Shell: 5.8 - /bin/zsh
  Binaries:
    Node: 15.12.0 - /usr/local/bin/node
    npm: 7.6.3 - /usr/local/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 89.0.4389.90
    Firefox: 86.0
    Safari: 14.0.3
  npmPackages:
    gatsby: ^3.0.1 =&gt; 3.1.2
    gatsby-plugin-image: ^1.1.1 =&gt; 1.1.2
    gatsby-plugin-postcss: ^4.1.0 =&gt; 4.1.0
    gatsby-plugin-sharp: ^3.1.1 =&gt; 3.1.2
    gatsby-source-filesystem: ^3.1.0 =&gt; 3.1.0
    gatsby-source-sanity: ^7.0.0 =&gt; 7.0.0
    gatsby-transformer-sharp: ^3.1.0 =&gt; 3.1.0
  npmGlobalPackages:
    gatsby-cli: 3.1.0
any ideas?
Mar 24, 2021, 8:54 AM
Hey
user P
I can help you with this
Mar 24, 2021, 10:02 PM
Here’s your graphql page query:

profiles {
  id: _key
  name
  bio
  image {
    asset {
      fluid(maxWidth: 700) {
        ...GatsbySanityImageFluid
      }
    }
  }
}
Mar 24, 2021, 10:04 PM
See how I’m using the
...GatsbySanityImageFluid
fragment here
Mar 24, 2021, 10:04 PM
that’s what I think you’re missing
Mar 24, 2021, 10:04 PM
Then here’s my custom Image component
Mar 24, 2021, 10:05 PM
import React, { useEffect, useRef, useState } from 'react';
import Image from 'gatsby-image';
import './Image.scss';

const ResponsiveImage = (props) =&gt; {
  const {
    image,
    className = '',
    style = {},
    onLoaded,
  } = props;

  const [loaded, setLoaded] = useState(false);
  const imgRef = useRef();

  useEffect(() =&gt; {
    if (onLoaded &amp;&amp; imgRef.current &amp;&amp; imgRef.current.state.imgLoaded) {
      onLoaded();
    }
  }, [imgRef.current, loaded]);

  return (
    &lt;Image
      className={className}
      fluid={image.asset.fluid}
      alt={image.alt || ''}
      style={style}
      ref={imgRef}
      onLoad={() =&gt; setLoaded(true)}
    /&gt;
  );
};

export default ResponsiveImage;
Mar 24, 2021, 10:05 PM
You don’t need all that (eg. the onLoad handler), but its a nice little bit of functionality
Mar 24, 2021, 10:05 PM
Here’s a little extra
Mar 24, 2021, 10:06 PM
I would recommend you use
gatsby-plugin-sanity-image
Mar 24, 2021, 10:06 PM
It’s made by
user L
, and I wish I had used it earlier in my project, because it allows for extra control over images sourced from Sanity than
gatsby-source-sanity
Mar 24, 2021, 10:08 PM
Very nice, thanks so much for the help
Mar 25, 2021, 7:43 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Clean Next.js + Sanity app
- Template

Official(made by Sanity team)

A clean example of Next.js with embedded Sanity ready for recomposition.

Cody Olsen
Go to Clean Next.js + Sanity app

Blog with Built-in Content Editing
- Template

Official(made by Sanity team)

A Sanity-powered blog with built-in content editing and instant previews.

Go to Blog with Built-in Content Editing