【问题标题】:React nested route fails to load on refreshReact 嵌套路由在刷新时无法加载
【发布时间】:2017-02-25 18:23:50
【问题描述】:

我有一个由 react-router 提供导航的 React 应用程序,我在开发中运行该应用程序时使用 webpack-dev-server 并启用了历史回退选项。这是我在index.js中定义的路线

ReactDOM.render((
  <Router history={browserHistory}>
    <Route path="/" component={App}>
      <IndexRedirect to="/intro" />
      <Route path="/intro" component={Intro} />
      <Route path="/device" component={Device} />
      <Route path="/clothing" component={Clothing} />
      <Route path="/build" component={Build}>
        <IndexRedirect to="/build/pattern" />
        <Route path="/build/pattern" component={Pattern} />
        <Route path="/build/layout" component={Layout} />
        <Route path="/build/color" component={Color} />
      </Route>
      <Route path="/capture" component={Capture} />
      <Route path="/review" component={Review} />
    </Route>
  </Router>
), document.getElementById('app'))

当我浏览链接时,一切正常,我可以看到嵌套的路由组件按预期嵌套在其父路由组件中。例如,当我导航到/build/color 时,我可以看到我的App 组件嵌套了Build 组件,其中嵌套了Color 组件。

失败的地方是当我尝试点击嵌套路由中的刷新按钮时。 React 完全无法加载,我收到以下错误

GET http://localhost:8080/build/app.js 404(未找到)

我的应用程序中确实没有这样的文件,但我仍然对为什么它会自动查找此文件而不是从根目录重新加载路由感到困惑。请注意,在 /device 之类的页面上点击刷新确实没有问题。

如果您需要有关我的设置的更多详细信息,请告诉我。 谢谢!


解决方案:

我的webpack 设置实际上是使用HtmlWebpackPlugin 注入我的应用程序包的路径,如下所示

&lt;script src="app.js" type="text/javascript"&gt;&lt;/script&gt;

我需要做的就是将我的webpack 公共路径配置为publicPath: '/',以便按如下方式注入包

&lt;script src="/app.js" type="text/javascript"&gt;&lt;/script&gt;

【问题讨论】:

    标签: reactjs routing http-status-code-404 react-router webpack-dev-server


    【解决方案1】:

    在package.json文件中,设置一下

        "homepage": "/"
    

    【讨论】:

    • 这对我有用! React 隐藏了所有 webpack 配置,我唯一遇到的问题是 "homepage": "./" 所以谢谢!
    【解决方案2】:

    对于那些尝试过上述方法但仍然有问题的人,我在我的 webpack 配置中使用了 html-webpack-plugin,因此它将捆绑包注入到 HTML 中,而无需在 index.html 上添加任何脚本导入

    所以我从我的 index.html 文件中删除以下行:

    <script src="/bundle.js"></script>
    

    并在 webpack 插件部分添加以下配置:

    // some other imports 
    const HtmlWebpackPlugin = require('html-webpack-plugin')
    
    module.exports = {
    ...
    webpack default config
    ...
    
    plugins: [
        new HtmlWebpackPlugin({
          template: './public/index.html',
          hash: true,
          inject: true,
        }),
        ...
        other plugins,
      ],
    
    }
    

    记住也要根据你的项目使用输出配置,比如我的:

    output: {
        path: path.resolve(__dirname, 'public'),
        publicPath: '/',
        filename: 'bundle.js',
      },
    

    【讨论】:

      【解决方案3】:

      @niba 的回答完美解决了我的问题。我一直遇到的问题是,当我将&lt;Route/&gt; 元素嵌套在已经由&lt;Route/&gt; 呈现的组件中时,当我尝试转到子路由 url 时,它将无法加载脚本 bundle.js .在 webpackConfig.json 中添加以下内容后,一切恢复正常。

      module.exports = {
          ...
          output: {
              path: path.resolve('dist'),
              publicPath: '/',
              filename: 'bundle.js'
          },
          ...
      }
      

      【讨论】:

      • 我只是错过了publicPath: '/',
      【解决方案4】:

      它不起作用,因为它无法加载您的 javascript 包。我猜问题出在 HTML 中脚本标记中的路径。可能你已经像这样在开头用点指定了 app.js 的路径 &lt;script src="./app.js"&gt;&lt;/script&gt;,如果是这样请删除点并检查问题是否仍然存在&lt;script src="/app.js"&gt;&lt;/script&gt;

      我们来说明./app.js和/app.js有什么区别

      案例 1. 您正在使用第一级路由加载页面,例如 / 或 /intro

      • ./app.js:HTML 尝试从 http://address/app.js 加载脚本
      • /app.js:HTML 尝试从 http://address/app.js 加载脚本

      没有区别

      案例 2. 您正在使用二级路由 /build/pattern 加载页面

      • ./app.js:HTML 尝试从 http://address/build/app.js 加载脚本 - 不存在
      • /app.js:HTML 尝试从 http://address/app.js 加载脚本 - 好的

      【讨论】:

      • 我的 webpack 设置当前将我的应用程序包注入为&lt;script src="app.js"&gt;&lt;/script&gt;,在我的情况下产生类似于./app.js 的结果。我只需要配置 webpack 来注入我的包的绝对路径。如果您也有解决方案,我也会接受:)
      • 如果你使用HTML Webpack Plugin生成HTML你需要设置webpackConfig.output.publicPath = '/'
      • 这个答案帮助我找到了一个类似问题的解决方案,即 Express 通过 index.html 为 React 前端提供服务。我不得不从我的 package.json 文件中删除 "homepage": "."。
      猜你喜欢
      • 2020-07-21
      • 2017-04-15
      • 2020-03-16
      • 2016-10-28
      • 2017-03-18
      • 2016-10-07
      • 2018-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多