-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Boundary Serialization - Props must be serializable for components in "use client" file
warning
#74343
Comments
“use client” should be used only for marking the boundaries between client-server code - similarly to “use server”. Not every client component should be marked with it. Boundaries have additional restrictions, e.g. serializable props you mentioned |
That's easy to say, and has been said - but it doesn't make a lot of sense in practice (or in this context), nor is it a viable solution to this problem. First of all: I think that's a great model, and if this philosophy was more deeply & widely propagated (more importantly - the parts of the framework that actively oppose and prevent this are changed/fixed) it could work. Model 1: What You're Suggesting
Benefits
Caveats
Model 2: How 88% of Next.js devs use
|
“use server”, “use client” aren’t Next.js features. These are React feature that a few frameworks already implemented. Next.js merely warns you that you’re breaking React rules here. That’s the same as not marking every server code piece with “use server” |
That's true, but your response isn't contributing to the solution... passing the blame to React doesn't solve problems that we're having in Next.js. The Next.js team is known for overcoming issues, and taking responsibility for ones in the framework it is built on top of. There is a real issue here that needs to be given more effort than a 3-line copy-pasta from a different framework's docs - respectfully. (and your last sentence didn't make sense to me - SC's aren't marked by design and SA's can be marked individually when a file or module marking isn't clear/sensical) Edit: The philosophies, errors, and the entire context in question are those of Next.js. Using React as a scapegoat (instead of a consideration in a valid solution) is like if Resend shamelessly blamed AWS for email delivery problems. When you consume a service as a brand, you become the layer that takes the hit for your users. Just wanted to drive this one home because Next & Vercel has such a great history of doing so — and your effort & stance on this antagonizes that. |
Okay - let’s assume for a moment that marking every client component with “use client” should be the way and this warning should be removed. Let’s say you’re working in a big code base where you aren’t the only developer. How would you know whether you can add a unserializable prop to component? Would you search for every single usage of the component and investigate it? How would you know whether you should optimize component props because they’ll be serialized in RSC payload or not? How would you prevent other developers from adding non-serializable props to your component that’s supposed to be used from server component? How would you prevent incorrect usages where someone tries to pass unserializable prop to component that acts as a boundary? The list could go on and on 😃 These directives and the model that React team has in mind aren’t perfect imho, but they have a huge advantage - they’re 1) statically analyzable 2) you see server-client boundary at a glance. Both of which are huge when you are working with code which you didn’t author/at bigger scale/older code. |
That's a really good question... something that I really haven't given much thought. Maybe another directive (lol) on top of My main point so far is that when solving a (newer) problem like the serialization thing - breaking/changing what already works (the model/pattern) is usually not the best first step. *OR* at the end of the day, keep pushing Model 1 but fix the broken parts... so that you can use
I don't think this would be efficient, but it would be more reliable/predictable than the current behaviour until we come up with a better solution.
That's something that we need a (hopefully, different) solution for.
We need a solution for that, I agree - but meddling with client/server directives is not it. Would love to hear some more opinions on both of our stances from people in the original discussion.
DX warning is probably the best preventative measure, but again, this needs to be a result of a different solution/approach, not the intermingling with
I do see your point. But instead of creating extra complexity on the (already confusing for new AppDir users) client/server dynamic — make the solution independent of this. Maybe this means a I hope these answers make sense to you, and are productive to the goal. |
On a better solution: Thinking Process
I think issues like this need to be solved lowest-level. This is a matter of serialization of components in general (from my understanding), and should not be described using terms like "Server Actions" and "Client/Server". DirectionI think we need to take a page out of Swift's playbook here. I'm not to informed on the relevance of protocols in Typescript, but following a similar concept: Certain client components (boundaries, the ones installed in server components) need to be serializable. That's it. Mark these "boundaries" as serializable — allow regular CC's to be marked at the component, file, or module level like SA's. This would create consistency in declaration patterns. With this, we have created two separate concepts, two layers of thinking. Mitigated complexity through careful abstraction. Using an additional directive, export, marking, or component type of some sort indicating:
These are the most accurate solutions that I can think of - all somewhat more clear than the current implementation. |
My favourite idea so far is typing the props as
|
Props must be serializable for components in "use client" file
warning
Link to the code that reproduces this issue
https://github.com/inducingchaos/riley-barabash
To Reproduce
(Ignore code repro - SEE #46795)
In my experience: just create any component in a
"use client"
file that accepts a function, dispatch for state, or anything that is non-serializable as props.Current vs. Expected behavior
Current: Typescript (Next.js version) will give a warning (the title) to either a) make props serializable, or b) to postfix the prop name with 'Action'. This has led to an unnecessary amount of renamings simply to avoid this warning - where the majority of cases don't have anything to do with Server Actions at all.
Expected: No warnings when passing props unrelated to SA's or the client-server boundary, especially in cases where client components are layered.
Provide environment information
Which area(s) are affected? (Select all that apply)
Developer Experience, Linting, Partial Prerendering (PPR)
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
Didn't provide a repro because I'm lazy and I think there's enough context within the discussion thread I linked.
See: #46795
The text was updated successfully, but these errors were encountered: