Javascript 如何减少react应用程序的首次加载页面时间?
React项目包大小太大,尽管使用了所有压缩技术,但加载第一个索引页花费的时间太长。首先浏览器下载包js,然后加载页面。如何先加载索引页,然后加载其他组件包。 以下是webpack.jsJavascript 如何减少react应用程序的首次加载页面时间?,javascript,reactjs,npm,webpack,react-redux,Javascript,Reactjs,Npm,Webpack,React Redux,React项目包大小太大,尽管使用了所有压缩技术,但加载第一个索引页花费的时间太长。首先浏览器下载包js,然后加载页面。如何先加载索引页,然后加载其他组件包。 以下是webpack.js module.exports = { devtool: 'source-map', context: path.resolve(__dirname, '..'), entry: { 'main': [ 'bootstrap-sass!./src/theme/bootstrap
module.exports = {
devtool: 'source-map',
context: path.resolve(__dirname, '..'),
entry: {
'main': [
'bootstrap-sass!./src/theme/bootstrap.config.prod.js',
'font-awesome-webpack!./src/theme/font-awesome.config.prod.js',
'./src/client.js'
]
},
output: {
path: assetsPath,
filename: '[name]-[chunkhash].js',
chunkFilename: '[name]-[chunkhash].js',
publicPath: ''
},
module: {
loaders: [
{ test: /\.jsx?$/, exclude: /node_modules/, loaders: [strip.loader('debug'), 'babel']},
{ test: /\.json$/, loader: 'json-loader' },
{ test: /\.css$/, loader: ExtractTextPlugin.extract('style-loader', 'css-loader')},
{ test: /\.less$/, loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=2&sourceMap!autoprefixer?browsers=last 2 version!less?outputStyle=expanded&sourceMap=true&sourceMapContents=true') },
{ test: /\.scss$/, loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=2&sourceMap!autoprefixer?browsers=last 2 version!sass?outputStyle=expanded&sourceMap=true&sourceMapContents=true') },
{ test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
{ test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/font-woff" },
{ test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=application/octet-stream" },
{ test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: "file" },
{ test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: "url?limit=10000&mimetype=image/svg+xml" },
{ test: webpackIsomorphicToolsPlugin.regular_expression('images'), loader: 'url-loader?limit=10240' },
{ test: /\.html$/, loader: 'html-loader' }
]
},
progress: true,
resolve: {
modulesDirectories: [
'src',
'node_modules'
],
extensions: ['', '.json', '.js', '.jsx']
},
plugins: [
// css files from the extract-text-plugin loader
new ExtractTextPlugin('[name]-[chunkhash].css', {allChunks: true}),
// ignore dev config
new webpack.IgnorePlugin(/\.\/dev/, /\/config$/),
// optimizations
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false
}
}),
webpackIsomorphicToolsPlugin
]
您可以遵循代码拆分策略以获得有效负载。
开源库react loadable为代码拆分提供了一个react友好的API
以下是与此相关的博客链接:-
希望有帮助。您可以遵循代码拆分策略以获得有效负载。
开源库react loadable为代码拆分提供了一个react友好的API
以下是与此相关的博客链接:-
希望能有所帮助。请使用动态功能轻松拆分代码,且无需做太多更改。作为一般指导原则,动态导入所有顶级路由组件。当与相结合时,这将是一个非常有效的工具,可以快速有效地拆分代码
执行此操作时,可以使用任何可用的加载包装库(如react loadable
)在导入实际组件时显示等待的组件
例如():
从“react Loadable”导入可加载;
从“./Loading”导入加载;
常量LoadableComponent=可加载({
加载程序:()=>导入(
/*webpackPreload:true*/
/*WebPackageChunkName:“仪表板”*/
“/仪表板”
),
加载:加载,
})
导出默认类LoadableDashboard扩展React.Component{
render(){
返回;
}
}
在上面的示例中,魔术注释用于定义预加载行为和分割块名称。阅读更多关于这些的信息
使用动态导入也可以拆分非反应代码,例如存储
请使用动态函数轻松拆分代码,而无需做太多更改。作为一般指导原则,动态导入所有顶级路由组件。当与相结合时,这将是一个非常有效的工具,可以快速有效地拆分代码
执行此操作时,可以使用任何可用的加载包装库(如react loadable
)在导入实际组件时显示等待的组件
例如():
从“react Loadable”导入可加载;
从“./Loading”导入加载;
常量LoadableComponent=可加载({
加载程序:()=>导入(
/*webpackPreload:true*/
/*WebPackageChunkName:“仪表板”*/
“/仪表板”
),
加载:加载,
})
导出默认类LoadableDashboard扩展React.Component{
render(){
返回;
}
}
在上面的示例中,魔术注释用于定义预加载行为和分割块名称。阅读更多关于这些的信息
使用动态导入也可以拆分非反应代码,例如存储
我使用了上面的url,但是区块文件没有被创建。我使用了上面的url,但是区块文件没有被创建
import Loadable from 'react-loadable';
import Loading from './Loading';
const LoadableComponent = Loadable({
loader: () => import(
/* webpackPreload: true */
/* webpackChunkName: "dashboard" */
'./Dashboard'
),
loading: Loading,
})
export default class LoadableDashboard extends React.Component {
render() {
return <LoadableComponent />;
}
}