Clarifying the correct usage and reliability of server functions from client components #77469
Replies: 3 comments 2 replies
-
|
did you manage to get to the underlying issue? I'm having the same problems after a Stripe redirect; only by printing the error message I get "An unexpected response was received from the server." which makes no sense since the action was executed successfully. |
Beta Was this translation helpful? Give feedback.
-
|
@reh2ur, I ran into the same issues and questions. In my case, the problem was caused by the middlewares — specifically the i18n and Supabase Auth middlewares. You may want to check if your middleware is affecting the response as well. |
Beta Was this translation helpful? Give feedback.
-
|
Alright, I found this solution for my case. It's not a production-grade solution I've tested, but I hope it helps or offers a different perspective. in your middleware file ` if (isServerAction) { |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hey folks,
I'm confused about using server functions from the client side in Next.js and could use some help clarifying things.
Since the beginning, we've relied heavily on server functions declared with
'use server'for API communication. Recently, we've noticed increased occurrences of the error:Failed to find Server Action "...". Investigating further, these errors appear when calling server functions withinuseEffector Tanstack Query'suseQuery, often after redirects from our third-party payment provider. Sometimes these functions then unexpectedly returnundefinedor throwAn unexpected response was received from the server, despite normally being reliable.Here's how we're currently calling server functions:
Using
useQuery:Using
useEffect:I initially thought server functions would translate to simple fetch calls (like calling API routes). But given these issues, could someone clarify:
Why might server functions be unreliable in
useEffects and/or after certain redirects?Is there any documented recommendation or best practice for using server functions from the client?
Should we instead rely on explicit route handlers or something like TRPC for these scenarios?
Thanks for any insights!
Beta Was this translation helpful? Give feedback.
All reactions