我有一个盖茨比生成的网站,我在其中替换了主页的内容。
不幸的是以前的版本已经提供了/page-data/index/page-data.json
使用不正确的缓存控制标头,导致/page-data/index/page-data.json
被缓存在客户端浏览器上(除非强制刷新,否则会显示陈旧的数据)。我还发现 page-data.json 文件没有经过哈希处理(请参阅https://github.com/gatsbyjs/gatsby/issues/15080 https://github.com/gatsbyjs/gatsby/issues/15080).
我已经更新了缓存控制标头,以便从现在起版本不会被缓存,但这对现在拥有缓存版本的客户端没有帮助。
我该如何强制客户端请求该文件的最新版本?
我最终到达了那里......这是我的gatsby-node.js
const hash = md5(`${new Date().getTime()}`)
const addPageDataVersion = async file => {
const stats = await util.promisify(fs.stat)(file)
if (stats.isFile()) {
console.log(`Adding version to page-data.json in ${file}..`)
let content = await util.promisify(fs.readFile)(file, 'utf8')
const result = content.replace(
/page-data.json(\?v=[a-f0-9]{32})?/g,
`page-data.json?v=${hash}`
)
await util.promisify(fs.writeFile)(file, result, 'utf8')
}
}
exports.onPostBootstrap = async () => {
const loader = path.join(__dirname, 'node_modules/gatsby/cache-dir/loader.js')
await addPageDataVersion(loader)
}
exports.onPostBuild = async () => {
const publicPath = path.join(__dirname, 'public')
const htmlAndJSFiles = glob.sync(`${publicPath}/**/*.{html,js}`)
for (let file of htmlAndJSFiles) {
await addPageDataVersion(file)
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)