Hi all,
I’m having difficulty getting the contact page on my NextJS website to work with Netlify Forms. I’ve been chatting back and forth with Ask Netlify but we haven’t managed to find a solution.
My scenario is simple: I have a NextJS client component (marked with "use client"
at the top of the file) which is rendering a form. Therefore, I understand since this is a JavaScript-rendered form + it’s rendered on the client-side, Netlify won’t be able to detect it at build time. In fact I was originally getting a build error like this.
1:37:44 PM: Error message
1:37:44 PM: Error: Failed assembling prerendered content for upload
1:37:44 PM: @netlify/plugin-nextjs@5 requires migration steps to support Netlify Forms. Refer to https://ntl.fyi/next-runtime-forms-migration for migration example.
From that error message it became pretty clear that Netlify Forms is designed to work with forms as static HTML. However, this website: Using Netlify Forms with Next.js - OpenNext which is linked to in the Netlify documentation, promises a workaround for NextJS projects which I should be able to implement.
I followed the steps to the letter. I made a __forms.html
file containing the hidden HTML form within my /public
directory.
Step 3 says "In your dynamic page or form component, handle the submission by serving a POST
request to the static HTML file you’ve created above (using the example above: /__forms
)."
Fine. I coded a POST request to /__forms.html
. But no matter what, it fails with a 404. GET requests work well and return the form HTML, so that confirms I’m not trying to perform a fetch request using an invalid file path. The file indeed exists, but seems that it is not allowing POST requests, resulting in 404 responses.
Simultaneously, it’s important to note there does NOT seem to be any issue with Netlify’s form processing. On the backend, it is detecting my form (thanks to __forms.html
in /public
. If I remove this, it fails to detect the form). Furthermore, if in the browser I manually navigate to https://quentinfalzon.netlify.app/__forms.html, unhide the form from the inspector and submit the form, Netlify records this as a form submission.
So to summarize, I need to programmatically trigger a submission of the form in /public/__forms.html
. I need help with the POST request. It seems like I must be missing some step/detail, or the method outlined in the OpenNext article no longer is correct (or wasn’t correct to begin with?). Quoting step 3 again, “serving a POST
request to the static HTML file” does not seem to be allowed.
Also - side question: What’s the point of placing validation on the JS-rendered form if ultimately a bad actor can perform an arbitrary number of form submissions by accessing the static form page within /public
, unhiding it and submitting? I’m hoping there are no malicious users on these forums.
Thank you in advance everyone,
Kind regards,
Quentin