In NextJS, I have this form server action: const [errorMessage, dispatch] =…
- User
In NextJS, I have this form server action: const [errorMessage, dispatch] = useFormState(authenticate, undefined);
When running with "next dev" it is working fine, but with @cloudflare/next-on-pages, I get a 404 POST error when I submit the form.
- Assistant
It looks like the issue might be related to the way you're specifying the form action in your NextJS application. The 404 error indicates that the server is unable to find the specified route for the form action.
In NextJS, instead of using an action attribute on the form element, you typically handle form submissions using an onSubmit event handler. You can then make a request to your server or API endpoint using the fetch API or a library like Axios.
Here's an example of how you can handle form submissions in NextJS using an onSubmit event handler:
- User
The request gets made to the same endpoint, but with next dev I get a 200 code and with next-on-pages a 404
- Assistant
If the request is being made to the same endpoint but you are receiving different responses with
next devand@cloudflare/next-on-pages, it could be due to the way the server-side routing is handled in your application.One thing to check is whether the Cloudflare Workers used by
@cloudflare/next-on-pagesare correctly configured to handle the POST request to the/api/authenticateendpoint. Make sure that the route is correctly defined in your Cloudflare Workers script.Another thing to check is if there are any differences in the routing setup or configuration between your local development environment (
next dev) and the Cloudflare Workers environment (@cloudflare/next-on-pages). Make sure that the routes are correctly defined, and that all necessary configurations are in place for handling POST requests in both environments.