-
Notifications
You must be signed in to change notification settings - Fork 28.5k
Error: ServerContext: [context] already defined #55750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Additionally, it seems this will only happen when server actions are enabled by setting the following in
|
After enabling server actions, I started to get the same error. Using |
Downgrading to |
@kspeyanski I still get the same issue after downgrading (cleaned up cache completely). I guess the fact that ServerContext is still canary in React, it is not supported "officially" by NextJS. The docs state that in order to use context, one must use |
Yes, it is not officially supported. I thought it would make sense to raise the bug report anyway, since something changed in 13.4.20-canary.32. I believe it has to do with the experimental React channel, as this problem only appears when using that (as a result of The "official" workaround would be to pass whatever is supposed to go to Server Context via props. |
@marozzocom that makes total, let's hope we can track attention here. @kspeyanski I was not convinced with my later result so I downgraded again and dig a little further. I can confirm Basic version of my working stack: // context.ts
import 'server-only'
import { createServerContext } from "react"
export const I18nContext = createServerContext<string>("i18n", "en")
// useI18n.tsx
import { I18nContext } from "../create-i18n-context"
export function useI18n() {
const locale = useContext(I18nContext)
...
} Hook is imported into server components, so translations get rendered server side and not shipped as client module. Just make sure you don't use this context in any client component. |
Thanks @marozzocom ! The |
I'm not sure I completely follow the discussion, so just to confirm
At least I could not get it to work. |
Sorry for the confusion. You're correct. I tested the same recipe (shown above) on both next 13.4 and 13.5. It only worked with 13.4. |
@marozzocom I just tested with both 13.4.20-canary.31 and v13.4.20-canary.32. It does work in 31. Does not work in 32. |
This issue is resolved after
I think there is no reason to keep this open, since the whole feature is undocumented. The problem was likely fixed when vendored React was updated. Let's reopen it if the problem reoccurs. |
@marozzocom I'm not really sure if v13.5.4-canary.1 resolves the issue above, at least it's not my experience. How did you end up to that conclusion? |
Folks, seems like |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Uh oh!
There was an error while loading. Please reload this page.
Link to the code that reproduces this issue
https://github.com/marozzocom/server-context
To Reproduce
npm run dev
⌘+R
or otherwiseCurrent vs. Expected behavior
Expected behaviour
Page renders again as before.
Current behaviour
The following error message, that will only be reset by restarting the development server:
Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.6.0: Wed Jul 5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000 Binaries: Node: 18.18.0 npm: 9.8.1 Yarn: 3.4.1 pnpm: 8.7.6 Relevant Packages: next: 13.5.3-canary.0 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 5.2.2 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
App Router
Additional context
Issue introduced in
This issue appears in 13.4.20-canary.32 and later releases.
Additional info
The error only appears during development mode, and is not present when running a production build.
I was able to also reproduce this in a Codesandbox environment. In Codesandbox, an edit of the page and the following hot refresh seems to be needed to trigger the error.
I also tested a deployment into Vercel. Since it runs a production build, there is no error.
The text was updated successfully, but these errors were encountered: