Using server actions for GET requests #54337
Replies: 16 comments 2 replies
-
Getting the same wired error |
Beta Was this translation helpful? Give feedback.
-
Up |
Beta Was this translation helpful? Give feedback.
-
Seeing the same as well! |
Beta Was this translation helpful? Give feedback.
-
Yes, I also got POST requests, when using server actions for GET requests. |
Beta Was this translation helpful? Give feedback.
-
same here, any hotfix? |
Beta Was this translation helpful? Give feedback.
-
Same here |
Beta Was this translation helpful? Give feedback.
-
Same here and also: |
Beta Was this translation helpful? Give feedback.
-
Got the same Error: A React form was unexpectedly submitted. If you called form.submit() manually, consider using form.requestSubmit() instead. If you're trying to use event.stopPropagation() in a submit event handler, consider also calling event.preventDefault(). |
Beta Was this translation helpful? Give feedback.
-
Having the same issue here |
Beta Was this translation helpful? Give feedback.
-
the same problem |
Beta Was this translation helpful? Give feedback.
-
Same problem |
Beta Was this translation helpful? Give feedback.
-
Hi, I'm new to Next.js and I too thought of using Server Actions for GET requests and after digging around the docs, I found this:
This suggests that If you want to maintain the state of the app in the URL, presumably via URL Search Parameters, then you can either use the Assuming you don't maintain the state via the URL Search Params, then I think client-side fetching is what you'll need to do. Hope this helps, curious to see what you arrive at |
Beta Was this translation helpful? Give feedback.
-
Having the same issue here |
Beta Was this translation helpful? Give feedback.
-
Same issue |
Beta Was this translation helpful? Give feedback.
-
Still not fixed |
Beta Was this translation helpful? Give feedback.
-
up |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I've a search form and a results component. The search form uses the default
GET
method and the results component access the search params to filter results.Everything works great and I get to store the state of the app in the URL. But now I want to enhance it so the form submit does not reload the whole page.
I try to use Server Actions, but I notice a few things:
POST
method instead ofGET
.method="POST"
attribute).I can partially overcome the first problem by using Server Actions custom invocation and get the results from the
POST
request. But this doesn't solve the problem that I no longer have the state in the URL, so I would need to manually implement something to store the query as a URL search param.To fix the second problem, I can delay the rendering of the
action
prop of the form by using a combination of state and effect. This will avoid to have both attributesaction
andmethod="POST"
on the initial HTML render.So now it works both ways, with or without JavaScript but I do get this weird
action
value:I think I could avoid this problem by simply using an API route and doing a normal client side fetch.
I have setup a CodeSandbox with the example:
https://codesandbox.io/p/sandbox/get-data-from-server-action-in-next-js-sjj637?file=/app/page.tsx:1,1
What do you think of this approach? How would you do it instead? I feel it a bit hackish to be honest.
Beta Was this translation helpful? Give feedback.
All reactions