Issue with AsyncLocalStorage in Next.js 14: context is not being shared between middleware, handlers and SSR pages #67305
Replies: 5 comments 1 reply
-
unfortunately, i have the same problem. |
Beta Was this translation helpful? Give feedback.
-
Having the same issue and wondering about a solution particular to this problem or a general solution in integrating a proper logging mechanism with context propagation for a NextJs App. My files look the following and I'm having trouble accessing the store server side when a transaction is triggered through the client by calling a route in the ApiRouter. So the following setup for me as follows:
Example usage in Api Route:
Then Server Side like so which is part of the transaction triggered through the call of the route above:
Here the logger.info will not populate the trace field with the UUID set in the route. This is because Any idea how to solve this or any other suggestions for a lightweight solution? |
Beta Was this translation helpful? Give feedback.
-
Any news regarding this? |
Beta Was this translation helpful? Give feedback.
-
Not sure if this is really the same but have you looked at server side instrumentation https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation and the new client side instrumentation: https://nextjs.org/docs/app/api-reference/file-conventions/instrumentation-client ? |
Beta Was this translation helpful? Give feedback.
-
same problem |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I am experiencing issues with sharing the AsyncLocalStorage context between Middleware, API Handlers, and SSR Pages in a Next.js 14 project using AsyncLocalStorage. Even though the context is correctly set in the middleware, it does not propagate to API handlers or server-rendered pages. Is this a expected behaviour? I want to achieve a custom distributed tracing solution that will be defined at middleware level and should be accesible in api handlers and ssr pages.
This works fine with a custom server with Express, but I would like to use the built-in server.
Here is a simplified example that reproduces the problem:
Beta Was this translation helpful? Give feedback.
All reactions