[Webpack 2] Hashing with Webpack for long term caching

本文介绍如何使用Webpack的hash特性来实现资源版本控制及浏览器长期缓存优化,通过具体配置示例帮助开发者理解并应用到实际项目中。

Leveraging the browser cache is an important part of page load performance. A great way to utilize this cache is by versioning your resources. In this lesson, learn how to use Webpack’s hashing feature so you can take advantage of long term caching of your assets.

 

Install: 

npm install -D html-webpack-plugin

 

Webpack.config.js

const {resolve} = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const isTest = process.env.NODE_ENV === "test";
module.exports = env => {
  return {
    entry: './js/app.js',
    output: {
      filename: 'bundle.[chunkhash].js',
      path: resolve(__dirname, 'dist'),
      pathinfo: true,
    },
    context: resolve(__dirname, 'src'),
    devtool: env.prod ? 'source-map' : 'eval',
    module: {
      loaders: [
        {test: /\.js$/, loader: 'babel!eslint', exclude: /node_modules/},
        {test: /\.css$/, loader: 'style!css'},
      ],
    },
    plugins:[
      new HtmlWebpackPlugin(
            {
              template: './index.html'
            }
        )
    ]
  }
}

 

Remove bundle.js in index.html

Then in the browser you can see the bundle file has hash name. So everytime, you change the source code, it will update automatically

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值