I’m trying to call local functions using netlify dev in a new vite/react project. If I invoke from the command line I get the correct result. But if I try to fetch from react I get the projects’ index.html file. Here’s my netlify.toml file:
[[redirects]]
from = "/api/*"
to = "/netlify/functions/:splat"
status = 200
force = true
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
[dev]
port = 8895
targetPort = 3005
autoLaunch = true
I talked to the netlify bot and it came up with this response:
"Based on the information provided, it seems like there might be a conflict between Vite’s development server and Netlify Dev’s handling of redirects.
When you run netlify dev
, it starts up its own server and also starts your project’s server (in this case, Vite). It then proxies requests between the two. However, Vite also has its own handling of routes and may be intercepting the requests before they get to Netlify Dev’s redirect rules.
Here are a few things you can try:
- Check your Vite configuration: Ensure that Vite is not set up to handle routes starting with
/api/
. If it is, it might be intercepting these requests and servingindex.html
instead. - Use absolute URLs in development: Instead of using relative URLs like
/api/function-name
, try using the absolute URL to your function endpoint when in development. For example, if Netlify Dev is running on port 8888, you would call your function athttp://localhost:8888/.netlify/functions/function-name
. - Check your function’s setup: Make sure your function is correctly set up to handle requests. If there’s an error in your function, it might result in
index.html
being served instead.
If none of these solutions work, you might want to consider reaching out to Netlify’s support or community forum with specific details about your setup for more targeted assistance."
None of those solutions work. Here’s my vite config file:
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
port: 3005,
},
resolve: {
alias: {
"@": "/src",
},
},
});
Any ideas on how to get my local dev functions working?