Netlify forms with an Astro site that is using Svelte, and SSR ... is it really this complicated?

Hello helpful people!

As the title says, I’m building a pretty simple site using Astro and Svelte, that is in SSR mode (to pull blog entries from a Xata db). I’ve had no problem setting up and using Netlify forms on a few other non-SSR sites … but now the exact same code is extremely complex to get working :frowning:

I’ve spent most of the afternoon looking at documentation and this support forum, and have implemented a few different things:

  • I couldn’t get Netlify to recognize the form at all until I put a static .html copy in the /public folder
  • I’ve added the <input type="hidden" name="form-name" value="contact"> to both versions of the form (the one in /public, and the actual page, in /src/pages/contact.astro
  • the .html version that lives in the public folder works, but am I really expected to link to that file? It won’t pull in any build updates, so if I do something like update the footer, am I supposed to manually copy the generated code in the browser that is at /contact, which would now not be linked to from anywhere, because the menu links would actually go to the /public version … ack, I can’t even describe it with out falling into a pit of confusion :frowning:
  • I would like everything to happen in the /contact (/src/pages/contact.astro) file, but that file does not seem to actually be connected to the Netlify form detection in any way, even though it has the same name value, as outlined in the documentation.

What the heck am I missing?

It may be simple by Astro & Svelte standards, but obviously there’s a lot of code ‘behind the scenes’.
A truly simple site would be plain HTML, which is what Netlify Forms was originally conceived to work with.

Yes, although it’s not due to Netlify Forms, which while far from perfect, remains unchanged.

Many of the systems utilizing SSR could be described as “sitting in front of Netlify’s default handling”, and when that’s the case they effectively break it.

The crux of it is to make sure you have an actually static form file, (not a file that may appear static but is really being served by some ‘magically generated function powered middleware’), such that the file is detected by Netlify during the post build processing, and that you can POST to that route so that Netlify’s default handling actually catches the request.

I’ll take a look at your form now and see if I can spot what might be wrong.

What is the name of that file?

@nathanmartin great, thank you so much for your timely assistance!

My confusion lies around the fact that the form code lives on the “page” already, in as much as it’s the same path (/src/pages/contact.astro) that works on a non-SSR Astro site … but I will freely admit that I have very little understanding of what changes once you enable SSR mode.

public/netlifyneedsthis.html

You and everyone else, which is really the ultimate problem.

Things are ‘easy’ but nobody understands how they work.

Note that your form is currently performing the POST to:

Testing the static page that you have results in:

I bet you’ll find that submission in your Netlify UI.

I don’t know for certain, but there’s a chance that while /contact may seem to be a “static page” as far as Astro is concerned, it may not be a traditional “static page” (actually on the CDN and served by Netlify’s default handling).

You should adjust your form to POST to /netlifyneedsthis.html and see what happens.

Wow, ok … that actually works! I added action=“/netlifyneedsthis.html” to the form tag, and all is good.

Did I miss that somewhere in the documentation? I don’t recall seeing anything about needing to add action=“/path/to/your.form”

Thank you so much for your help!

@hitchless Don’t get ahead of yourself, while it works it isn’t necessarily what you actually want for your final solution.

I can address your other concerns from your original post.

I just wanted you to first “see what works”, so that you (and anyone else that encounters this thread), can actually understand what is occurring.

What you’ve done there works purely because the file that the POST is being made to actually exists in a place where Netlify’s default handling (and thus the forms handling) takes effect.

When you POST to a route that isn’t running on that, for example something that may be running via an automatically generated function to facilitate SSR via a framework like Astro or Next, then that Netlify handling never occurs.

So to solve for that limitation what you want to do is:

  • Submit your form via AJAX
  • Submit the form to the truly ‘static’ form route that is handled by Netlify
  • Redirect the user to your actual ‘success’ page that can be handled by Astro

They recently added an explanation/example to the Next documentation:
https://docs.netlify.com/frameworks/next-js/overview/#netlify-forms-compatibility

2 Likes

Fantastic, thank you … today was a good day for learning :smiley:

1 Like

Hey, Product Manager at Netlify here, working on web framework support as part of my work :slight_smile:

I want to emphasize a few things:

  1. The file in public/ doesn’t need any styling, or any extra HTML besides the form tags (footer etc.)! it should only include the form tags and the inputs. This is for Netlify to detect form & field names during the build, and to provide a target static file for form submissions using AJAX. The end user does not see this form at all, and never navigates to it.
  2. What the user sees and interacts with is your “actual” pages and components. Submission should be as an XHR (fetch) request, and then you decide whether to stay on the same page and show some indication of success, or to navigate to a dedicated success page.
    The above is exactly how the Next.js example works (live demo and code), and it’s very very similar in that regards.

This is a minor typo.
It should say ‘does not need styling’.

Thanks, fixed!
(now adding more words to meet 20 chars minimum)

1 Like

All good information for me to learn/know, thank you as well, @elad.rosenheim :slight_smile:

How can we send a file object to that static html file? I dont see that anywhere in the docs. Does it need to be a blob?

@Rivas918 The documentation for file uploads is here:
https://docs.netlify.com/forms/setup/#file-uploads

If you scroll down you’ll see it also has documentation for submitting file uploads via AJAX:
https://docs.netlify.com/forms/setup/#submit-file-uploads-with-ajax

It’s somewhat irrelevant that it be “that static html file”, you’re really just talking about Netlify Forms, so that documentation is what you would read.