apollographql / apollo-client-nextjs

Apollo Client support for the Next.js App Router
https://www.npmjs.com/package/@apollo/experimental-nextjs-app-support
MIT License
458 stars 35 forks source link

Unable to import in Next 15 due to registerApolloClient #383

Open oalexdoda opened 1 month ago

oalexdoda commented 1 month ago

Hi,

It looks like @apollo/experimental-nextjs-app-support is throwing an error where it can't find registerApolloClient.

Not sure if it's because of it importing from index.ssr.js vs. another file, or what I'm doing wrong.

Would appreciate some help!

Thanks!

Code:

image

Server Error:

image

phryneas commented 4 weeks ago

You should only get that if you try to import registerApolloClient from a Client Component, and it seems that this happens in a SSR step, which would indicate a Client Component.

I see that you do a server-only import at the top of the file, which should prevent that, but could you please double-check your import paths? Does any client component directly or indirectly import this? (Look out for barrel files!)

oalexdoda commented 4 weeks ago

I had to pass the use server directive in that file for the import to work and it no longer crashes.

image

but it does cause a type error here

image

it builds/works in dev, but not sure if that's how it's supposed to be.

here's the index.ts that exports things (this is all in a transpiled next.js package)

image

and how it gets exported in the package.json

image

not sure if any of this helps, but trying to give you some context of how it's used

phryneas commented 4 weeks ago

Yeah, you definitely shouldn't use the "use server", it turns everything there into server functions, not server components.

I assume that your index.ts here is the problem - if you import anything from a barrel file like that into a "use client" file, that turns everything there into "client side" code, which will then crash, even if it was only meant for the server and never for the client.

I'm afraid, but with RSC/Next.js you can't use barrel files like that - you either have to create a index.rsc.js and index.cc.js (or similar), or - probably even better for your bundle size - stop using barrel files altogether.

oalexdoda commented 3 weeks ago

Got it, that makes perfect sense - thanks a lot @phryneas !