Next.js Discord

Discord Forum

No response is returned from route handler

Answered
Giant panda posted this in #help-forum
Open in Discord
Giant pandaOP
OK this is a weird one and unfortunately I am not able to create a pithy repro.

I believe the if (!(res instanceof Response)) guard is getting triggered due to a NEW Response instace!. I have no idea why all of a sudden the standard Response object is changing but using a weakmap/objectId kungfu, I can see I am getting a mismatch between the Response instance used in the above guard and the Response instance the NextResponse is extending.

This seems to only happen with a production build pnpm run build; pnpm run start.

// Working output, we can see instanceOf Response is true and they have the same objectId of 1.
instanceof Response? true
instanceof NextResponse? true
Response -- WeakMapObjectId: 1
---- proto NextResponse -- WeakMapObjectId: 2
---- proto _Response -- WeakMapObjectId: 1
---- proto Object -- WeakMapObjectId: 3

// Not working output, instanceOf Resposne is false and objectId for Response is now 4 for some reason.
instanceof Response? false
instanceof NextResponse? true
Response -- WeakMapObjectId: 4
---- proto NextResponse -- WeakMapObjectId: 2
---- proto _Response -- WeakMapObjectId: 1
---- proto Object -- WeakMapObjectId: 3


Any ideas how Response could change while runinng the server? What the heck!?
Answered by Giant panda
export const GET = (req: NextApiRequest, res: NextApiResponse) => {
  return auth0().handleAuth({
    logout: auth0().handleLogout({ returnTo: "/api/auth/login" }),
  })(req, res);
};
View full answer

4 Replies

Giant pandaOP
I think Response is changing on the fly due to a polyfill that's getting included.
what is your code of route handler
Giant pandaOP
export const GET = (req: NextApiRequest, res: NextApiResponse) => {
  return auth0().handleAuth({
    logout: auth0().handleLogout({ returnTo: "/api/auth/login" }),
  })(req, res);
};
Answer
Giant pandaOP
I traced the issue though down to a library that uses a fetch polyfill -- https://github.com/lquixada/cross-fetch

So NextResponse was defined using the original Response object and then global.Response changed, and so instanceof Response started failing