GatsbyImage - GraphQL TypeError: Cannot Read Property 'Split' of Undefined
I can see you're experiencing issues with gatsby-plugin-image and gatsby-source-sanity integration in Gatsby v3. The error you're getting is related to how you need to query images with the new plugin. Let me explain what's happening and how to fix it.
The problem is that Gatsby v3 introduced gatsby-plugin-image which replaced the old gatsby-image plugin, and the way you query images has changed significantly. You should not be querying fluid or src anymore - those were part of the old API.
The Solution
1. Update your GraphQL query to use gatsbyImageData:
query vehicleQuery($slug: String) {
sanityVehicle(id: {eq: $slug}) {
id
_id
modellbetegnelse
manufacturer {
label
logoimage {
asset {
gatsbyImageData(
width: 400
placeholder: BLURRED
formats: [AUTO, WEBP]
)
}
}
}
}
}2. Use the GatsbyImage component correctly in your template:
import { GatsbyImage, getImage } from "gatsby-plugin-image"
// In your component:
const VehicleDetail = ({ data }) => {
const image = getImage(data.sanityVehicle.manufacturer.logoimage.asset)
return (
<div>
{image && (
<GatsbyImage
image={image}
alt={data.sanityVehicle.manufacturer.label || "Logo"}
/>
)}
</div>
)
}
export default VehicleDetailWhy You're Getting the Errors
"Field 'asset' must have a selection of subfields" - This happens because GraphQL requires you to specify what data you want from the
assetobject. You can't just queryassetby itself."Cannot read property 'split' of undefined" - This error occurs because when you tried to query
fluidandsrc, those fields don't exist in the newgatsby-plugin-imageAPI, so the data structure being passed togetGatsbyImagePropsis malformed.
Important Notes
- With gatsby-source-sanity, you should be using
gatsbyImageData(notfluidorfixed) - The
getImage()helper function fromgatsby-plugin-imagesafely extracts the image data - Always add a null check (
{image && ...}) before renderingGatsbyImageto prevent errors if the image is missing - Make sure both
gatsby-plugin-imageANDgatsby-plugin-sharpare installed and configured in yourgatsby-config.js
Verify Your Setup
Make sure your gatsby-config.js includes both plugins:
module.exports = {
plugins: [
{
resolve: 'gatsby-source-sanity',
options: {
projectId: 'your-project-id',
dataset: 'your-dataset',
// ... other options
},
},
'gatsby-plugin-image',
'gatsby-plugin-sharp',
],
}The fact that using fluid.src with a regular <img> tag works confirms your Sanity images are properly configured - you just need to use the correct modern API for gatsby-plugin-image. These changes should resolve both errors you're experiencing!
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.