Sanity fetch causing error decoding lambda response: unexpected end of JSON input on netlify only

Site URL: https://fmt-dp-dev.netlify.app/

I am facing a weird issue with my next.js app deployed on netlify. I say that because I don’t get this issue on Vercel, or when I build locally. This issue only occurs with my netlify app.

Anytime I try to open my app, I get
error decoding lambda response: error decoding lambda response: unexpected end of JSON input

After a lot of debugging, I realized it is coming from any page.tsx file that I have sanity loading data using const results = await client.fetch(query);
When I remove the data fetching, then the app works fine.

Again,

  • I don’t see this issue when I build and run locally.
  • I don’t also see this issue when I deploy to vercel.
  • I only see it when I deploy to netlify which is the platform I want to use.

Also, it is worth mentioning that, this was working previously on next.js v14.x.x. I upgraded to next.js 15 and this issue started.
What confuses me is the fact that this error is caused by sanity in server components. it works fine everywhere else but netlify.

In the build logs I see the below error.

ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"ReferenceError: window is not defined","reason":{"errorType":"ReferenceError","errorMessage":"window is not defined","stack":["ReferenceError: window is not defined"," at 65947 (/var/task/.next/server/chunks/9490.js:1:25520)"," at t (/var/task/.next/server/webpack-runtime.js:1:143)"," at 68517 (/var/task/.next/server/app/(quotes-layout)/quotes/[id]/page.js:1:99786)"," at t (/var/task/.next/server/webpack-runtime.js:1:143)"," at 97290 (/var/task/.next/server/app/(quotes-layout)/quotes/[id]/page.js:1:89024)"," at Function.t (/var/task/.next/server/webpack-runtime.js:1:143)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: ReferenceError: window is not defined"," at process.<anonymous> (file:///var/runtime/index.mjs:1276:17)"," at process.emit (node:events:529:35)"," at emit (node:internal/process/promises:149:20)"," at processPromiseRejections (node:internal/process/promises:283:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}

I can see that there is a "ReferenceError: window is not defined". But I don’t know why it only occurs on netlify. I also don’t use any direct window objects in the place it is pointing to.

any help would be appreciated.

Hard to say without a reproduction that we can test on our end. But, in the past, these issue have bee platform specific. Here’s a comment: "window is not defined" using sanity client on sveltekit while deploying to cloudflare pages + workers · Issue #88 · sanity-io/client · GitHub that says it worked fine on Vercel, but not on Cloudflare.

Not saying for certain that this is something Sanity needs to fix, but we’d need a codebase we can deploy and test to draw some conclusions.

Yes. I can confirm this looks like a sanity issue but it only happens on some platforms.

I am not 100% sure what exactly fixed it, but I updated Sanity to the latest version and also updated my Sanity client config/setup.

When I look at the config file, it doesn’t seem like much has changed, but I got it working in the end with those changes, so anyone facing the same issue can look along those lines.

You can basically do sanity init in a new project and copy the sanity settings over.