[Astro SSG] Why i am redirected after a form submit
Hello,
When i submit my form i don't know why i am redirected to the
MY_URL.com/api/submit
page.
I am using Astro (SSG) (V 3.3.4) with Pages (V 8.0.0) and Wrangler (V 3.22.4)
What a want to do?
- I want to create a simple form. When i submit the form my cloudflare pages function send me an email (with Resend.com api)
What is the problem?
- Everything is working, when i submit my form it send my an email but i don't want to be redirect from the MY_URL.com/contact
to MY_URL.com/api/submit
- I have this problem in dev (local) and in deployment
I don't understand why i have this redirection (Step 2 in the screenshoot)
My code:
pages/contact.astro
functions/api/submit.js
3 Replies
It's work like the exemple her:
https://github.com/cloudflare/submit.pages.dev
Form page
You have the same redirection when you submit the form.
I didn't find how to make my function working without this redirect
GitHub
GitHub - cloudflare/submit.pages.dev
Contribute to cloudflare/submit.pages.dev development by creating an account on GitHub.
i'm not familiar with astro but had the same problem in NextJS, I didn't use the form element and instead made an onclick on my submit button that will fetch to my api. Hopefully this helps you
I think i have fixed my problem, thanks @Befus
I thought that it's a problem with my worker
/functions/api/submit.js
but it was not, it's an Astro problem. Because i was using SSG, if i was using SSR i probably didn't got this redirect.
Source: Astro doc
But i don't want use SSR just for a contact/newsletter form. I think it's possible to do that with SSG, i didn't change anything in functions/api/submit.js
file but i did some modifications on my form pages/contact.astro
It's look like that:
I don't have redirection or page refresh with this code but i don't know if it's the right way to do it or notDocs
Build HTML forms in Astro pages
Learn how to build HTML forms and handle submissions in your frontmatter.