reactjs - 使用 React 路由器的 Azure 静态应用程序路由配置
问题描述
我正在尝试在 azure 上配置静态应用程序,并且正在努力正确路由。当我导航到/lti/login/
应用程序内时,它工作正常。但是如果我刷新它会抛出一个 404,这告诉我我routes.json
的设置不正确(也许)。我希望有人可以对此有所了解。
routes.json
{
"routes": [
{
"route": "/",
"serve":"/"
},
{
"route": "/lti/login/*",
"serve":"/lti/login"
}
]
}
App.js
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/lti/login/">About</Link>
</li>
</ul>
<hr />
{/* A <Switch> looks through its children <Route>s and
renders the first one that matches the current URL. */}
<Switch>
<Route exact path="/">
<Form />
</Route>
<Route path="/lti/login/*"> <----- If I navigate to this within the app and then refresh it throws a 404.
<About />
</Route>
</Switch>
</div>
</Router>
解决方案
至于最新的文档,不推荐使用路由:
以前用于配置路由的 routes.json 已弃用。如本文所述,使用 staticwebapp.config.json 为您的静态 Web 应用程序配置路由和其他设置。
现在您需要在 staticwebapp.config.json 中添加 navigationFallback 部分,如下所示:
{
"navigationFallback": {
"rewrite": "index.html",
"exclude": ["*.{svg,png,jpg,gif}","*.{css,scss}","*.js"]
}}
你可以在这里找到完整的文档:
https://docs.microsoft.com/en-us/azure/static-web-apps/configuration#fallback-routes
推荐阅读
- python - 从字符串列表中消除所有非拉丁语单词
- php - Laravel 插入后直接使用新创建的条目
- python - 管理条目小部件的事件和日期
- css - 如何在 Linux (GTK) 中更改 Firefox 侧边栏搜索字段中的文本颜色?
- java - JPA - 休眠 - OneToOne
- bash - 如何在 UNIX 中抑制来自失败重定向的错误消息?
- html - 如何在 html 中标注不带 id 的标签?
- python - 在列表中,如果一组连续的项目满足给定条件,则将它们替换为包含这些项目的子列表
- sonarqube - 无法实现单模块设计中所有模块的声纳覆盖报告需要配置
- c++ - 尽管代码完全没问题,但显示 Abort 调用的代码