-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
nextjs release breaks server component use of Apollo query latest release #10974
Comments
Thank you for the report! Are you using the Also, can you please show what exactly that import looks like? |
@phryneas phryneas
and
when constructing the queries and mutations |
I could reproduce this with |
This seems to have been fixed between canary.5 and canary.6 I think it could have been fixed by a rollback in vercel/next.js#51316 I pinged them over in vercel/next.js#51341 to test my reproduction repo before merging it in again. |
Previously it's picking the cjs bundle that nextjs cannot analyse the imports properly, by picking up ESM bundle now we can analyze which is not expected to be used on RSC server side. apollo-lcient conatins client components imports which shoudn't be bundled on RSC server layer side as those hooks are only available in SSR and client browser. Previously it's bundling everything even containing the stuff for client on server rendering. There're few way to workaround on it:
From what I saw from the documentation is If apollo client is going have server components solution then it needs to have a separate |
@huozhi |
Just to clarify this: |
I see, thanks for the explaination, we'll discuss to explore the better solution for the warning. 🙏 |
While we're exploring the different way to warn unexpected imports, will apollo client consider having a |
@huozhi : what is a |
@markerikson it would contain the code that will only run inside server components, excluding all the code which will run on client components. For example, hooks with |
@huozhi : that's definitely a problem. RTK Query is designed with two separate entry points: one that is entirely UI-agnostic and has no knowledge of React at all, and another that is React-specific and automatically generates hooks for each declared endpoint. We're still trying to work out some of the intended use cases and approaches, but it's reasonable from our perspective that a user might want to declare an RTKQ API definition using the React-specific entry point, using the plain data fetching logic it exposes within an RSC, and also import that same API definition into a client component to fetch via hooks. This should only be a runtime issue if they tried to actually use the hooks in an RSC, but it would be frustrating for Next to throw an error even if they're not being used, just imported by RTKQ. |
@markerikson Thanks for the explanation, it's super helpful! 👍 We'll move to the direction that only erroring the used ones. The change was only landed in a canary release and reverted already. Will improve the detectors first then to re-land the change that erroring on apollo-client |
On top of that: we currently pre-bundle each of RTK's entry points: This simplifies our shipping a package that works properly with ESM imports (since we don't have to do all the nonsense around I don't know how I'm supposed to split out our package into more bundles just to satisfy Next and this requirement. |
Also, for extra context on the RTK-Query thing, here is a bit more of a writeup on that: facebook/react#26460 |
@markerikson I answered on the other thread. facebook/react#26460 (comment) The user of your api can still use the same api, as long as you publish an optimized version of the inner implementation that excludes the unnecessary code branches. |
@sebmarkbage This would be forcing every ecosystem package to add an Also, even apart from that, this is not a minor thing, it requires to completely revamp the packaging setup, with the risk of breaking other things. This puts an enormous amount of churn on the ecosystem. I'd love to turn around the question here: in which way does it hurt that these hooks are in the bundle? As long as they are not executed, they don't have any consequences - and if they are, it's a runtime error that would usually already occur during bundling. That said, the real solution would still be that you don't validate all touched files, but apply some kind of dead code elimination. In the case of Apollo Client, the hooks are in completely different files than the imported values - they are just re-exported by a common parent file. |
This really is part of the RSC spec and not a “linter” per se. It’s arguably just a bug that it didn’t error before. Not sure why it didn’t - it should’ve. The workaround are fragile since any downstream dependency can change from something that accidentally doesn’t error to one that does. Dead code elimination is not semantics and changes over time as compiler change. Sometimes to do less because they realize an optimization wasn’t safe. So it can’t be relied upon anyway. In other words, even if fixed, this might break again in a minor/patch when the compiler changes. Even if it was enough to silence the warning in some cases it’s likely you still end up with a lot of bloated unnecessary code if you don’t have two separate branches so it’s also a suboptimal runtime regardless. Most code out there written specifically for client React just won’t work in a Server Component which is why early errors are important. Most libraries need to be designed with Server Components support in mind at which point there’s a change to be made where this can be addressed. That’s an opportunity to add the fork. |
That's exactly what compilers are for. We write code with focus on tree-shakability, compilers do the tree-shaking. And that will always be more efficient than us manually trying to foresee what users could want to use.
Yes, and that library you are talking about there is The part of Apollo Client we are talking about here is not written with React in mind at all - there is a small part of Apollo Client that is written for React, correct, but the core is also used in various other packages. |
I don’t get why it would be a major? It would only be an update to Consumers don’t have to update theirs. |
We don't have an |
Interesting. You’re on ESM but without exports for branching. Most issues I’ve seen with exports is when people try to use it to add ESM but you’ve already done that step. |
Yes, for years - at that point, the (Edit: I was assuming that v3 added ESM in 2020, but it seems that even 2.0 already had ESM - back in 2017, three years before the But we are barred from an |
Yes seems like you’re stuck behind a rock and a hard place. Maybe you can do You can combine this with feature tests:
Then you could optimize using the condition in the next Major. |
@sebmarkbage that looks almost like the workaround I had in mind when I said "evasion of static analysis" above. What are your thoughts something on https://github.com/phryneas/rehackt ? The first would be essentially equal to what you suggest here, but the latter should make it possible to tree-shake when bundling for the browser (although I'm not sure if React even has parts that would tree-shake in any way). |
|
Just read through the last few hours of comments and I have to say I'm pretty frustrated. I've spent months trying to upgrade the Redux family packages just to get proper Now you're telling me that I have to spend more time coming up with more bundle configurations, just to keep my code from breaking in an RSC environment, and there's no real documentation or proper examples on what we need to do to keep things working. For that matter, the suggested import workaround seems pretty bizarre. This is extremely demoralizing :( From my perspective, there is a huge amount of churn going on with React right now, and little to no documentation or help being given to the ecosystem to deal with it. I realize a lot of this stuff is still WIP, but this is putting a ridiculous amount of stress on me (and presumably other library maintainers) just trying to keep up with what's going on. I don't have a particular solution to offer atm, but you should know I'm pretty upset by this. |
I'm gonna +1 on what Mark said here, and want to raise one additional concern: what React encourages here (and does itself) is to have an export that sometimes, depending on the environment, is absolutely not typesafe. TypeScript will tell me that And this is a decision that React made for itself, which is kinda fine, but essentially you are now going around and start encouraging other libraries to potentially also remove parts of their public interface and let users discover on accident that things they just imported are I mean, of course, we can still have those imports there and at least return functions that will throw on execution, or something like that (and we will definitely be doing that instead of just |
I mean you don’t have to have the same interface for both. You can also just have two different entry points for the two environments or one shared and one extra for client. That’s a design decision for the library. The main intention is for it to have different implementations with the same API. The way React does it, by using static named exports, you still get early build errors which are just as good as TypeScript errors. That said, it’s pretty silly that TypeScript doesn’t yet support multiple environments. Like |
For clarity, the Next.js PR and It's possible to run into errors when running the Next.js We appreciate the feedback here and will make sure we consider your feedback as we roll out a proper PR here. |
At this point, I think it's reasonable to start asking when the RSC experiment is going to be cancelled. You are introducing so much uncertainty, churn and complexity into the ecosystem... for what? The purported benefits absolutely do not make up for this much chaos. |
@dbbk I know we all are here for the Twitter drama, but can we please keep the issue tracker on topic here? 😄 This issue is about potential ways Nextjs might be changing how their bundler interprets external packages. |
@leerob the messaging here is continuously switching back and forth between "this was reverted" and "we will reintroduce this, as this is how it should have worked from the start", so please, when you have found a direction you think you want to settle on, bring us in on the discussion again, let's test that together and make sure it works for everyone (or that there is at least a consistent workaround) - and please do so early enough that we have enough time to implement & release necessary (possible) changes on our side before you ship and we get flooded with bug reports :) |
When we landed #51179 it broke library like `apollo-client` as it's bundling client hooks into RSC bundle, so our RSC linter caught them and reported fatal errors. But those client hook APIs won't get executed in RSC. The original purpose of erroring on invalid hooks for server & client components was to catch bugs easier, but it might be too strict for the 3rd party libraries like `apollo-client` due to few reasons. We changed the rules only applying on user land source code. For 3rd party packages if they're not being imported correctly into proper server or client components, we're still showing runtime errors instead of fatal build errors. x-ref: apollographql/apollo-client#10974 Closes NEXT-1673
This seems to be addressed by vercel/next.js#56501 . The only question still open is if that PR also addresses #11167 - in that case we could close this issue. |
from nextjs version 13.4.6-canary.2 this package breaks when trying to use in server component
getting error
this was working just fine before the release and then the update broke it, am thinking maybe it is caused by exporting server usable apis and only client usable in the same files
I dont know but I kinda think the issue could be somewhere around there this is caused by importing
gql
used to construct the queryThe text was updated successfully, but these errors were encountered: