Gatsby "Invalid string length" error with unknown document ID from Sanity
This is a frustrating issue, especially when nothing in your code changed! The "Invalid string length" error at query-runner.js:130 with JSON.stringify typically indicates that Gatsby is trying to serialize data that exceeds JavaScript's maximum string size limits. Since the site worked yesterday and broke today without code changes, this almost certainly points to a content change in Sanity.
Why the UUID Isn't Helpful
The UUID dbb54862-d17c-4e11-a1f0-701364e617ac in the error message appears to be Gatsby's internal error ID rather than a Sanity document ID, which is why sanity documents get couldn't find it. This makes debugging tricky because Gatsby isn't directly telling you which document or query is causing the problem.
Most Likely Causes
Since no code changed, someone probably:
- Uploaded an extremely large image or file
- Added a massive amount of content to a document
- Created or modified a document with very large array fields
- Edited a document that's now triggering a query to return too much data
Troubleshooting Steps
1. Check Recent Sanity Content Changes
In your Sanity Studio, look at the document history to see what was modified in the last 24 hours. Look for:
- New or updated documents
- Large image uploads
- Documents with extensive arrays or portable text blocks
2. Find Your Largest Documents
Run this GROQ query in Vision (your Studio's query tool) to identify potentially problematic documents:
*[!(_id in path("_.**"))] {
_id,
_type,
"approximateSize": length(string::split(string(*), ""))
} | order(approximateSize desc)[0...20]This gives you a rough idea of which documents are largest by character count.
3. Review gatsby-source-sanity Configuration
Check your gatsby-config.js to see if you're pulling more data than necessary:
{
resolve: 'gatsby-source-sanity',
options: {
projectId: 'your-project-id',
dataset: 'your-dataset',
overlayDrafts: false, // Try disabling if you have this enabled
watchMode: false,
}
}4. Increase Node Memory (Temporary Fix)
While you investigate, you might be able to get builds working again by increasing Node's memory allocation:
NODE_OPTIONS="--max-old-space-size=8192" gatsby buildThis isn't a permanent solution but can help you get unblocked while you identify the root cause.
Longer-Term Solutions
Once you identify the problematic content:
- Split large documents: Break oversized documents into smaller, related pieces
- Optimize image handling: Ensure you're using Sanity's image pipeline properly and not embedding raw image data
- Limit query scope: If a specific document type is causing issues, consider filtering it in your GraphQL queries or excluding it from gatsby-source-sanity
- Review array fields: Documents with hundreds or thousands of array items can grow very large when serialized
Additional Debugging
If you're still stuck, try temporarily removing gatsby-source-sanity from your config and rebuilding to confirm that's where the issue originates. Then add it back with a more restrictive configuration to narrow down which content is problematic.
The combination of "worked yesterday, broken today" + "no code changes" makes this almost certainly a content issue rather than a configuration problem. Start with your Sanity Studio's recent history—that's your best bet for finding the culprit quickly.
Show original thread2 replies
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.