Next.js Discord

Discord Forum

use server and server-only package

Answered
Broad-snouted Caiman posted this in #help-forum
Open in Discord
Broad-snouted CaimanOP
Hi there! Should I use 'use server' together with import 'server-only' for a .ts file with server actions or 'use server' is enough? What's the difference between them? Thanks in advance!
Answered by Pacific herring
They achieve very different things. You can read use server as “use (from) server” or “use server (action)”. It makes server code available to a client so you can make remote calls. server-only essentially does the exact opposite: you’ll be completely unable to call the code from the client!

Use use server for Server Actions because you need the code to be accessible to the web browser. Use server-only in a file that absolutely cannot ever be allowed to make it into the web browser.
View full answer

2 Replies

Pacific herring
They achieve very different things. You can read use server as “use (from) server” or “use server (action)”. It makes server code available to a client so you can make remote calls. server-only essentially does the exact opposite: you’ll be completely unable to call the code from the client!

Use use server for Server Actions because you need the code to be accessible to the web browser. Use server-only in a file that absolutely cannot ever be allowed to make it into the web browser.
Answer