Server only dependencies

Hi, I wanted to ask if there is something like server only dependencies, for example, I don't want to be able to use or even import something like bcrypt on the client side, and also, it shouldn't be included in the client bundle... How can I achieve that?
Solution:
Do you need server-only? Using it makes sure your file is never imported from the client. https://nextjs.org/docs/getting-started/react-essentials#the-server-only-package...
Getting Started: React Essentials
An overview of essential React features for building Next.js Applications, including Server Components.
Jump to solution
16 Replies
Steven-sensei
Steven-sensei16mo ago
I don’t know about the import part but you client bundle wont include backend lib as long as you aren’t importing it in a client component and « use it »
dickinsontiwari
dickinsontiwari16mo ago
You can use server-only package. It will throw an error if client dependency is used. This is recommended by official nextjs docs.
TheBassGuy 🎸
TheBassGuy 🎸16mo ago
Can u link where it was used? Thanks :D
Solution
Benjamin
Benjamin16mo ago
Do you need server-only? Using it makes sure your file is never imported from the client. https://nextjs.org/docs/getting-started/react-essentials#the-server-only-package
Getting Started: React Essentials
An overview of essential React features for building Next.js Applications, including Server Components.
TheBassGuy 🎸
TheBassGuy 🎸16mo ago
also for some packages like bcrypt
Benjamin
Benjamin16mo ago
I don't really understand your need. Are you talking about Next.js framework or something else?
TheBassGuy 🎸
TheBassGuy 🎸16mo ago
I am talking about next js
Benjamin
Benjamin16mo ago
In Next.js, the bundled JS sent to the client is limited and such server package is never included. server-only implicitely makes sure those imported packages in your code are never bundled in the client. You can try it, add the import in your file and check the bundled JS, you will have no reference to your package.
TheBassGuy 🎸
TheBassGuy 🎸16mo ago
ok, so like for example, I accidentally import bcrypt in my client components, it wont be bundled for the client?
Benjamin
Benjamin16mo ago
as long as it's not used in any client component, yes
TheBassGuy 🎸
TheBassGuy 🎸16mo ago
I see Thanks all :D
Benjamin
Benjamin16mo ago
Try to use fs interactions or even with your database from a use client file, you'll see what happens
TheBassGuy 🎸
TheBassGuy 🎸16mo ago
also, what are some things I can do to optimize my app?
Benjamin
Benjamin16mo ago
first, if you respect the official documentation, they give very good advices about best practices like for app router, how to use server and client component efficiently
TheBassGuy 🎸
TheBassGuy 🎸16mo ago
ohh ok Thanks
Benjamin
Benjamin16mo ago
Then, when you're testing your app, just monitor the logs, use the debug mode everywhere you can when in development mode (like your database access, etc.), it's really helpful to see how the network and caching are behaving The doc covers the usual pitfalls we tend to fall in 👍
Want results from more Discord servers?
Add your server