Possible to send redirect from Function on Form submission?

We have just started using Netlify Forms with a Netlify Function back end (our submission-created.js handler) on our site and are pleased with how everything is working for the most part. But I do have a question about what our options are conditionally redirecting the user in browser post-form submission.

I understand the standard way is to set the action of the form to the page you want to redirect to post-submission. And we have another form on the site where we preventDefault the submission and then, based on the values in one of the form fields, manually redirect them to different post-submission pages in the client side js.

But we have one form where we would like to submit the form, and then check an external DB in the submission-created handler, and then depending on that retrieved data, we would like to redirect the user in browser to different pages. We cannot do this client side like described above because in this case it’s not dependent on the values of the form fields, but instead, the api call we make in the function back end.

Which brings me to my question: is it possible to send a response back to the browser from the back end lambda to route the user post-submission instead of just relying on the static form action or client side manual redirects? Would like to be able to respond to the form submission with something like:

const response = {
  statusCode: 301,
  headers: {
    Location: location
  }
}
return response

And have the user routed to that location in browser upon success, but have not had any success thus far with this approach. Is this possible? Is this documented somewhere in the docs that I might have missed? Thank you for any assistance in advance!

Hi @dtflowers,

Thanks for the details. The way to do that is to handle the form submission via javascript directly. You don’t need to use Netlify forms for this, you can just submit to your function and then save the data in your DB, send out e-mails, etc… If you still want to submit the form to Netlify forms then you would make that submission using javascript and in your client-side code make a request to another function and then redirect based on the result of that function. You can do both submissions simultaneously with Promise.all() and do client-side redirect based on the result, or do the requests sequentially and use the Location header from your second function to re-route the user.