【问题标题】:How to tell google bot that page not found in React?如何告诉 google bot 在 React 中找不到该页面?
【发布时间】:2020-10-23 21:11:05
【问题描述】:

我有一个带有路由器的 React 应用程序,我也有 404 组件等。 但我不知道如何向 google bot 发送 404 标头。

我只是在想,由于没有 SSR 的 React 应用程序是不可能的。 我说的对吗?

【问题讨论】:

    标签: reactjs react-router server-side-rendering


    【解决方案1】:

    如果你使用Router,你可以这样设置你的页面:

    <Router>
        <Switch>
            <Route exact path = "/home" component={HomePage} />
            <Route exact path = "/sign-up" component={SignUpPage} />
            <Route exact path = "/login" component={LoginPage} />
            <Route exact path="/incorrect-login" component={IncorrectLoginPage} />
            <Route exact path = "*" component={PageNotFound} /> //404 Component
        </Switch>
    </Router>
    

    这样,您将PageNotFound 设置到路由器列表的底部,如果 React 应用程序的 url 与您之前的任何 url 不对应,则触发 PageNotFound 路由。您可以像在应用程序中的任何其他页面一样,在该页面下呈现未找到的组件。

    我还使用了exact,因此 url 必须与 Switch 语句完全相同,以便一次只能触发一个 SINGLE 页面组件。 * 基本上只表示除之前指定的所有其他 url。

    PageNotFound 被视为一种默认的 catch 语句,因此它位于底部

    【讨论】:

    • 但是 googlebot 对它的解释呢?
    猜你喜欢
    • 2011-06-18
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2011-02-13
    • 2012-02-15
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多