Next.js 15 – Do server actions have to be inside the app folder? Here’s what I tried #83783
-
|
So I’ve been playing around with Next.js 15 and server actions, and I noticed something interesting. Normally in the docs/examples, server actions are defined inside the app folder (like directly in route files or colocated components). But in my project we’ve got a src/services/ folder where we keep logic, so I tried putting server actions there instead. Here’s what I did: When I click the button, the log shows up in the server terminal, not in the browser console. So it’s definitely running on the server side, even though the file is outside the app folder. I also tested removing the "use server" directive from the file, when I did that, the log appeared in the browser console instead. That makes me pretty confident that adding "use server" in this file forces it to run on the server. That got me thinking:
Feels a bit cleaner for organization, but I don’t wanna shoot myself in the foot later if Next suddenly decides “nope, server actions must live under app.” Would love to hear how you all are handling this! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
AFAIK, they can be anywhere, even library code, as long as Next.js can find the So, the only issue I can ever see, is if this code is in a library, and bundled As far as placing it as you describe, there should be no problem, within your own project code it should always work. |
Beta Was this translation helpful? Give feedback.
AFAIK, they can be anywhere, even library code, as long as Next.js can find the
use serverdirective in the right place. At the top of a file, or inside an async function.So, the only issue I can ever see, is if this code is in a library, and bundled
"incorrectly", for example the entire library code is put into one file such that theuse serverdirective is not in the right place, or if the directive is dropped altogether. I guess one might have to usetranspiledPackagesor something like that forsomelibrary use cases.As far as placing it as you describe, there should be no problem, within your own project code it should always work.