Replies: 1 comment 3 replies
-
@victorlrz Is there a reason why you are not using The CodeSandbox is also not publicly available. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Link to the code that reproduces this issue
CodeSandbox
To Reproduce
Create a simple module (counter.ts):
Use the module in another file (instrumentation.ts):
Use the getCount function in a page (page.tsx):
console.log(getCount()); // Expected: 1, but it logs 0
Current vs. Expected behavior
I’m encountering an issue with shared state in a Next.js project where the state isn’t updating as expected across different imports. See the simplified reproduction of the problem.
In the instrumentation.ts file, the state (count) is incremented as expected, and getCount() correctly logs 1 after the increment. However, when I import and use getCount() in page.tsx, it logs 0 instead of 1, indicating that the state was not updated across the imports. I would expect to benefit from the statefulness of NodeJs on server side.
Provide environment information
Which area(s) are affected? (Select all that apply)
Module Resolution
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
What I've tried:
Questions
Is Next.js isolating module state between imports?
Could SSR or module caching be affecting the shared state across different parts of the application?
What would be the recommended approach to ensure that state updates are reflected across all imports?
I suspect this might be related to how Next.js handles module caching or server-side rendering, but I’m not sure of the best way to handle this. Any insights or suggestions would be greatly appreciated!
Thanks in advance for your help!
Beta Was this translation helpful? Give feedback.
All reactions