Next.js Discord

Discord Forum

Impossible to make a Next.js Component library where client components are on the "leaves"?

Unanswered
Australian Freshwater Crocodile posted this in #help-forum
Open in Discord
Australian Freshwater CrocodileOP
Following the philosophy that client components should be on the leaves and having most if not all components as server components.

I am building a Next.js component library bundled with tsup.

Single components - Solved
1. I pretty much had to export from package.json each component independently. And disable code splitting.
Maintains 'use client; for each individual component -> import { Button } from '@ui/Button'
Does NOT maintain use client; for individual components -> import { Button, Card } from '@ui' (unforunately, this style is more ergonomic way to import)

Component that uses client components - Unsolved
2. I have many components available to users (public), if these components contain any client components as internal components. I need to make the entire component a client component. This is because the use client; is not maintained when bundling a component that should be a server component that uses a client component (get the typical client component error). Even if the use client was maintained the server component would unecessarily become a client component.

So is this the biggest limitation of Next.js right now?

Component libraries can only be simple or low abstraction APIs so that you fine-grained control over use client; directive . And the loss of code splitting. But then again less network requests.

Any component library like mine where there is high abstraction (Choosing consistency over Composability), I have to make most if not all my components Client...

7 Replies

Wdym by on the leaves?
Australian Freshwater CrocodileOP
Nothing?
i personally have no idea about this
It's May now, did you find any solution or help with this @Australian Freshwater Crocodile ? I am facing a similar issue with an internal UI library. Trying to bundle everything together so it 'works' is just not working.
@Laustin Spayce It's May now, did you find any solution or help with this <@1117125685874524262> ? I am facing a similar issue with an internal UI library. Trying to bundle everything together so it 'works' is just not working.
Australian Freshwater CrocodileOP
No, I made some Reddit posts too and had a turborepo maintainer actually comment. Their workarounds still didn’t work.

Just passing clienty component props (event listeners) as conditionals to render client component through server component doesn’t work.

Whole component needs to be client. I gave up tbh. I switched to SvelteKit 5 beta lmao