Gatsby "Invalid string length" error with unknown document ID from Sanity

2 replies
Last updated: Aug 18, 2020
a gatsby site I’m working on started crashing this morning, which is weird since no code has been changed between today and yesterday and the site worked fine yesterday.
I’m getting a cryptic error stating

 ERROR 

dbb54862-d17c-4e11-a1f0-701364e617ac


 ERROR 

Invalid string length



  RangeError: Invalid string length
  
  - JSON.stringify
  
  - query-runner.js:130 queryRunner
    [web]/[gatsby]/dist/query/query-runner.js:130:27
  
  - task_queues.js:97 processTicksAndRejections
    internal/process/task_queues.js:97:5
  
  - task_queues.js:66 runNextTicks
    internal/process/task_queues.js:66:3
  
  - timers.js:429 processImmediate
    internal/timers.js:429:9
  


 ERROR 

UNHANDLED REJECTION Cannot read property 'sourceMessage' of undefined



  TypeError: Cannot read property 'sourceMessage' of undefined
  
I tried to find a sanity document matching that ID through sanity cli but it could not find any documents matching that ID. I have no idea where the error is so any tips are appriciated! Full error in comments


➜  studio git:(master) sanity documents get dbb54862-d17c-4e11-a1f0-701364e617ac
Error: Failed to fetch document:
Document not found
AI Update

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 build

This 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 thread
2 replies
➜  web git:(master) gatsby build
success open and validate gatsby-configs - 0.030s
success load plugins - 1.111s
success onPreInit - 0.014s
success delete html and css files from previous builds - 0.017s
success initialize cache - 0.006s
success copy gatsby files - 0.055s
warn [sanity] Using `watchMode` when not in develop mode might prevent your build from completing
info [sanity] Fetching remote GraphQL schema
info [sanity] Transforming to Gatsby-compatible GraphQL SDL
info [sanity] Stitching GraphQL schemas from SDL
success onPreBootstrap - 0.274s
success createSchemaCustomization - 0.011s
info [sanity] Fetching export stream for dataset
warn [sanity] Document "032bcc84-a29f-43e7-813a-9891b518c073" has type defaultPage (SanityDefaultPage), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "14eb51af-b637-49ff-acec-57e79decde73" has type defaultPage (SanityDefaultPage), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "2d180332-eaeb-46d6-acfd-1f32162b9255" has type subPages (SanitySubPages), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "2fbbb9a2-96e6-49f3-9e9d-684a42fca7d8" has type subPages (SanitySubPages), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "3ad3469a-a80a-4368-a17a-682bc30d2006" has type subPages (SanitySubPages), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "474ff26d-60f3-4695-958a-bd4b4df581d6" has type seo (SanitySeo), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "58448333-f086-4991-be69-ba38141e7316" has type defaultPage (SanityDefaultPage), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "616066c8-a50b-4bc5-a0ba-1a1661c148a9" has type frontPage (SanityFrontPage), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "76e070d6-2423-48b3-b83c-73ee8412e713" has type projects (SanityProjects), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "78df9b1e-92d3-4914-a54e-493c241f793f" has type defaultPage (SanityDefaultPage), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "7e2bdbf3-f8cb-416d-9c16-2238775b526c" has type defaultPage (SanityDefaultPage), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "8cf47c47-9442-466a-841c-051f4b3a1023" has type defaultPage (SanityDefaultPage), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "dbda5458-b53f-48a2-b7df-343e665405c6" has type projects (SanityProjects), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
warn [sanity] Document "e1bea075-bfe3-431d-a404-a466127606d7" has type projects (SanityProjects), which is not declared in the GraphQL schema. Make sure you run "graphql deploy". Skipping document.
info [sanity] Overlaying drafts
info [sanity] Watch mode enabled, starting a listener
info [sanity] Done exporting!
success source and transform nodes - 0.309s
warn The type `SanityImageAsset` does not explicitly define the field `childImageSharp`.
On types with the `@dontInfer` directive, or with the `infer` extension set to `false`, automatically adding fields for children types is deprecated.
In Gatsby v3, only children fields explicitly set with the `childOf` extension will be added.

success building schema - 0.463s
info Creating landing page: /training-materials/
info Creating landing page: /aquarium-of-genova/
info Creating landing page: /about-in-tour-sub-level-1-page-4/
info Creating landing page: /training-material-sub-level-1-page-1/
info Creating landing page: /about-in-tour/
info Creating landing page: /about-in-tour-sub-level-1-page-3/
info Creating landing page: /about-in-tour-sub-level-1-page-2/
info Creating landing page: /consortium-sub-level-page-1/
info Creating landing page: /about-in-tour-sub-level-2-page-1/
info Creating landing page: /about-in-tour-sub-level-2-page-3/
info Creating landing page: /consortium/
info Creating landing page: /about-in-tour-sub-level-1-page-1/
info Creating landing page: /pilots-sub-level-1-page-1-/
info Creating landing page: /about-in-tour-sub-level-2-page-2/
info Creating landing page: /about-in-tour-sub-level-1-page-5/
info Creating landing page: /pilots-page/
success createPages - 0.094s
success createPagesStatefully - 0.048s
success onPreExtractQueries - 0.003s
success update schema - 0.038s
success extract queries from components - 0.460s
success write out requires - 0.005s
success write out redirect data - 0.005s
success Build manifest and related icons - 0.109s
success onPostBootstrap - 0.115s
⠀
info bootstrap finished - 6.283s
⠀
success Building production JavaScript and CSS bundles - 12.942s
success Rewriting compilation hashes - 0.010s
[====                        ]   13.117 s 4/23 17% run queries

 ERROR

f6831b11-df2c-4ea0-aa79-d729e0696d1e


 ERROR

Invalid string length



  RangeError: Invalid string length

  - JSON.stringify

  - query-runner.js:130 queryRunner
    [web]/[gatsby]/dist/query/query-runner.js:130:27

  - task_queues.js:97 processTicksAndRejections
    internal/process/task_queues.js:97:5

  - task_queues.js:66 runNextTicks
    internal/process/task_queues.js:66:3

  - timers.js:429 processImmediate
    internal/timers.js:429:9



 ERROR

UNHANDLED REJECTION Cannot read property 'sourceMessage' of undefined



  TypeError: Cannot read property 'sourceMessage' of undefined

  - error-map.js:12 Object.text
    [lib]/[gatsby-cli]/lib/structured-errors/error-map.js:12:37

  - construct-error.js:32 constructError
    [lib]/[gatsby-cli]/lib/structured-errors/construct-error.js:32:18

  - index.js:194 Object.error
    [lib]/[gatsby-cli]/lib/reporter/index.js:194:29

  - index.js:185
    [lib]/[gatsby-cli]/lib/reporter/index.js:185:46

  - Array.map

  - index.js:185 Object.error
    [lib]/[gatsby-cli]/lib/reporter/index.js:185:24

  - index.js:144 Object.panic
    [lib]/[gatsby-cli]/lib/reporter/index.js:144:28

  - create-cli.js:31
    [lib]/[gatsby-cli]/lib/create-cli.js:31:74

  - task_queues.js:97 processTicksAndRejections
    internal/process/task_queues.js:97:5

  - task_queues.js:66 runNextTicks
    internal/process/task_queues.js:66:3

  - timers.js:429 processImmediate
    internal/timers.js:429:9

not finished run queries - 85.021s
Turns out that setting resolveReferences depth too high causes this
_rawContent(resolveReferences: { maxDepth: 50 })
Changed to
_rawContent(resolveReferences: { maxDepth: 20 })

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.

Was this answer helpful?