[英]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.