我试图让我的前端代码将所有/api请求重定向到heroku上托管的后端,但是当我转到部署的站点时,它仍然向netlify url而不是我想要的heroku url发出失败的/api请求作为代理。
我想这样做的全部原因是,在开发过程中,我在 vite.config 文件中定义了一个到后端的代理,结果却发现该文件未包含在生产版本中。
这是我立即从 React 应用程序发出的获取请求
// fetch the cookie from the server if user is logged in
const checkAuth = async () => {
fetch("/api/user/login", { credentials: "include" })
.then((res) => res.json())
.then((result) => {
console.log(result);
if (result.data.userId) setLoggedIn(true);
})
.catch((err) => console.log(err));
};
useEffect(() => {
checkAuth();
}, []);
我已按照文档中的说明将 _redirects 文件夹放置在发布目录中。就我而言,我只是运行“vite build”,它会生成一个 dist 文件夹,将 _redirects 文件移到其中,然后手动将 dist 文件粘贴到 netlify 中进行部署。它部署成功,日志显示所有重定向规则均已处理。但是当我导航到已部署的站点时,我对 /api/user/login 的初始获取请求仍然以 netlfiy url 为前缀,而不是它应该代理的 heroku url。
应该看到一个请求
https://gentle-spire-83185-d5ea8d952a7d.herokuapp.com/api/user/login
但在我的控制台中,它仍然尝试从我的 netlify url 获取端点
GET https://64cad3f677969a087f5ce558--glittering-starburst-0ee53e.netlify.app/api/user/login 404
这是我的 _redirects 文件内容:
/api/* https://gentle-spire-83185-d5ea8d952a7d.herokuapp.com/:splat 200
为什么 netlify 说我的重定向已成功处理,但仍然没有将我的 api 请求代理到 heroku url?