Closed kgpax closed 9 months ago
Update: the issue could be related to this code:
The intent here is that the node enableTracing
call is only done once, but there are cases (on first page refresh and on hot reload) where this gets called twice, presumably due to a race condition over two calls trying to set this.
If I put a console log inside this if (!initialized)
block, then on the occasions where I see that log written twice (which shouldn't happen, in theory), this is where the number of traces being shown increases.
Issue
Observed when using Envy on a NextJS 14 project, we have an API route which calls a 3rd party backend API once. E.g.,
When this API router is called from a web page, Envy shows both the call to the API route and the call to the backend API, as we would expect:
All good π
However... if we reload the web page which makes this call, we notice that Envy shows two traces for the backend API:
Wierd π€. What's more is that with enough page reloading or even maybe hot reloading during development, Envy will show that the API route makes many, many calls to the backend API for each call to the API route... even if hitting the API route directly. All this despite there only ever being one call actually being made to the backend API by the API route.
Interestingly, these are not even duplicate traces, since whilst almost all of the details are identical, the timings data differs, so something is legitimately treating these as separate traces to report to Envy.
Reproduction steps
I've created a branch
bug/envy-nextjs-duplicate-traces
which can demonstrate this issue. To see if for yourself:bug/envy-nextjs-duplicate-traces
branchyarn start
in the/packages/envy
directoryyarn dev
in the/examples/next
directoryVideo
Not got time for the above, I recorded myself doing it...
https://github.com/FormidableLabs/envy/assets/17857833/40bf1b0f-49cc-483d-a180-7b941cf7094c
What's going on?
withEnvy
HoF from the@envyjs/nextjs
package; if we use@envyjs/node
we don't get this issue, but there doesn't seem to be a good place to bootstrap that for Next 13/14 API routes