如何从js和css文件的名称中删除contenthash(hash)



Google缓存每天只更新一到两次,并且在任何构建的缓存页面上都会破坏网站。有一个请求是为了避免在任何构建中出现损坏的谷歌缓存网站。因此,一个解决方案是从js文件的名称中间删除contenthash。并删除css文件的哈希。

将下一个代码推送到next.config.js

module.exports = {
webpack(config, options) {
if (!options.dev) {
const NextMiniCssExtractPlugin = config.plugins[11];
config.output.filename = config.output.filename.replace('-[contenthash]', '');
config.output.chunkFilename = config.output.chunkFilename.replace('.[contenthash]', '');
config.module.generator.asset.filename = config.module.generator.asset.filename.replace('.[hash:8]', '');
if (NextMiniCssExtractPlugin) {
NextMiniCssExtractPlugin.options.filename = NextMiniCssExtractPlugin.options.filename.replace('[contenthash]', '[name]');
NextMiniCssExtractPlugin.options.chunkFilename = NextMiniCssExtractPlugin.options.chunkFilename.replace('[contenthash]', '[name]');
}
}
return config;
},
...
}

这段代码从nextjs的构建配置文件名中删除了contenthash和hash:8。允许在js和css文件的构建目录中使用不带哈希的文件名。

最新更新