reactjs - Gatsby 在生产环境中禁用缓存
问题描述
当您将 Gatsby 应用程序构建到生产版本中时,您将在控制台中看到以下内容:
success Caching JavaScript and CSS webpack compilation - 25.915s
success Caching HTML renderer compilation - 14.491s
Gatsby 缓存了一些构建的文件,这样子序列构建会更快。问题是,我正在将我的项目部署到不支持缓存的服务。部署后,一切都将被丢弃。所以把 20% 的构建时间花在缓存上是没有意义的。
有谁知道如何在构建生产构建时禁用缓存?
解决方案
有两种方法(据我所知)可以做到这一点,或者至少你可以尝试:
在您
gatsby-node.js
使用onPostBuild
API 调用中,其中设置了缓存管理:exports.onPostBuild = async function ({ cache, graphql }, { query }) { const cacheKey = "some-key-name" const twentyFourHoursInMilliseconds = 24 * 60 * 60 * 1000 // 86400000 let obj = await cache.get(cacheKey) if (!obj) { obj = { created: Date.now() } const data = await graphql(query) obj.data = data } else if (Date.now() > obj.lastChecked + twentyFourHoursInMilliseconds) { /* Reload after a day */ const data = await graphql(query) obj.data = data } obj.lastChecked = Date.now() await cache.set(cacheKey, obj) /* Do something with data ... */ }
您可以尝试在缓存起作用之前返回/跳过该功能。
尝试使用 Gatsby 标志:
module.exports = { flags: { PRESERVE_WEBPACK_CACHE: false, PRESERVE_FILE_DOWNLOAD_CACHE: false }, plugins: [...] }
在这种情况下,
PRESERVE_WEBPACK_CACHE
可能PRESERVE_FILE_DOWNLOAD_CACHE
对您有用(相关的伞形问题)。
也许你可以开始用这些方法拉线。
推荐阅读
- c# - Array.Exists - 可能的错误?
- android - 我可以在 android 应用程序上使用 Oracle No sql 数据库吗
- flutter - Android 在 Flutter 上的 ViewFlipper 替代方案
- reactjs - React js用材质ui调用路由
- c++ - 我将如何使用在 C++ 对象中扩展 libev 的库?
- sql-server - 如何备份 SQL Server 2019 Express 并上传到 Azure Blob,保留 120 天
- python - 我可以撤消 Black 对我的 Python 代码所做的格式样式更改吗?
- css - Angular Typescript:添加从数组读取的动态 CSS
- algorithm - 伪代码的成本常数
- javascript - 单击时从类元素更改此样式属性