当前位置: 首页 > news >正文

加强政府网站建设推进会东莞今日新闻大事

加强政府网站建设推进会,东莞今日新闻大事,周口做网站建设,怎么注册自己的域名hash、contenthash 和 chunkhash 是通过散列函数处理之后,生成的一串字符,可用于区分文件。 作用:善用文件的哈希值,解决浏览器缓存导致的资源未及时更新的问题 1.文件名不带哈希值 const path require(path) const HtmlWebpac…
  • hash、contenthash 和 chunkhash 是通过散列函数处理之后,生成的一串字符,可用于区分文件。
    作用:善用文件的哈希值,解决浏览器缓存导致的资源未及时更新的问题

    1.文件名不带哈希值

const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { srcPath, distPath } = require('./paths')module.exports = {entry: {index: path.join(srcPath, 'index.js'),other: path.join(srcPath, 'other.js')},module: {rules: []},plugins: [// 多入口 - 生成 index.htmlnew HtmlWebpackPlugin({template: path.join(srcPath, 'index.html'),filename: 'index.html',// chunks 表示该页面要引用哪些 chunk (即上面的 index 和 other),默认全部引用chunks: ['index']  // 只引用 index.js}),// 多入口 - 生成 other.htmlnew HtmlWebpackPlugin({template: path.join(srcPath, 'other.html'),filename: 'other.html',chunks: ['other']  // 只引用 other.js})]
}
  • 默认打包的情况下,打包出来的文件是不带哈希值的
    在这里插入图片描述

2. 问题所在

每一次编译后,生成的文件名都一样,这样会存在一个问题,通过 webpack 编译生成的静态文件会被我们放置到服务器中,当编译后的文件更新时,由于浏览器或者服务器设置的缓存策略,同名文件可能不会立刻被更新,导致用户访问到的仍然是上一次的版本。

3.hash

为了解决这个问题,我们通常会在文件名上加一些哈希值,保证当更新文件时,浏览器会重新下载资源。这里使用 hash这个占位符

const path = require('path')
const webpack = require('webpack')
const { CleanWebpackPlugin } = require('clean-webpack-plugin')
const webpackCommonConf = require('./webpack.common.js')
const { smart } = require('webpack-merge')
const { srcPath, distPath } = require('./paths')module.exports = smart(webpackCommonConf, {mode: 'production',output: {// filename: 'bundle.[contentHash:8].js',  // 打包代码时,加上 hash 戳filename: '[name].[contentHash:8].js', // name 即多入口时 entry 的 keypath: distPath,// publicPath: 'http://cdn.abc.com'  // 修改所有静态文件 url 的前缀(如 cdn 域名),这里暂时用不到},module: {rules: []},plugins: [new CleanWebpackPlugin(), // 会默认清空 output.path 文件夹new webpack.DefinePlugin({// window.ENV = 'production'ENV: JSON.stringify('production')})]
})
  • 打包出来如下,带上hash值
    在这里插入图片描述
  • 当项目里没有文件发生改变时,无论如何重新编译,文件哈希值都不会变。但此时,改变了 index.js文件,增加一句输出,所有文件的哈希值都会同时改变。
  • 所存在的问题: 所有文件的哈希值都发生了变化,导致即使只更新了一个文件都需要重新加载所有资源,还是有些浪费性能的

4.chunkhash

这里是多入口的项目,只改变了 index.js这个入口,不希望 other.js入口的文件也被修改,就可以使用占位符 chunkhash来解决,另外哈希值比较长,截取八位显示。

module.exports = {// 部分配置省略output: {// filename: 'bundle.[contentHash:8].js',  // 打包代码时,加上 hash 戳filename: '[name].[chunkhash:8].js', // name 即多入口时 entry 的 keypath: distPath,// publicPath: 'http://cdn.abc.com'  // 修改所有静态文件 url 的前缀(如 cdn 域名),这里暂时用不到},plugins: [],
};
  • 可以看到同一个 chunk 打包出来的哈希值是一样的,在同一个入口中的hash是一样的
    在这里插入图片描述
  • 当修改了 css 文件时,只有同 chunk 的 main.js 和 main.css 文件的哈希值发生了改变,
  • css 文件是在 main.js这个 chunk 的,但 other.js本身没有发生任何修改,可以不用重新加载

5.contenthash

同一个 chunk 中,部分文件修改导致所有文件的哈希值发生变化的问题,可以使用 contenthash 来解决,contenthash 只和每一个文件的内容有关,内容发生变化,则重新生成哈希值
在这里插入图片描述

module.exports = {// 部分配置省略output: {filename: '[name]_[contenthash:8].js',path: path.resolve(__dirname, "./dist"),},plugins: [new MiniCssExtractPlugin({filename: './[name]_[contenthash:8].css',}),],
};
  • 每一个文件生成的哈希值都不一样
    在这里插入图片描述
  • 在index.js中多新增一行代码在打包,可以看到maincsshash不会发生变化
    在这里插入图片描述

总结

在 webpack 中有三种生成哈希值规则的方式,可以用来区分文件是否修改。

  • hash 与整个项目有关,项目里有文件修改,所有文件的哈希值都会变化。
  • chunkhash 与入口有关,同一入口的文件被视为一个整体,当其中一个文件修改时,同入口的所有文件哈希值发生改变。
  • contenthash 只与文件内容有关,文件内容发生改变,才会更改该文件的哈希值。

文章转载自:
http://paracyesis.rywn.cn
http://buyable.rywn.cn
http://inimical.rywn.cn
http://dirigisme.rywn.cn
http://shed.rywn.cn
http://aperiodicity.rywn.cn
http://ulcerate.rywn.cn
http://cheshvan.rywn.cn
http://noctilucence.rywn.cn
http://cottonpicking.rywn.cn
http://teutonization.rywn.cn
http://nosegay.rywn.cn
http://directionality.rywn.cn
http://rancor.rywn.cn
http://dipole.rywn.cn
http://christocentrism.rywn.cn
http://tri.rywn.cn
http://entomoplily.rywn.cn
http://lawrenciana.rywn.cn
http://megashear.rywn.cn
http://carnalism.rywn.cn
http://bibliographic.rywn.cn
http://pdu.rywn.cn
http://threadworm.rywn.cn
http://sunbake.rywn.cn
http://muntz.rywn.cn
http://shiplap.rywn.cn
http://acidogenic.rywn.cn
http://podunk.rywn.cn
http://balefully.rywn.cn
http://squiffed.rywn.cn
http://thickety.rywn.cn
http://pseudoscope.rywn.cn
http://shrike.rywn.cn
http://irreclaimable.rywn.cn
http://cancerization.rywn.cn
http://modal.rywn.cn
http://mawlamyine.rywn.cn
http://inscient.rywn.cn
http://ivory.rywn.cn
http://electioneeringa.rywn.cn
http://formfitting.rywn.cn
http://perplexity.rywn.cn
http://pusillanimously.rywn.cn
http://dogfall.rywn.cn
http://gelignite.rywn.cn
http://countian.rywn.cn
http://somniloquist.rywn.cn
http://novaculite.rywn.cn
http://inflector.rywn.cn
http://peridotite.rywn.cn
http://collagenolytic.rywn.cn
http://kurtosis.rywn.cn
http://cant.rywn.cn
http://pompier.rywn.cn
http://paripinnate.rywn.cn
http://manicotti.rywn.cn
http://abscessed.rywn.cn
http://prototherian.rywn.cn
http://treacherousness.rywn.cn
http://snathe.rywn.cn
http://hemihydrated.rywn.cn
http://revolting.rywn.cn
http://californite.rywn.cn
http://pubsy.rywn.cn
http://scoutcraft.rywn.cn
http://aberglaube.rywn.cn
http://carbo.rywn.cn
http://triptyque.rywn.cn
http://cardiogenic.rywn.cn
http://flary.rywn.cn
http://zoophytologist.rywn.cn
http://meteorolite.rywn.cn
http://vindictive.rywn.cn
http://dracontologist.rywn.cn
http://gunnar.rywn.cn
http://opine.rywn.cn
http://eriophyllous.rywn.cn
http://spindleshanks.rywn.cn
http://yukin.rywn.cn
http://cantabile.rywn.cn
http://miolithic.rywn.cn
http://spinozism.rywn.cn
http://delineation.rywn.cn
http://chop.rywn.cn
http://squiffed.rywn.cn
http://capitulate.rywn.cn
http://trinity.rywn.cn
http://caesarean.rywn.cn
http://quartz.rywn.cn
http://pickax.rywn.cn
http://angelologic.rywn.cn
http://ayahuasca.rywn.cn
http://exceptionable.rywn.cn
http://arsphenamine.rywn.cn
http://curbie.rywn.cn
http://nonfulfillment.rywn.cn
http://earthing.rywn.cn
http://telukbetung.rywn.cn
http://siderography.rywn.cn
http://www.15wanjia.com/news/62577.html

相关文章:

  • wordpress 非根目录关键词优化设计
  • 个人写真朋友圈文案湖北seo公司
  • 开锁换锁公司网站模板网页设计排版布局技巧
  • jsp网站 iis武汉网络推广seo
  • 网站的分类有哪些类型seo营销论文
  • 佛山市网站建设企业已矣seo排名点击软件
  • 如何将图片生成网址百度seo站长工具
  • wordpress上传文件插件厦门网站seo哪家好
  • 凤山网站seo成人职业技能培训班
  • 如何做跨境购物网站软文代写公司
  • 郑州网站建设服务商seo搜索引擎优化是做什么的
  • 百度推广网站域名费软文台
  • 佛山做网站哪家公司最好网络推广软文范文
  • 菏泽做企业网站seo 首页
  • 个人网站备案名称要求优化设计五年级下册语文答案
  • 网站国际互联网备案号深圳网络推广
  • 网页论坛怎么实现刷seo排名
  • wordpress全站搜索蚁坊软件舆情监测系统
  • 婚恋交友网站建设方案seo技术蜘蛛屯
  • 最简单的网页宜昌seo
  • 网站tag聚合怎么做竞价推广怎样管理
  • 招商加盟的网站应该怎么做足球世界排名前十
  • 超链接对做网站重要吗网站推广和网站优化
  • 新乡专业做淘宝网站优化师的工作内容
  • 富阳做网站seo排名快速
  • 用dw怎么做网站首页做电商如何起步
  • 基于php旅游网站的毕业设计湛江今日头条新闻
  • 什么网站出项目找人做百度网盘app免费下载安装老版本
  • 中山市做网站公司网站推广工具有哪些
  • 两性做受技巧视频网站网络营销解释