简体   繁体   English

React Route Splitting代码无法加载块

[英]React Route Splitting Code cannot load chunk

i try to forwards React Route Huge App Example . 我尝试转发React Route Huge App Example But, after webpack splitting code, always load chunk with 404. 但是,在webpack拆分代码后,始终使用404加载块。

Here is my Router. 这是我的路由器。

<Router history={appHistory} >
  <Route path='/' component={RootContainer}>
    <IndexRoute component={MainContainer}></IndexRoute>
    <Route path='/demo1' getComponent={(location, cb) => {
      require.ensure([], require => {
        cb(null, require('../../components/container/DemoContainer1.jsx').default);
      });
    }}/>
    <Route path='/message/:id' getComponent={(location, cb) => {
        require.ensure([], require => {
          cb(null, require('../../components/container/DemoContainer5.jsx').default);
        });
      }}>
    </Route>
  </Route>
</Router>

Then i click the following link 然后我点击以下链接

 <Link to='/message/3'>Go</Link>

i switch to Dev Tool, i found that it load http:/localhost:8080/message/10.chunk.js 我切换到开发工具,发现它加载了http:/ localhost:8080 / message / 10.chunk.js

so that, it cause 404. Anyone know how to config the Route Config to make the splitting code correctly? 这样就导致了404错误。有人知道如何配置Route Config才能正确生成拆分代码?

Finally, i found the solution. 最后,我找到了解决方案。

webpack.config.js webpack.config.js

output: {
  path: __dirname + '/client/src/assets',
  filename: '[name].js',
  chunkFilename: '[id].chunk.js',
  publicPath: '/__build__/'
},
debug: true,
devtool: 'source-map',
devServer: {
  inline:true,
  contentBase: __dirname + '/client/src/assets',
  port: 3333,
  host: '127.0.0.1',
  historyApiFallback: true,
  publicPath: '/__build__/'
},

Need to define the publicPath. 需要定义publicPath。 Then webpack will base on the publicPath to load resource. 然后,webpack将基于publicPath加载资源。

seems like there is a bug being fixed on react router example where taion says call getComponent with nextState instead instead of location on your Routes this should be 似乎在React Router示例上已修复了一个错误,其中taion说用nextState调用getComponent而不是在Routes上定位,这应该是

    <Router history={appHistory} >
  <Route path='/' component={RootContainer}>
    <IndexRoute component={MainContainer}></IndexRoute>
    <Route path='/demo1' getComponent={(nextState, cb) => {
      require.ensure([], require => {
        cb(null, require('../../components/container/DemoContainer1.jsx').default);
      });
    }}/>
    <Route path='/message/:id' getComponent={(nextState, cb) => {
        require.ensure([], require => {
          cb(null, require('../../components/container/DemoContainer5.jsx').default);
        });
      }}>
    </Route>
  </Route>
</Router>

just check the link that you referred to in your question 只需检查您在问题中提到的链接

where the nextState is a type RouterState 其中nextState是RouterState类型

  type RouterState = {
location:Location,
routes: Array<Route>,
params: Params,
components:Array<Component>
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Webpack 基于路由的代码拆分中的异常供应商块加载行为 - Unusual Vendor Chunk loading behaviour in Webpack route based code splitting 使用webpack设置块路径a并响应延迟代码拆分 - set chunk path a with webpack and react lazy code splitting 反应代码拆分:ChunkLoadError:加载块 0 失败 - React Code-Splitting: ChunkLoadError: Loading chunk 0 failed 代码拆分不适用于 CRA + React Route v4 + 代码拆分 - Code Splitting not working with CRA + React Route v4 + Code Splitting 尝试使用 React Route 和 Webpack 实现 React “Lazy” 路由以进行代码拆分 - Trying to implement a React “Lazy” route for code splitting with React Route and Webpack 基于webpack路由的代码拆分减少了我的包大小,但增加了我的块大小 - webpack route based code splitting reduced my bundle size but increased my chunk size 代码拆分在 chrome 上加载所有块文件,但在 firefox 中单独加载它们 - Code splitting loads all chunk files on chrome but load them separately in firefox 使用React Loadable封装在HOC中的代码拆分路由组件 - Code splitting route components wrapped in a HOC with React Loadable React 基于路由的代码拆分导入 Unexpected token 错误 - React Route-based code splitting import Unexpected token error 反应路由器基本代码拆分(获取第一个块,然后在后台异步获取其他块) - React router base code-splitting (get first chunk and then get other chunks async in background)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM