Closed NickVilaribov closed 7 months ago
This would happen if you import registerApolloClient
from a Client Component import tree.
Do you import apollo-client.ts
from a file that is marked as "use client"
, or from a file that imports from a file (etc.) that is marked as "use client"
?
This would happen if you import
registerApolloClient
from a Client Component import tree.Do you import
apollo-client.ts
from a file that is marked as"use client"
, or from a file that imports from a file (etc.) that is marked as"use client"
?
No,
Are you building for Edge functions?
Are you building for Edge functions?
Sorry, don't get it. What is Edge functions? It's really small app, I use only next-intl and apollo client.
Okay, obviously not :)
I think we need to get back to my previous question:
I see that you re-export getClient
from "@/configs"
- do you ever import "@/configs"
from a "use client"
file, or from a file that is imported from a "use client"
file, and so on?
@/configs
No, I only use import from "@/configs" in Layout.tsx and MainIntro.tsx. And both of them don't use "use client". It's really strange, inside MainIntro.tsx i use FilterClient.tsx components and it's client component.
Something seems to pull your server component file into client components here :/
Generally, Next.js build two independent versions of your app:
"use client"
."use client"
boundary and includes all files imported from these files, and files imported by those, and so on.Internally, these import completely different files for the same import
statement.
So, if you call import { ... } from '@apollo/experimental-app-support/rsc'
, from the "RSC side", it points to the file which contains the registerApolloClient
method. If you call the same import from the "Client side", it just points to an empty file, because registerApolloClient
uses React.cache
, which works only in React Server Components (and also doesn't make sense in Client Components).
That's that you experience here right now.
React itself does the same. If you import { ... } from 'react'
from the Client side, you will be able to import useState
- but if you import it from the RSC side, useState
won't be there.
Got it. It happens when I add "use client" on the Header.tsx file. Without this all works fine. Do you have any ideal how to solve it?
Your Header.tsx
imports from "@/components"
, which imports from "./main-intro"
, which imports from "./MainIntro"
, which imports from "@/configs"
which calls import { registerApolloClient } from "@apollo/experimental-nextjs-app-support/rsc"
.
You have to stop using these index.ts
files that mix server components and client components.
Your
Header.tsx
importsfrom "@/components"
, which importsfrom "./main-intro"
, which importsfrom "./MainIntro"
, which importsfrom "@/configs"
which callsimport { registerApolloClient } from "@apollo/experimental-nextjs-app-support/rsc"
.You have to stop using these
index.ts
files that mix server components and client components.
HM. I guess I catch the problem. Header.tsx works with "use client" when I remove
@phryneas You are right, another import solve it.
import { Container } from "@/components/ui";
//import { Container } from "@/components";
Exactly that :)
But why? =) This type of import isn't a mistake, it works, but not in the Next.js.
Your @/components/index.ts
file re-exports from files that are both RSC and Client Components. You can never mix them together.
I would really recommend that you never use index.ts
files like that to have a "nicer import", because all you do is pull files into your build that shouldn't be there.
A Client Component is never allowed to import from a file that contains a React Server Component, because that "makes it a Client Component". In some cases it's just a bundling error, in some cases it can even pose a security risk.
A Client Component is never allowed to import from a file that contains a React Server Component, because that "makes it a Client Component". In some cases it's just a bundling error, in some cases it can even pose a security risk.
Thanks for your help
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.
Hey there. This is a first time I'm using Next JS 14 and Apollo client. I've done ApolloProvider and apollo-client.ts like in the documentation. In MainIntro I use an SSR request to preload the data that I use in the child component. The Header component must be client-side because I'm going to use hooks. As soon as I add "use client" to Header.tsx, I get the described error. Please help, below I post the component code and errors.
error
apollo-client.ts
apollo-wrapper.tsx
lauout.tsx
page.tsx
MainIntro.tsx
header.tsx