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-sensei2y 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
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 🎸OP2y ago
Can u link where it was used? Thanks :D
Solution
Benjamin
Benjamin2y 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 🎸OP2y ago
also for some packages like bcrypt
Benjamin
Benjamin2y ago
I don't really understand your need. Are you talking about Next.js framework or something else?
TheBassGuy 🎸
TheBassGuy 🎸OP2y ago
I am talking about next js
Benjamin
Benjamin2y 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 🎸OP2y ago
ok, so like for example, I accidentally import bcrypt in my client components, it wont be bundled for the client?
Benjamin
Benjamin2y ago
as long as it's not used in any client component, yes
TheBassGuy 🎸
TheBassGuy 🎸OP2y ago
I see Thanks all :D
Benjamin
Benjamin2y ago
Try to use fs interactions or even with your database from a use client file, you'll see what happens
TheBassGuy 🎸
TheBassGuy 🎸OP2y ago
also, what are some things I can do to optimize my app?
Benjamin
Benjamin2y 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 🎸OP2y ago
ohh ok Thanks
Benjamin
Benjamin2y 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