Next.js Discord

Discord Forum

call server action on different route

Answered
Sun bear posted this in #help-forum
Open in Discord
Sun bearOP
If I create a server action, it creates it on the current page endpoint I am on. How can I use a server action from a different end point? I don't want it to automatically create the action on the page I am on. This is useful for re-using code, and for revalidating the page you're on.

Thanks,
J
Answered by joulev
so,
1. it is impossible to do what you are trying to do
2. it is meaningless, you can still revalidatePath etc normally as the server action part is separate from the routing part
View full answer

23 Replies

European sprat
You can export server actions from a file
Sun bearOP
It still creates the endpoint from the same file, I need to use a different directory
how is your server action set up?
@Sun bear If I create a server action, it creates it on the current page endpoint I am on. How can I use a server action from a different end point? I don't want it to automatically create the action on the page I am on. This is useful for re-using code, and for revalidating the page you're on. Thanks, J
this is how server actions work:
* when you use it, its address is saved in the server
* when you call it from a page, the call will include the address in the payload in the form of a POST to the same URL
* the server calls the action based on the address
so,
1. it is impossible to do what you are trying to do
2. it is meaningless, you can still revalidatePath etc normally as the server action part is separate from the routing part
Answer
@joulev this is how server actions work: * when you use it, its address is saved in the server * when you call it from a page, the call will include the address in the payload in the form of a POST to the same URL * the server calls the action based on the address
Sun bearOP
Right, in SvelteKit, for example, you can manually call a server action from a different URL... is this not possible in NextJS? This is important for revalidation purposes especially
and the above holds regardless of whether you declare the action inside. aserver component or in a separate file
and what do you mean by revalidation purposes?
i already told you revalidatePath can play with this just fine
Sun bearOP
on demand isr...
the router is separate from the actions; the URL used to call the action is not important since the payload is the only thing important here
@Sun bear on demand isr...
yes so revalidatePath still works fine (i say it the third time)
@joulev i already told you revalidatePath can play with this just fine
Sun bearOP
yes, but you can't revalidate from the same url...
@Sun bear yes, but you can't revalidate from the same url...
you just revalidate("/any/path")? i don't understand your case here
@joulev yes so revalidatePath still works fine (i say it the third time)
Sun bearOP
you can't revaliate from the same path you're on... I have revalidatePath working if I call it from a different page, but not from the same page
If I create a server action on a path (say root page), it will create that action on the root page url... you can't revalidate a page from the page you're on from the server... now if I use fetch instead of a server action, no problem... but I want to use a server action... I would need to use a server action from a different url, which seems impossible in Next, hence my question here
@Sun bear you can't revaliate from the same path you're on... I have revalidatePath working if I call it from a different page, but not from the same page
i have an app running right now and i can confirm revaldiatePath("/the-page"), when called in /the-page, it works normally
Sun bearOP
that was my question...
thank you
basically you can't revalidate from the path your on, which is the real problem