Overview - Server-only modules
What is it?
Server-only modules in Next.js are JavaScript or TypeScript files that run only on the server side. They contain code that should never be sent to the browser, such as database queries, secret keys, or server logic. These modules help keep sensitive operations secure and improve performance by separating server tasks from client tasks.
Why it matters
Without server-only modules, sensitive code like database credentials or API keys could accidentally be exposed to users, causing security risks. Also, mixing server and client code can make apps slower and harder to maintain. Server-only modules solve these problems by clearly separating what runs on the server from what runs in the browser.
Where it fits
Before learning server-only modules, you should understand basic Next.js app structure and the difference between server and client environments. After mastering server-only modules, you can explore advanced server features like API routes, server components, and server actions.