next-http-proxy-middleware icon indicating copy to clipboard operation
next-http-proxy-middleware copied to clipboard

API resolved without sending a response for /v2/file, this may result in stalled requests.

Open rbabayoff opened this issue 3 years ago • 11 comments

Hey there,

Getting the message in the subject after every request in my dev env when running npm run dev.

How do I make it go away?

Thanks

rbabayoff avatar Aug 03 '21 03:08 rbabayoff

@rbabayoff Please refer to the link below 😅 https://stackoverflow.com/questions/60684227/api-resolved-without-sending-a-response-in-nextjs

stegano avatar Aug 03 '21 13:08 stegano

Hey, don't see how this is relevant. It seems unrelated to the proxy middleware. When trying to implement it as is I get an error that I cannot set headers after response has already been sent. This is the exact error message: I get when calling res.setHeader in the then clause:

httpProxyMiddleware error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

rbabayoff avatar Aug 04 '21 21:08 rbabayoff

@rbabayoff The point of the answer above is to return a Promise.resolve() or Promise.reject().

stegano avatar Aug 05 '21 04:08 stegano

@stegano just returning resolve or reject didn't work for me, I needed to also call res.end() before resolving or rejecting. Below's the entire code that worked for me, please let me know if that should be ok:

return new Promise<void>((resolve, reject) => {
  httpProxyMiddleware(req, res, {
    headers: {
      Authorization: "SomeAPIKey",
    },
  }).then(() => {
    res.end();
    resolve();
  }).catch((e) => {
    res.end();
    reject();
  });
});

Thanks

rbabayoff avatar Aug 12 '21 18:08 rbabayoff

@stegano just returning resolve or reject didn't work for me, I needed to also call res.end() before resolving or rejecting. Below's the entire code that worked for me, please let me know if that should be ok:

return new Promise<void>((resolve, reject) => {
  httpProxyMiddleware(req, res, {
    headers: {
      Authorization: "SomeAPIKey",
    },
  }).then(() => {
    res.end();
    resolve();
  }).catch((e) => {
    res.end();
    reject();
  });
});

Thanks

Hi rbabayoff

There seems to be no problem in operation even with this code. However, it seems that an asynchronous function is created and processed unnecessarily.

Are the symptoms still present? In my case, the issue is not reproduced as a result of running the sample code below.

export default (req: NextApiRequest, res: NextApiResponse) => (
  httpProxyMiddleware(req, res, {
    headers: {
      Authorization: 'TEST',
    },
    // You can use the `http-proxy` option
    target: 'http://127.0.0.1:8080',
    // In addition, you can use the `pathRewrite` option provided by `next-http-proxy`
    pathRewrite: {
      '^/api/new': '/v2',
      '^/api': '',
    },
  })
);

I checked this library code again, and it was already returning Promise.resolve and Promise.reject internally. (Note: https://github.com/stegano/next-http-proxy-middleware/blob/master/src/index.ts#L65L66)

Thanks :)

stegano avatar Aug 16 '21 12:08 stegano

Hello, I'm also experiencing this warning show up and it seems like it's related to this package. I've tried implementing the suggestion from @rbabayoff but that actually caused more problems. So I'm not sure how to get rid of these warnings/errors, and if they're false positive or not.

rametta avatar Nov 22 '21 18:11 rametta

Hi @rametta Could you show me the code using next-http-proxy-middleware?

stegano avatar Nov 23 '21 23:11 stegano

Struggled with this today (even went as far as cloning and trying to debug the package itself), hoping that I can clear this up for others having this issue.

This warning has no functional impact on the proxy and it's responses, it is a "warning" after all. It looks like it has something to do with the way Next checks that their API route URLs resolve anything, the path rewrite happening during the proxy (URLs change when you rewrite them :exploding_head:), or simply having the proxy resolve the request rather than Next. They have an escape hatch for exactly this kind of problem too! Check out: externalResolver

This flag tells Next that the API is resolved externally to the built-in API route and resolvers. Check docs here.

Altogether this looks like...

// ./pages/api/[...proxy].ts
export const config = {
  api: {
    externalResolver: true
  }
}

export default (req, res) => httpProxyMiddleware(req, res, {
  target: 'http://localhost:1337,
  pathRewrite: [
    {
      patternStr: '^/api',
      replaceStr: ''
    }
  ]
})

JackCuthbert avatar Feb 07 '22 02:02 JackCuthbert

@JackCuthbert Thanks for the great answer! I'll add it to the documentation.

stegano avatar Feb 09 '22 02:02 stegano

@all-contributors please add @JackCuthbert for doc

stegano avatar Feb 13 '22 05:02 stegano

@stegano

I've put up a pull request to add @JackCuthbert! :tada:

allcontributors[bot] avatar Feb 13 '22 05:02 allcontributors[bot]