Can anyone provide a solution for the CORS issue when trying to run Netlify Functions from a Webflow website?

Published on
September 22, 2023

Solution for CORS issue when running Netlify Functions from a Webflow website

To resolve the CORS (Cross-Origin Resource Sharing) issue when trying to run Netlify Functions from a Webflow website, you can follow these steps:

  1. Check the CORS headers from the server-side: Before diving into any client-side solutions, make sure the server-side code for your Netlify Function is properly configured to send the appropriate CORS headers. These headers should allow cross-origin requests from your Webflow website's domain. For example, you can include the following headers in your server response:

    ```
    Access-Control-Allow-Origin: https://www.yourwebflowwebsite.com
    Access-Control-Allow-Headers: Content-Type
    Access-Control-Allow-Methods: GET, POST, PUT, DELETE
    ```

  2. Enable CORS from the Netlify Functions configuration: Netlify Functions allows you to configure CORS directly in your netlify.toml file. Add the following code to your configuration file:

    ```toml
    [build]

    ...

    [[redirects]]
    from = "/.netlify/functions/*"
    to = "/.netlify/functions/:splat"
    status = 200
    force = true
    headers = { Access-Control-Allow-Origin = "*", Access-Control-Allow-Headers = "Content-Type" }
    ```

    This configuration snippet ensures that all function endpoints have the appropriate CORS headers set.

  3. Use a Proxy: If you are still encountering CORS issues, you can try using a proxy to forward your requests. In your Webflow website, create an API proxy via their hosting settings. Configure the proxy to point to your Netlify Function endpoint, and make the request from your Webflow website to your proxy instead of directly to the function endpoint.

  • In the Webflow Designer, go to Hosting Settings by clicking on the project name in the top-left corner and selecting Hosting Settings from the dropdown.
  • Scroll down to the API Proxies section and click on + Add New Proxy.
  • Configure the proxy by specifying the path or paths where the function request originates, and the function endpoint URL. For example, /api/* and https://your-netlify-function.netlify.app/api/:splat respectively.
  • Save the proxy and test it by making a request to the proxy URL instead of directly to the Netlify Function.

Using the above solutions, you should be able to resolve the CORS issue and successfully run Netlify Functions from your Webflow website.

Additional Questions:

  1. How can I configure CORS headers in a Netlify Function?
  2. What is Cross-Origin Resource Sharing (CORS) and why is it important?
  3. How do I set up an API proxy in Webflow hosting settings?