You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have recently started using your middleware package on a personal project and I quite enjoyed following through with the docs and implementing what I needed. At some point, I saw the following example.
However, when trying to setHeader after responding to the client on the API Route, I get the following error:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
I have run the debugger multiple times and res is only called once. Also, if I disable request-tracing.pipe.ts the error stops occurring.
Here is a snippet of the code that runs before the middleware.
asyncfunctiongetHandler(req: NextApiRequest,res: NextApiResponse){const{ search }=req.query// res.status(200).json(data) executes and the next middleware to execute is `request-tracing.pipe.ts`if(search){constdata=awaitforwardGeocode(searchasstring)res.status(200).json(data)👈}else{thrownewAppError(400,`Query string must define "search" key value pair.`)}}asyncfunctionhandler(req: NextApiRequest,res: NextApiResponse){if(req.method==="GET"){awaitgetHandler(req,res)}}exportdefaultwithMiddleware()(METHODS_GUARD)(handler)
Question
Has this happened before? Are there any Next API changes we are not aware of?
Expected behavior
After performing res.status(code).json(body) I expected the res to still pass through request-tracing.pipe.ts before being returned to the client, so that, timing headers can be set. However, this might not be a bug related to next-api-middleware.
Desktop (please complete the following information):
OS: Ubuntu
Version: 20.04
Next 12.2.3
The text was updated successfully, but these errors were encountered:
Thanks for answering so promptly @htunnicliff. Do you have any idea how this example can remain valid or changed to retain functionality even if done in a slightly different manner?
What I have done so far was override the ServerResponse prototype to include a jsonWrite function that does write(json.Stringify(data)) without invoking end like Next's utility function. Then finally, adding the end() on my error.filter.ts after the await next() statement. Noticing that error.filter.ts is which is the first and last middleware to work in the execution chain.
Edit: I can only assume that I either do not understand Express API or Next.js is doing more magic behind the scenes. Following the approach above, I still get the same error, if the request-tracing.pipe.ts is enabled with post-next() call to setHeader.
Scenario 1: do not set the response header on tracing middleware and invoke end on error.filter.ts
Works as expected. Sends the response properly, as if using native implementation. ✔️
Scenario 2: do not set the response header on tracing middleware and not invoke end on error.filter.ts
Works as expected. Does not send the response because end was not invoked. ✔️ :
Scenario 3: set the response header on tracing middleware after doing the write and invoke end on error.filter.ts
Fails with error ERR_HTTP_HEADERS_SENT. Never reaches end statement but somehow get the error saying response was already sent ✖️
Describe the bug
I have recently started using your middleware package on a personal project and I quite enjoyed following through with the docs and implementing what I needed. At some point, I saw the following example.
However, when trying to
setHeader
after responding to the client on theAPI Route
, I get the following error:Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
I have run the debugger multiple times and
res
is only called once. Also, if I disablerequest-tracing.pipe.ts
the error stops occurring.Here is a snippet of the code that runs before the middleware.
Question
Has this happened before? Are there any Next API changes we are not aware of?
Expected behavior
After performing
res.status(code).json(body)
I expected theres
to still pass throughrequest-tracing.pipe.ts
before being returned to the client, so that, timingheaders
can be set. However, this might not be a bug related tonext-api-middleware
.Desktop (please complete the following information):
The text was updated successfully, but these errors were encountered: