Skip to content
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

Types From Server-Only Modules Aren't Allowed in Client Components #363

Open
Barzi-Ahmed opened this issue Jan 2, 2025 · 0 comments
Open

Comments

@Barzi-Ahmed
Copy link

Hello,
As you might all know that there is a package called "server-only", and when you use that package as "import 'server-only" in a file, it makes that file a server module, and if you try to import it in a client component, the development server will stop and will warn you about it, as expected. This is used for security purposes. BUT types should be allowed to be imported in client component, as TypeScript types are erased during compilation, so they don't affect the runtime behavior.

This issues makes tRPC impractical to use, as client tRPC instance should have the type of the routers (routers are collection of Procedures, the P in tRPC). Below is my code sample:

import { createTRPCClient, httpBatchLink } from '@trpc/client';
import type { APP_ROUTER_TYPE } from './routers';

export const TRPC_CLIENT = createTRPCClient<APP_ROUTER_TYPE>({
  links: [
    httpBatchLink({
      url: 'http://localhost:8081/rpc',
    }),
  ],
});

And that TRPC_CLIENT instance is supposed to be used in client components, but I can't as One warns me that server-only modules cannot be used in client components.

Please make One to behave according to the expected behavior of server-only, which is allowing types to be imported to client components from server-only modules, and disallowing the rest (i.e. values, functions, etc).

Please note that I tested these in Next.js to see if it also has same issue, but it successfully allowed importing the types to client components.

zetavg added a commit that referenced this issue Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant