我使用Express后端的React应用程序托管在Heroku上,所有路由都按照预期工作,直到页面刷新。以编程方式刷新页面,或者使用刷新按钮返回:
Failed to load resource: the server responded with a status of 404 (Not Found)
“后退”和“前进”按钮(在刷新前工作)停止工作,使应用程序返回的唯一方法是导航到"/“。直到下一次刷新时,一切都会正常工作。
我的服务器路线:
router.use("/api/skus", skuRoutes);
router.use("/api/categories", categoryRoutes);
// serve static files if in production
if (process.env.NODE_ENV === "production") {
router.use(express.static("client/build"));
router.get("*", (req: Request, res: Response) => {
res.sendFile(
path.resolve(__dirname, "..", "client", "build", "index.html")
);
});
}我在这里读过许多其他类似的问题,也读过很多关于在Heroku上使用react路由器的教程,但我还没有找到适合我的解决方案。
我已经尝试过使用static.json文件的解决方案:
{
"root": "build/",
"clean_urls": false,
"routes": {
"/**": "index.html"
}
},并添加创建-反应-应用程序构建包到Heroku,但这些不适用于我。
GitHub repo 这里.
当前部署这里。
发布于 2022-01-01 12:15:59
通过将我的BrowserRouter更改为HashRouter解决了这个问题。现在,刷新功能非常完美。
https://stackoverflow.com/questions/70543753
复制相似问题