【问题标题】:React Router redirect on error 404反应路由器重定向错误 404
【发布时间】:2018-09-13 05:45:24
【问题描述】:

晚上好,我遇到的问题与 react 路由器有关。

这是我的 App.js,其中正在处理我的所有路由

const App = () => (

      <div className="App">
        <MediaQuery minWidth={700}>
  {(matches) => {

    if (matches) {
      return (<Router>
    <div>
      <Headroom>
      <Header />
      </Headroom>
      <main>
        <Route exact path='/' component={homepage} />
        <Route path='/home' component={homepage} />
        <Route path='/OurPubs' component={OurPubs} />
        <Route path='/OurEmail' component={OurEmail} />
        <Route path='/OurQuest' component={OurQuest} />
        <Route path='/StayRelevant' component={StayRelevant} />
        <Route path='/GetCurious' component={GetCurious} />
        <Route path='/TheGoods' component={TheGoods} />
        <Route path='/post/:slug' component={Post}>
<Redirect from="*" to="/" />
        </Route>

      </main>
    </div>
  </Router>

当我使用 GraphQL 获取数据并使用模板时(在某种意义上,每个 Post 都代表一个 Route)

&lt;Route path='/post/:slug' component={Post}&gt; 将用户带到不同的帖子。

现在,如果在移动设备上,删除缓存后,浏览器将显示错误 404 消息。这是正确的,毕竟 url post/XXXX 实际上并不存在于服务器上。

Not Found <br /> The requested URL /post/XXXX was not found on this server.

该问题仅在帖子内刷新或退出浏览器后在移动设备上出现。

问题是如何简单地将用户从未知路线带回 path="/"(家)?

【问题讨论】:

  • 我认为问题本质上是在尝试刷新时,浏览器向不存在的 url 发送请求,因此无法加载任何内容。由于我无权更改服务器配置,我几乎一无所知
  • 也许哈希路由会解决这个问题?

标签: javascript reactjs react-router graphql react-router-v4


【解决方案1】:

当来自服务器的调用以获取具有 slug 的帖子在 Post 组件中解析时,您需要检查它是否为 404,如果是,则将用户导航到主页。

...
fetch('/post/abc-post').then((obj) => {
  // check if you have 404 error
  if (obj.statusCode === 404) {
    this.props.history.push('/');
  }
})
...

如果您向我提供有关Post 组件的更多信息,或者在哪里进行调用,我可以为您提供有信心的解决方案,但基本想法将如上所示。

【讨论】:

  • query singlePost($slug: String!) { Post: Post(slug: $slug) { id slug title textContent coverImageURL description }}
  • 这是我的 graphql 查询,其中正在获取用于 url 的 slug。之后,我将该 slug 导出到 App.js 上,它被用作路由。这里导出:export default graphql(singlePost, { options: ({ match }) =&gt; ({ variables: { slug: match.params.slug }})})(Post)
【解决方案2】:

TL;DR: 配置您的 HTTP 服务器以将所有请求重定向到 /index.html


这里的问题似乎是您的 HTTP 服务器 返回 404。这意味着当您访问 /index.html(React 代码所在的位置)时,应用程序已加载,并且作为用户与应用程序交互,URL 发生变化,无需使用 react-router 重新加载页面。

刷新时,您是在向 HTTP 服务器请求一个不是您的 React 应用程序的文件。因此,您应该配置您的 HTTP 服务器以将所有请求(如 /post/XXXX)重定向到 /index.html。

这个问题可能会有所帮助:https://serverfault.com/questions/188373/apache-2-2-rewrite-force-all-requests-to-index-html

【讨论】:

  • 问题是我正在使用 React,如果我在“serverfault”帖子中调用 index.html,则站点中断并且错误为:a2.blackoperatives.com/:1 Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://a2.blackoperatives.com/index.html". index.html:1 Uncaught SyntaxError: Unexpected token &lt;
  • 原来我的服务器主机更改了不允许创建或更改 .htaccess 文件的安全规则。
  • 你的服务器主机是什么?他们应该为创建某种"when any url that is not .js,.css,.png, etc arrives, serve the index.html file" 规则提供支持。请注意,您不应重定向。您希望 url 为 /XXX,但网络服务器使用 index.html 回答我会使用 FallbackReserouce 在 apache 配置上执行此操作:httpd.apache.org/docs/trunk/mod/mod_dir.html#fallbackresource
  • TL;DR:您的服务器主机必须帮助您进行此配置
【解决方案3】:

一种快速的方式,您可以在您的 api 客户端中设置一个拦截器,例如 fetch api、superagent 等:

 if (!!response && response.status === 404) {
        store.dispatch(push("/404"));
    }

【讨论】:

  • 那会是阿波罗吗?我在客户端使用 Apollo 和 graphql 和 graphcms(作为 graphql 数据库服务)
  • try { const { data } = await graphql.query({ yourQuery }); } 捕捉(错误){ console.log(err.response.status); }
  • 我把那个放在哪里?
  • 根据阿波罗文档发送请求客户端的任何地方 [github.com/apollographql/apollo-client]
  • mhhh 错误消息是 await 是一个保留字...我也应该将它放在我的 Post.js 文件的返回正文中,其中正在加载 Post,因为您将它包装在大括号?
猜你喜欢
  • 2023-01-21
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 2017-10-22
  • 1970-01-01
  • 2021-12-23
  • 2018-08-13
  • 1970-01-01
相关资源
最近更新 更多