Javascript 在硬刷新之前,网页不会在生产中刷新
对于我的网站,我有一个flask服务器,为webpack生成的文件提供服务。不幸的是,当我更新文件时,由于浏览器缓存的原因,网页通常在硬刷新(Ctrl-F5)之前不会更新。我希望网页更新后,定期刷新,因为大多数用户不知道硬刷新。在开发中,有一些方法可以绕过硬刷新,例如WebpackDevServer。在生产中最简单的方法是什么 我有以下Javascript 在硬刷新之前,网页不会在生产中刷新,javascript,python,webpack,browser-cache,Javascript,Python,Webpack,Browser Cache,对于我的网站,我有一个flask服务器,为webpack生成的文件提供服务。不幸的是,当我更新文件时,由于浏览器缓存的原因,网页通常在硬刷新(Ctrl-F5)之前不会更新。我希望网页更新后,定期刷新,因为大多数用户不知道硬刷新。在开发中,有一些方法可以绕过硬刷新,例如WebpackDevServer。在生产中最简单的方法是什么 我有以下webpack.config.js文件: var webpack = require('webpack'); var path = require('path')
webpack.config.js
文件:
var webpack = require('webpack');
var path = require('path');
module.exports = {
entry: ['react-hot-loader/patch', './js/main.js'],
output: {
filename: "./static/bundle.js",
},
resolveLoader: {
moduleExtensions: ['-loader']
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loaders: 'babel',
query: {
presets: ['react', 'es2015', 'stage-0']
}
},
{
test: /\.css$/,
loader: 'style-loader',
},
{
test: /\.css$/,
loader: 'css-loader',
query: {
modules: true,
localIdentName: '[name]__[local]___[hash:base64:5]'
}
}
]
}
};
Flask服务器提供的index.html
文件如下所示:
<html>
<body>
<div id="app"></div>
<script src="bundle.js"></script>
</body>
</html>
这不是关于网页包的问题,服务器无法强制浏览器不使用缓存。你可以做的是给你的url添加一些后缀,比如用bundle.js替换bundle.js
,这不是关于webpack的问题,服务器无法强制浏览器不使用缓存。你可以做的是给你的url添加一些后缀,比如用bundle.js替换bundle.js
,你需要自己破坏缓存。Webpack已经为它做了准备
output: {
filename: "./static/bundle-[hash:6].js",
},
生成的捆绑文件如下所示:bundle-1e3dab.js
现在,在HTML
中:
<html>
<body>
<div id="app"></div>
<script src="bundle-1e3dab.js"></script>
</body>
</html>
以及更新散列
var fs = require('fs');
var path = require('path');
var basePath = path.join(__dirname, '/');
function replace (statsData, fileName, readFilePath, regex, assetChunkName, writeFilePath, replaceWith) {
// Read the data so that hash can be read
let stats = statsData.toJson();
if (!stats.errors.length) {
// read the file i.e. index.html and store the contents
let contents = fs.readFileSync(path.join(readFilePath, fileName), 'utf8'),
// Replace the pattern with the user-defined replacedWith variable or the chunkHash webpack provides
htmlOutput = contents.replace(
regex,
replaceWith || stats.assetsByChunkName[assetChunkName][0]
);
// Write back the modified contents into the file
fs.writeFileSync(path.join(writeFilePath, fileName), htmlOutput);
}
}
在配置内部,在模块
键后,添加以下代码:
plugins: [
function() {
// To be executed when build is done
this.plugin('done', (statsData) => {
// `statsData` has the info regarding the file bundling(hash)
// Replace the filename with the update chunkHash for build/prod only
replace(
statsData,
'index.html', // filename which needs to be modified
path.join(basePath, '/dist/'), // path from where to read index.html
/bundle\.js/i, // regex i.e. which needs to be replaced
'bundle',
path.join(basePath, '/dist/')) // path from where to write index.html, can be same if needs ot override
})
}
]
替换路径名,即可完成:)
如果您遇到任何错误,请告诉我。您需要自己清除缓存。Webpack已经为它做了准备
output: {
filename: "./static/bundle-[hash:6].js",
},
生成的捆绑文件如下所示:bundle-1e3dab.js
现在,在HTML
中:
<html>
<body>
<div id="app"></div>
<script src="bundle-1e3dab.js"></script>
</body>
</html>
以及更新散列
var fs = require('fs');
var path = require('path');
var basePath = path.join(__dirname, '/');
function replace (statsData, fileName, readFilePath, regex, assetChunkName, writeFilePath, replaceWith) {
// Read the data so that hash can be read
let stats = statsData.toJson();
if (!stats.errors.length) {
// read the file i.e. index.html and store the contents
let contents = fs.readFileSync(path.join(readFilePath, fileName), 'utf8'),
// Replace the pattern with the user-defined replacedWith variable or the chunkHash webpack provides
htmlOutput = contents.replace(
regex,
replaceWith || stats.assetsByChunkName[assetChunkName][0]
);
// Write back the modified contents into the file
fs.writeFileSync(path.join(writeFilePath, fileName), htmlOutput);
}
}
在配置内部,在模块
键后,添加以下代码:
plugins: [
function() {
// To be executed when build is done
this.plugin('done', (statsData) => {
// `statsData` has the info regarding the file bundling(hash)
// Replace the filename with the update chunkHash for build/prod only
replace(
statsData,
'index.html', // filename which needs to be modified
path.join(basePath, '/dist/'), // path from where to read index.html
/bundle\.js/i, // regex i.e. which needs to be replaced
'bundle',
path.join(basePath, '/dist/')) // path from where to write index.html, can be same if needs ot override
})
}
]
替换路径名,即可完成:)
如果您遇到任何错误,请告诉我。嗨,softvar,这是我尝试过的方法之一。不过,我想不出一种方法来将manifest插件生成的JSON文件读入index.html文件,也找不到在线示例。你知道或有这样的例子吗?谢谢。我已经用更新index.html文件hi-softvar中bundle.js所需的确切代码更新了代码,这是我尝试过的事情之一。不过,我想不出一种方法来将manifest插件生成的JSON文件读入index.html文件,也找不到在线示例。你知道或有这样的例子吗?谢谢。已经用更新index.html文件中bundle.js所需的确切代码更新了代码