Intermittent data fetch errors

I’m using DatoCMS for a next.js site, and I’m getting very intermittent failures to fetch data. Perhaps 50% of the time I build, I get one request that fails like this:

10:22:02.372 FetchError: invalid json response body at https://graphql.datocms.com/preview reason: Unexpected token < in JSON at position 0
10:22:02.372 at /vercel/path0/node_modules/node-fetch/lib/index.js:272:32

Running the same build again will usually work, so it isn’t an issue related to a specific piece of content.

Any ideas why this is happening?

Hello @kaushik, I’m sorry to hear that!

The problem is unfortunately a bit tricky to analyze, so we need you help to understand what’s going on.

It could be a network issue between Vercel and our CDN, or it could be a slow query that sometimes fail or could be a bug on our end.

To understand what’s happening we need more info:

  • are you able to reproduce the bug locally? Or the problem happens only on Vercel?
  • can you provide the full timestamp with timezone of the next problem that will happen?
  • can you provide the exact GraphQL call that is failing?

To mitigate the problem right now you can implement a retry mechanism on top of the GraphQL call, so that if it just fails occasionally you’ll be covered and the full build will go ahead.

If instead is something we can work on we’ll help you out!

Thank you and sorry, but we cannot do anything on our end without your help :frowning:

I don’t know if this is the cause for you, but I ran into that issue too when we were hitting the API rate limits. By default, Next.js will try to fetch everything as fast as it can, and if you have too many pages or queries (>40/sec or 1000/min), it can temporarily cause a rate limit error.

The “Unexpected token < in JSON at position 0” probably means the error is coming back as an HTML page instead of JSON (the < probably being the start of <html>). If you catch that error (in a try/catch block or promise catching) and log it, you should be able to see the actual error.

If it turns out to actually be a rate limit error, you can use a package like limiter to slow down your concurrent queries to DatoCMS.

Regardless, mat_jack1’s suggestion of retrying on fetch failure also sounds like a good one!

1 Like

Thanks for the suggestion Roger. I was under the impression the error was in the fetch call, but I added a try/catch around the .json() and a retry if it fails. Fingers crossed!

1 Like