我有一个附带项目,Vue.js 3
和vite
作为我的绑定器。
在每个构建之后,绑定的文件从前面的构建中获得相同的散列,例如:
index.432c7f2f.js <-- the hash will be identical after each new build
index.877e2b8d.css
vendor.67f46a28.js
因此,每次新构建之后(文件上有相同的哈希),我不得不重新加载浏览器,很难清除缓存并查看我所做的更改。
我在package.json
中尝试了package.json
,但是:
问题:
是否有任何方法配置vite以在新构建后随机创建新哈希,或者您知道清除缓存的另一个技巧吗?
发布于 2021-06-24 22:12:33
我找到了一种解决方案,如何使用构建过程对每个文件添加一个随机散列,它将清除浏览器中的缓存:
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { hash } from './src/utils/functions.js'
export default defineConfig({
plugins: [vue()],
build: {
rollupOptions: {
output: {
entryFileNames: `[name]` + hash + `.js`,
chunkFileNames: `[name]` + hash + `.js`,
assetFileNames: `[name]` + hash + `.[ext]`
}
}
}
})
// functions.js
export const hash = Math.floor(Math.random() * 90000) + 10000;
产出:
dist/index.html
dist/index87047.css
dist/index87047.js
dist/vendor87047.js
or
dist/index.html
dist/index61047.css
dist/index61047.js
dist/vendor61047.js
...
发布于 2022-10-12 01:05:03
为了摆脱@wittgenstein的回答,您也可以使用来自package.json
的版本(当您需要确保在发布到生产时缓存被破坏时):
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { version } from './package.json'
export default defineConfig({
plugins: [vue()],
build: {
rollupOptions: {
output: {
entryFileNames: `[name].js?v=${version}`,
chunkFileNames: `[name].js?v=${version}`,
assetFileNames: `[name].[ext]?v=${version}`
}
}
}
})
而且它不必是文件名本身,浏览器将看到添加到文件中的任何查询参数作为不同的文件
PWA可能会出现这样做的问题,因为这只需将版本添加到类似于上面的解决方案的文件名中:
// vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { version } from './package.json'
export default defineConfig({
plugins: [vue()],
build: {
rollupOptions: {
output: {
entryFileNames: `[name].${version}.js`,
chunkFileNames: `[name].${version}.js`,
assetFileNames: `[name].${version}.[ext]`
}
}
}
})
https://stackoverflow.com/questions/68046410
复制相似问题